今天尝试在visual stdio2010下通过c#调用opencv,原来需要安装Emgu CV,参照网上的教程(http://blog.csdn.net/liurong_cn/article/details/8778226)进行一系列Emgu
CV配置后,想测试一下是否配置成功,下面是个人测试的过程以及出现的问题还有最终的解决办法!亲测成功,望采纳!本人系统win7 64位 !
如图1所示,本人想实现点击“打开摄像头”按钮后,将摄像头画面实时显示到左下角的imagebox上,但是粘贴上述网址上的代码修改后出现 Emgu.CV.CvInvoke”的类型初始值设定项引发异常 的错误提示, 注意:我已经进行了必要的配置,包括注销计算机、添加环境变量等!
看了一些帖子,有大神说将Emgu CV下的.dll文件复制到工程所在目录下,有的说将具体某一个dll文件复制即可,但是我试了下还是没成功,索性将所有dll文件复制到工程文件下,结果成功了,运行结果如图2!下面具体说下,像我这样的初学者应该讲的详细点,因为很多专业的说法还不熟悉,含糊一说不如不讲!具体做法是:
1.打开 Emgu.CV 安装目录,找到dll文件所在的位置,因为有32位和64 两种系统,所以安装的时候自动两个都安装了,像我的路径是--D:\Program Files (x86)\emgucv-windows-universal-cuda
2.9.0.1922\bin\x86 ,在这个文件夹下就可以看到所有的dll文件了,但是我虽然是64位系统,用的x86即32位的dll文件照样成功,64位的没有尝试,大胆的话各位可以试试;
2.将所有dll文件复制到自己的新建工程的bin/debug文件夹下,如我的工程路径为
E:\yu\vs2010\WindowsFormsApplication2\WindowsFormsApplication2\bin\Debug,WindowsFormsApplication2为我的工程名,所有文件还是挺大的,有500多兆呢。之后再次运行程序,OK,可以了!效果如图2.
图1 我的窗体应用程序界面
图2 测试成功界面
3.下面再附上我的代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Emgu.CV; //PS:调用的Emgu dll using Emgu.CV.Structure; using Emgu.Util; using System.Threading; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private Capture capture; private bool captureinprocess;//判断摄像头的状态 private void button2_Click(object sender, EventArgs e) { if (capture!= null)//摄像头不为空 { if (captureinprocess) { button2.Text = "Stop!"; Application.Idle -= new EventHandler(processfram); } else { button2.Text = "Start!"; Application.Idle += new EventHandler(processfram); } captureinprocess = !captureinprocess; } else//摄像头为空则通过Capture()方法调用 { try { capture= new Capture(); } catch (NullReferenceException excpt) { MessageBox.Show(excpt.Message); } } } private void processfram(object sender, EventArgs arg) { Image<Bgr, Byte> frame = capture.QueryFrame(); imageBox1.Image = frame; } } }