Emgu CV介绍:
Emgu cv其实就是Opencv的net版本,opencv官方没有net版本的。
接下来开始我们的Demo
1.新建Winform/WPF项目(Emgu自带的控件继承Picturebox,wpf的话需要WindowsFormsHost来放置winform的控件),例子使用的是WPF的
2.使用Nuget添加Emgu的引用,我现在使用的是3.4的版本,安装这一个就可以,以前的版本Emgu很多东西都不一样,我使用最新的
安装完成时候可以在引用里面看到:
3.接下来开始使用Emgu cv相关打开摄像头
初始化
/// <summary>
/// Emgu cv 的控件
/// </summary>
ImageBox emguBox = new ImageBox();
/// <summary>
/// 用来打开摄像头,播放
/// </summary>
private VideoCapture _capture = null;
/// <summary>
/// 用来获取摄像头视频影像
/// </summary>
Mat frame;
/// <summary>
/// 初始化
/// </summary>
private void InitCamera()
{
if (_capture == null)
{
_capture = new VideoCapture(0);
//打开摄像头后获取的影像会触发事件
_capture.ImageGrabbed += _capture_ImageGrabbed;
frame = new Mat();
}
}
/// <summary>
/// 摄像头获取到影像事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public virtual void _capture_ImageGrabbed(object sender, EventArgs e)
{
if (_capture != null && _capture.Ptr != IntPtr.Zero)
{
//获取影像
_capture.Retrieve(frame);
//将影像更新到控件中显示
emguBox.Image = frame;
}
}
最后打开摄像头,就可以播放视频了
//将控件添加到host中
winformHost.Child = emguBox;
//初始化
InitCamera();
//打开摄像头
_capture.Start();
以后会介绍一些复杂的用法