标签:程序 logs 对象 ctr use new art typeof null
本示例主要测试了directshow、Emgucv的视频流采集功能,其中Emgucv还实现了人脸的识别。示例源码下载
实现原理:directshow可以将摄像头的数据流以事件的方式实时传递给程序,程序在此事件中拿到流后可以保存为图片(流可以经过多个filterGraph2对象)。directshow提供将数据绑定到picturebox控件上。以下为打开一个摄像头的步骤:
此控件可以在网上搜索,即好下载,名称为DirectShowLib-2005,它是directshow的.net版本的封装.
代码如下:
int hr = 0; this.filterGraph2 = (IFilterGraph2)new FilterGraph(); this.captureGraphBuilder2 = (ICaptureGraphBuilder2)new CaptureGraphBuilder2(); this.mediaControl = (IMediaControl)this.filterGraph2; this.videoWindow = (IVideoWindow)this.filterGraph2; DsError.ThrowExceptionForHR(hr);
说明:
int hr = 0; IEnumMoniker classEnum = null; IMoniker[] moniker = new IMoniker[1]; object source = null; ICreateDevEnum devEnum = (ICreateDevEnum)new CreateDevEnum(); hr = devEnum.CreateClassEnumerator(FilterCategory.VideoInputDevice, out classEnum, 0); DsError.ThrowExceptionForHR(hr); Marshal.ReleaseComObject(devEnum); if (classEnum == null) throw new Exception("classEnum is null"); if (classEnum.Next(moniker.Length, moniker, IntPtr.Zero) == 0) { Guid iid = typeof(IBaseFilter).GUID; moniker[0].BindToObject(null, null, ref iid, out source); //moniker绑定以 ibasefilter和object上 } Marshal.ReleaseComObject(moniker[0]); Marshal.ReleaseComObject(classEnum); return (IBaseFilter)source;
找到视频摄像头,将转换为一个IBaseFilter对象。
将IBaseFilter对象加入到filterGraph2对象上,作为第一层视频的过滤,后面会放到picturebox控件上。然后再加入SampleGrabber对象,用于捕获每一帧的视频数据。
此步代码较多,就不在贴代码,后面给出源码下载地址.
int hr = 0; hr = this.videoWindow.put_Owner(this.pictureBox.Handle); DsError.ThrowExceptionForHR(hr); hr = this.videoWindow.put_WindowStyle(WindowStyle.Child); DsError.ThrowExceptionForHR(hr); hr = this.videoWindow.put_Visible(OABool.True); DsError.ThrowExceptionForHR(hr); System.Drawing.Rectangle rc = this.pictureBox.ClientRectangle; hr = this.videoWindow.SetWindowPosition(0, 0, 640, 480); DsError.ThrowExceptionForHR(hr);
emgucv是opencv的.net版本封装,并且opencv获取视频流的方式也是采用的directshow。由于进行了封装,打开和识别人像就极为简单了。
至此,还需复制相关的dll到开发项目中:
private VideoCapture capture; private Mat mat; private Common.DetectFace detectFace; public Form1() { InitializeComponent(); CvInvoke.UseOpenCL = false; detectFace = new Common.DetectFace(); } private void Form1_Load(object sender, EventArgs e) { capture = new Emgu.CV.VideoCapture(); capture.ImageGrabbed += Capture_ImageGrabbed; capture.Start(); } private void Capture_ImageGrabbed(object sender, EventArgs e) { if (capture != null && capture.Ptr != IntPtr.Zero) { mat = new Mat(); capture.Retrieve(mat, 0); var ls = detectFace.Detect(mat); foreach(var face in ls) { CvInvoke.Rectangle(mat, face, new Bgr(Color.Red).MCvScalar, 2); } imageBox1.Image = mat; } }
说明:
标签:程序 logs 对象 ctr use new art typeof null
原文地址:http://www.cnblogs.com/cqhaibin/p/7225177.html