先在Form上拉两个控制项,分別是 Button ( Name : captureButton )与 ImageBox ( Name : captureImageBox )、ImageBox ( Name : grayscaleImageBox )、ImageBox ( Name : smoothedGrayscaleImageBox )、ImageBox ( Name : cannyImageBox )。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//引用
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.Util;
namespace Emgu_CameraCapture
{
public partial class Form1 : Form
{
private Capture _capture = null;
private bool _captureInProgress;
public Form1()//构造函数
{
InitializeComponent();
CvInvoke.UseOpenCL = false; //不使用OpneCL
try
{
_capture = new Capture();//构造一个摄像头实例
_capture.ImageGrabbed += ProcessFrame;//图像捕捉事件
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
private void ProcessFrame(object sender, EventArgs arg)
{
Mat frame = new Mat();//获取视频帧
_capture.Retrieve(frame, 0);
Mat grayFrame = new Mat();
CvInvoke.CvtColor(frame, grayFrame, ColorConversion.Bgr2Gray);
Mat smallGrayFrame = new Mat();
CvInvoke.PyrDown(grayFrame, smallGrayFrame);
Mat smoothedGrayFrame = new Mat();
CvInvoke.PyrUp(smallGrayFrame, smoothedGrayFrame);
//Image<Gray, Byte> smallGrayFrame = grayFrame.PyrDown();
//Image<Gray, Byte> smoothedGrayFrame = smallGrayFrame.PyrUp();
Mat cannyFrame = new Mat();
CvInvoke.Canny(smoothedGrayFrame, cannyFrame, 100, 60);
//Image<Gray, Byte> cannyFrame = smoothedGrayFrame.Canny(100, 60);
captureImageBox.Image = frame;//转成图片并显示在主界面上
grayscaleImageBox.Image = grayFrame;
smoothedGrayscaleImageBox.Image = smoothedGrayFrame;
cannyImageBox.Image = cannyFrame;
}
private void captureButtonClick(object sender, EventArgs e)
{
if (_capture != null)
{
if (_captureInProgress)
{ //stop the capture
captureButton.Text = "Start Capture";
_capture.Pause();
}
else
{
//start the capture
captureButton.Text = "Stop";
_capture.Start();
}
_captureInProgress = !_captureInProgress;
}
}
private void ReleaseData()//释放资源
{
if (_capture != null)
_capture.Dispose();
}
private void FlipHorizontalButton_Click(object sender, EventArgs e)
{
if (_capture != null) _capture.FlipHorizontal = !_capture.FlipHorizontal;
}
private void FlipVerticalButton_Click(object sender, EventArgs e)
{
if (_capture != null) _capture.FlipVertical = !_capture.FlipVertical;
}
}
}
播放本地视频
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//引用
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.Util;
namespace Emgu_CameraCapture
{
public partial class Form1 : Form
{
private Capture _capture = null;
private bool _captureInProgress;
public Form1()//构造函数
{
InitializeComponent();
CvInvoke.UseOpenCL = false; //不使用OpneCL
try
{
//_capture = new Capture();//构造一个摄像头实例
var fileName = "E:\\Homework\\Video\\0.avi";//文件地址就这里改动
_capture = new Capture(fileName);//就这里改动
_capture.ImageGrabbed += ProcessFrame;//图像捕捉事件
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
private void ProcessFrame(object sender, EventArgs arg)
{
Mat frame = new Mat();//获取视频帧
_capture.Retrieve(frame, 0);
Mat grayFrame = new Mat();
CvInvoke.CvtColor(frame, grayFrame, ColorConversion.Bgr2Gray);
Mat smallGrayFrame = new Mat();
CvInvoke.PyrDown(grayFrame, smallGrayFrame);
Mat smoothedGrayFrame = new Mat();
CvInvoke.PyrUp(smallGrayFrame, smoothedGrayFrame);
//Image<Gray, Byte> smallGrayFrame = grayFrame.PyrDown();
//Image<Gray, Byte> smoothedGrayFrame = smallGrayFrame.PyrUp();
Mat cannyFrame = new Mat();
CvInvoke.Canny(smoothedGrayFrame, cannyFrame, 100, 60);
//Image<Gray, Byte> cannyFrame = smoothedGrayFrame.Canny(100, 60);
captureImageBox.Image = frame;//转成图片并显示在主界面上
grayscaleImageBox.Image = grayFrame;
smoothedGrayscaleImageBox.Image = smoothedGrayFrame;
cannyImageBox.Image = cannyFrame;
}
private void captureButtonClick(object sender, EventArgs e)
{
if (_capture != null)
{
if (_captureInProgress)//一开始为0,所以此句不执行
{ //stop the capture
captureButton.Text = "Start Capture";
_capture.Pause();
}
else
{
//start the capture
captureButton.Text = "Stop";
_capture.Start();
}
_captureInProgress = !_captureInProgress;
}
}
private void ReleaseData()//释放资源
{
if (_capture != null)
_capture.Dispose();
}
private void FlipHorizontalButton_Click(object sender, EventArgs e)
{
if (_capture != null) _capture.FlipHorizontal = !_capture.FlipHorizontal;//水平翻转
}
private void FlipVerticalButton_Click(object sender, EventArgs e)
{
if (_capture != null) _capture.FlipVertical = !_capture.FlipVertical;//竖直翻转
}
}
}
参考
【-1】c# 远程监控(2) 摄像头调研及模拟 - Herenwei_Wayne - 博客园
http://www.cnblogs.com/herenwei-wayne/p/4375784.html
【0】EmguCV的配置和获取摄像头画面 - CSDN博客
http://blog.csdn.net/qq_22033759/article/details/47299269
【1】VS2010、C#、Emgu CV配置 ; 在C#下使用OpenCV ; C#中使用OpenCV(Emgu CV) - yimingsilence的专栏 - CSDN博客
http://blog.csdn.net/yimingsilence/article/details/49587641
【2】VS2010平台下的OpenCV、EmguCV( C#)安装、使用配置 - Mr. Liu - 博客园
http://www.cnblogs.com/idaidai/archive/2012/09/02/VS2010-OpenCV-EmguCV.html
【3】C#操作摄像头 实现拍照功能 - CSDN博客
http://blog.csdn.net/xblywl/article/details/51792951
【4】C#调用 opencv cv::Mat 图像, 采用折中方法 - CSDN博客
http://blog.csdn.net/yeyang911/article/details/51158621