Windows10下C# 使用EmguCV3.2 读取本机摄像头


Windows10下C# 使用EmguCV3.2 读取本机摄像头

参考路径:https://blog.csdn.net/qq_36131739/article/details/77703299

 

前面一篇文章详细介绍了如何在VS2013下配置EmguCV3.2。接下来,我们来编写代码进行摄像头的读取与显示。

先新建一个Windows窗体程序项目,配置好EmguCV,具体过程请参照前一篇博客。

在设计模式下,拖一个ImageBox到窗体上。

并拖一个按钮到窗体上,Name为startBtn,双击按钮,编写Click事件代码。具体代码如下:

 

[csharp]  view plain  copy
 
  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.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using Emgu.CV;  
  11.   
  12. namespace WindowsFormsApplication3  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.         private VideoCapture capture;  
  21.         private bool isProcess = false;  
  22.         private void startBtn_Click(object sender, EventArgs e)  
  23.         {  
  24.             if(capture != null)  
  25.             {  
  26.                 if(isProcess)  
  27.                 {  
  28.                     Application.Idle -= new EventHandler(ProcessFram);  
  29.                     this.startBtn.Text = "Stop";  
  30.                 }  
  31.                 else  
  32.                 {  
  33.                     Application.Idle += new EventHandler(ProcessFram);  
  34.                     this.startBtn.Text = "Start";  
  35.                 }  
  36.                 isProcess = !isProcess;  
  37.             }  
  38.             else  
  39.             {  
  40.                 try  
  41.                 {  
  42.                     capture = new VideoCapture();  
  43.                 }  
  44.                 catch(NullReferenceException expt)  
  45.                 {  
  46.                     MessageBox.Show(expt.Message);  
  47.                 }  
  48.             }  
  49.         }  
  50.   
  51.         private void ProcessFram(object sender,EventArgs arg)  
  52.         {  
  53.             imageBox1.Image = capture.QueryFrame();  
  54.         }  
  55.     }  
  56. }  


点击运行,可能会如下异常:System.TypeInitializationException”类型的未经处理的异常在 Emgu.CV.World.dll 中发生 


其他信息: “Emgu.CV.CvInvoke”的类型初始值设定项引发异常。

解决方法:将EmguCV bin目录下的x64 x86文件拷贝到项目下Debug目录下如: WindowsFormsApplication3\WindowsFormsApplication3\bin\Debug

这样就可以解决了


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM