System.TypeInitializationException使用Emgu.CV在C# [英] System.TypeInitializationException using Emgu.CV in C#

查看:2457
本文介绍了System.TypeInitializationException使用Emgu.CV在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这一点上,我有一个函数要求从我的相机界面的图像。

At this point I have a function which calls for an image from my camera interface. This image is then saved to hard drive and also displayed in the Windows Forms GUI.

相机界面内的返回图像的函数如下:
height和width都是摄像机接口类的一部分的整数。在这种情况下,他们设置为800x600。

The function inside the camera interface which returns the image is as follows: height and width are both integers that are part of the camera interface class. In this case they were set to 800x600.

public Image<Bgr,byte> QueryFrame()
{
    Image<Bgr, byte> temp;
    lock (key)
    {
        using (Capture cap = new Capture())
        {
            cap.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT, height);
            cap.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH, width);
            temp = cap.QueryFrame().Copy();
        }
    }
    return temp;
}

调用函数多次显示首先捕获一个帧比较长时间,锁定程序不使用几秒钟。然后,捕获几帧后运行程序在Debug与Visual C#2010,VShost.exe弹出一个Windows错误:

Calling the function a number of times revealed first that capturing a frame took a comparatively long time, locking the program out of use for a few seconds. Then, after capturing a few frames while running the program in Debug with Visual C# 2010, a windows error popped up for vshost.exe:

Faulting application DashboardGUI.vshost.exe, version 10.0.30319.1, time stamp 0x4ba2084b, faulting module MSVCR90.dll, version 9.0.30729.6161, time stamp 0x4dace5b9, exception code 0xc0000005, fault offset 0x00024682, process id 0xe78, application start time 0x01cc792086025f01.

然后我继续发布应用程序并从可执行文件运行它,并得到错误:

I then proceeded to publish the application and run it from the executable and got the error:

Application: DashboardGUI.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.TypeInitializationException
Stack:
   at Emgu.CV.CvInvoke.cvReleaseCapture(IntPtr ByRef)
   at Emgu.CV.Capture.DisposeObject()
   at Emgu.Util.DisposableObject.Finalize()

但是我也有它抛出相同的异常Emgu.CV.CvInvoke.cvCreateCameraCapture(Int32)。

However I have also had it throw the same exception with Emgu.CV.CvInvoke.cvCreateCameraCapture(Int32).

导致这些问题是什么?如何避免?有没有什么方法可以捕获一个比现在更快的帧(当它不会崩溃)?

What is causing these problems? How can they be avoided? And is there any way to make capturing a frame faster than it currently does (when it doesn't crash)?

推荐答案

看着你的代码,我看到的问题。我想到的崩溃的原因是由于使用语句,我建议对不起:。好不完全是使用语句。您似乎经常访问代码以供系统处理。

I've looked at you code and I see the problem. The reason it crashes I expect is due to the using statement which I suggested Sorry :s. Well not exactly the using statement. You would appear to be accessing the code to often for the system to handle.

Capture cap = new Capture()

对于少量的代码进行大量的操作。它不仅设置与您的相机通信,但检查它存在,处理驱动程序和创建环形缓冲区等。现在虽然给出的代码确保只有一个更新的图像返回它通常只有工作良好,如果你使用按钮或定时器一段时间延迟。现在我知道你的努力,因为你想要的图像更定期比可以合理地实现与这种方法,你有一个更实际的选择。

Does a large amount of operations for a small amount of code. Not only does it set up communication with your camera but checks it exists, handles drivers and creates ring buffers etc. Now while the code given does ensure only an updated image is returned it generally only works well if your using a button or a timer with a period of time delay. Now as I am aware of what your trying to achieve and since you want images more regularly than what can reasonably be achieved with this method you have a more practical option.

设置您的Capture设备全局和设置它的记录和调用ProcessFrame从缓冲区获取图像,无论什么时候。现在改变你的QueryFrame只是复制其刚刚获取的框架。这将有希望阻止您获取上一帧的问题,您现在将最新的帧从缓冲区。

Set your Capture device up globally and set it of recording and calling ProcessFrame to get an image from the buffer whenever it can. Now change your QueryFrame simply to copy whatever frames its just acquired. This will hopefully stop your problem of getting the previous frame and you will now have the most recent frame out of the buffer.

private Capture cap;
Image<Bgr, Byte> frame;

public CameraCapture()
{
    InitializeComponent();
    cap= new Capture();
    cap.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT, height);
    cap.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH, width);

    Application.Idle += ProcessFrame;
}

private void ProcessFrame(object sender, EventArgs arg)
{
    frame = _capture.QueryFrame();
    grayFrame = frame.Convert<Gray, Byte>();
}

public Image<Bgr,byte> QueryFrame()
{
    return frame.Copy();
}

希望这有助于你这次的解决方案,
对不起,其他方法没有用,

Hope this helps get you to a solution this time, and sorry the other method was of no use,

干杯
Chris

Cheers Chris

这篇关于System.TypeInitializationException使用Emgu.CV在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆