c#下使用EmguCV操作摄像头和最简单的人脸检测

运行环境:.net 4.0, wpf 

包管理控制台安装EmguCV

        public void openCamera()
        {

            if(capture == null)
            {
                capture = new Capture(0);
            }
        }

打开摄像头方法,Capture 表示摄像头对象,Capure(0)表示打开检测到的第一个摄像头

        public ImageSource getImage()
        {
            try
            {
                Mat mat = capture.QueryFrame();
                return ImageConverterL.ToBitmapSource(mat.Bitmap);
            }
            catch(Exception)
            {
                return null;
            }
        }

获取摄像头捕获到的图像,然后再Bitmap对象转换成wpf 下可用的ImageSource对象


        public CameraInfo identifyFace()
        {
            CameraInfo info = new CameraInfo();
            using(Mat mat = capture.QueryFrame())
            {
                List faces = getFaceRectangle(mat);
                if (faces.Count <= 0)
                {
                    info.isHasFace = false;
                }
                else
                {
                    // 绘制人脸
                    using (Graphics g = Graphics.FromImage(mat.Bitmap))
                    {
                        foreach (Rectangle face in faces)
                        {
                            g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Red, 2), face);//给识别出的人脸画矩形框
                        }
                    }
                    info.isHasFace = true;             
                }
                info.mat = mat;
                info.imagesSource = ImageConverterL.ToBitmapSource(mat.Bitmap);
                return info;
            } 
        }

getFaceRectangle()是对捕获到的图像进行人脸检测,返回人脸矩形框列表,然后将矩形绘制到图像中

        private List getFaceRectangle(Mat mat)
        {
            List faces = new List();
            try
            {
                using (UMat ugray = new UMat())
                {
                    CvInvoke.CvtColor(mat, ugray, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);//灰度化图片
                    CvInvoke.EqualizeHist(ugray, ugray);//均衡化灰度图片

                    Rectangle[] facesDetected = faceClassifier.DetectMultiScale(ugray, 1.1, 10, new System.Drawing.Size(20, 20));
                    faces.AddRange(facesDetected);
                }
            }
            catch (Exception)
            {
            }
            return faces;
        }

获取人脸矩形列表

        class ImageConverterL
        {
            /// 
            /// Delete a GDI object
            /// 
            /// The poniter to the GDI object to be deleted
            /// 
            [DllImport("gdi32")]
            private static extern int DeleteObject(IntPtr o);

            public static BitmapSource ToBitmapSource(Bitmap image)
            {
                IntPtr ptr = image.GetHbitmap();
                BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ptr, IntPtr.Zero,
                                                                                               Int32Rect.Empty,
                                                                                               System.Windows.Media.Imaging.
                                                                                                      BitmapSizeOptions.
                                                                                                      FromEmptyOptions());
                DeleteObject(ptr);
                return bs;
            }
        }

转换Bitmap为ImageSource


代码 下载  


人脸对比识别还在剥离中,原项目中用到的是从身份证获取图片源
















你可能感兴趣的:(c#)