Emgu CV 第一个实例

来源:互联网 发布:数据库管理员能干什么 编辑:程序博客网 时间:2024/04/28 01:45

Emgu CV下载地址

http://sourceforge.net/projects/emgucv/files/

找最新的下就行了,傻瓜式安装,选择目录后自动完成安装,然后提示安装VS2008和VS2010的插件,我使用的是VS2010,然后完成操作。

 

Emgu CV是什么?

Emgu CV是.NET平台下对OpenCV图像处理库的封装,也就是.NET版。可以运行在C#、VB、VC++等。

安装完成后需要设置环境变量,比如我安装在E:/Emgu/emgucv-windows-x86 2.2.1.1150,然后再系统环境变量添加E:/Emgu/emgucv-windows-x86 2.2.1.1150/bin即可

 

编写第一个小程序

在VS2010中新建一个Windows应用程序

首先需要导入UI插件

在浏览中定位到Emgu的安装目录bin下,选择Emgu.CV.UI.dll

在引用中添加dll调用,分别是Emgu.CV.dll和Emgu.CV.ML.dll和Emgu.CV.UI.dll和Emgu.Util.dll以及ZedGraph.dll

添加完毕后放置一个Button控件和一个imagebox控件(第三张图中导入的自定义插件),然后编写代码即可

代码

[c-sharp:nogutter] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using Emgu.CV;//PS:调用的Emgu dll  
  10. using Emgu.CV.Structure;  
  11. using Emgu.Util;  
  12. using System.Threading;  
  13.   
  14. namespace Emgu1  
  15. {  
  16.     public partial class Form1 : Form  
  17.     {  
  18.         public Form1()  
  19.         {  
  20.             InitializeComponent();  
  21.         }  
  22.         private Capture capture;  
  23.         private bool captureinprocess;//判断摄像头的状态  
  24.   
  25.         private void button1_Click(object sender, EventArgs e)  
  26.         {  
  27.             if (capture != null)//摄像头不为空  
  28.             {  
  29.                 if (captureinprocess)  
  30.                 {  
  31.                     Application.Idle -= new EventHandler(processfram);  
  32.                     button1.Text = "Stop!";  
  33.                 }  
  34.                 else  
  35.                 {  
  36.                     Application.Idle += new EventHandler(processfram);  
  37.                     button1.Text = "Start!";  
  38.                 }  
  39.                 captureinprocess = !captureinprocess;  
  40.             }  
  41.             else//摄像头为空则通过Capture()方法调用  
  42.             {  
  43.                 try  
  44.                 {  
  45.                     capture = new Capture();  
  46.                 }  
  47.                 catch (NullReferenceException excpt)  
  48.                 {  
  49.                     MessageBox.Show(excpt.Message);  
  50.                 }  
  51.             }  
  52.         }  
  53.   
  54.         private void processfram(object sender, EventArgs arg)  
  55.         {  
  56.             Image<Bgr, Byte> frame = capture.QueryFrame();  
  57.             imageBox1.Image = frame;  
  58.         }  
  59.     }  
  60. }  

 

原创粉丝点击