带有Capture类的emgu CV C#

时间:2013-10-05 15:17:47

标签: c# .net opencv emgucv

我一直致力于一个有趣的编码项目,该项目使用emguCV以及C#和.NET。我遇到的问题是尝试在我的代码中初始化Capture()类。每次我尝试初始化Capture时都会抛出异常:

    The Type initializer for 'Emgu.CV.CvInvoke' threw an Exception
    Exception type: System.InitializationException from Emgu.CV.dll

这是我的C#代码:

class Vision
{
    private Capture cap;
    private HaarCascade haar;
    private Form1 form;

    public Vision()
    {
            form = new Form1();
            cap = new Capture();
            haar = new HaarCascade("C:\\haarcascade_frontalface_alt2.xml");
    }
    public void faceDetect()
    {
        using(Image<Bgr, Byte> nextFrame = cap.QueryFrame())
        {
            if(nextFrame != null)
            {
                Image<Gray, Byte> grayframe = nextFrame.Convert<Gray, Byte>();
                var faces = grayframe.DetectHaarCascade(haar, 1.4, 4, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(nextFrame.Width / 8, nextFrame.Height / 8))[0];
                foreach(var face in faces)
                {
                    nextFrame.Draw(face.rect, new Bgr(0, double.MaxValue, 0), 1);
                }
                form.setImage(nextFrame.ToBitmap());
            }
        }
    }
}

代码的引用是:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Emgu.CV;
    using Emgu.Util;
    using Emgu.CV.Structure;
    using Emgu.CV.CvEnum;
    using System.Drawing;

每次异常发生时,它都显示在         cap = new Capture();

我还尝试将Capture类的相机索引设置为0,1,2 ......并且也没有运气。我也想也许因为我在Mac上运行Windows而它没有检测到网络摄像头,但后来我确实下载了最新的Windows驱动程序来访问摄像头。我感谢所有提前帮助的人! : - )

3 个答案:

答案 0 :(得分:0)

当非托管DLL依赖项无法加载时,可能会发生这种情况。请尝试以下方法:

  1. 确保使用正确版本的EmguCV,即如果您在x86模式下构建解决方案,则应该具有x86版本的EmguCV,x64版本也是如此。

  2. 确保OpenCV DLL在您的PATH中(或直接在bin目录中,如果这是一个Console / WinForms / WPF应用程序。)

  3. 您可以找到如何操作here

    您还可以参考官方文档here

答案 1 :(得分:0)

对于EmguCV的第一个用户来说,这是一个非常“经典”的错误,所以不要担心,它很容易解决!

您需要为您的Visual Studio版本安装MSVCRT。此外,可能是您没有将DLL文件放在项目中,并且“如果更新则复制”。

查看有关the official emguCV site

的所有详情

答案 2 :(得分:0)

如果您使用的是x64计算机,则需要更改项目设置。转到项目设置 - &gt;构建 - &gt;目标平台 - &gt; 64。更多详情:http://www.emgu.com/wiki/index.php/Setting_up_EMGU_C_Sharp

相关问题