【新手入门第一课】在VS2013环境、使用C#语言、结合EmguCV实现第一个入门级的程序“控制摄像头的开闭并获取图像
【新手入门第一课】在VS2013环境、使用C#语言、结合EmguCV实现第一个入门级的程序“控制摄像头的开闭并获取图像”
【新手入门第一课】在VS2013环境、使用C#语言、结合EmguCV实现第一个入门级的程序“控制摄像头的开闭并获取图像
废话不多说,先说一下博主的电脑环境配置:VS2013,Emgucv3.0(这里建议新手使用稍微老一点的版本,目前的最新版为3.4,因为不同版本的有的函数名称以及功能都变了,在网上找问题的时候,大家大部分用的都是3.0、3.1,所以这个版本也好在找资料。)
这个程序是我根据官方给出的Demo改的,上面有我自己的注释。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.Util;
namespace Single_camera
{
public partial class singlecamera : Form
{
private Capture _capture = null;
private bool _captureInProgress;
public singlecamera()
{
InitializeComponent();
_capture = new Capture();//新建一个Capture()函数的对象
_capture.ImageGrabbed += ProcessFrame;//当grab的时候,ImageGrabbed这个事件被call
CvInvoke.UseOpenCL = false;
/* try//定义异常
{
_capture = new Capture();//新建一个Capture()函数的对象
_capture.ImageGrabbed += ProcessFrame;
}
catch (NullReferenceException excpt)//捕获并处理异常
{
MessageBox.Show(excpt.Message);
}
*/
}
private void ProcessFrame(object sender, EventArgs arg)//得到不同的视频图像
{
Mat frame = new Mat();//定义frame伟Mat()函数的一个对象
_capture.Retrieve(frame, 0);//返回一个图像帧
imageBox1.Image = frame;//将捕捉的视频信息放到captureImageBox原始视频显示框中
}
private void imageBox1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)//判断是否点击切换状态键
{
// button1.Text = "停止";
// _capture.Start();//摄像头开始工作
if (_capture != null)
{
if (_captureInProgress)//_captureInProgress是逻辑量,只有两种情况,false or true
{ //stop the capture
button1.Text = "开始";
_capture.Pause();//摄像头停止工作
}
else
{
//start the capture
button1.Text = "停止";
_capture.Start();//摄像头开始工作
}
_captureInProgress = !_captureInProgress;//每次点击进来,执行完状态之后,都将状态量取反,以便下次进来的时候是不同的功能
}
}
}
}
大家在复制我的代码的时候,一定要注意引用.dll文件
引用这四个文件就行了。好了,分享就到这,如果有人看的话,大家收藏一下,有人收藏我下次再分享,哈哈。
最后上一个例程运行的图片: