C#+Emgu实现人脸检测

Emgu CV下载地址

http://sourceforge.net/projects/emgucv/files/

找最新的下就行了,傻瓜式安装,选择目录后自动完成安装。

 Emgu CV是什么?

Emgu CV是.NET平台下对OpenCV图像处理库的封装,也就是.NET版。可以运行在C#、VB、VC++等。


配置:

1.添加环境变量C:\Emgu\emgucv-windows-x86-gpu 2.4.2.1777\bin

2. 首先需要导入UI插件。

工具——选择工具箱选项

在浏览中定位到Emgu的安装目录bin下,选择Emgu.CV.UI.dll

3.在引用中添加dll调用,分别是Emgu.CV.dll和Emgu.CV.ML.dll和Emgu.CV.UI.dll和Emgu.Util.dll以及ZedGraph.dll

4.C:\Emgu\emgucv-windows-x86-gpu 2.4.2.1777\bin\x86目录下的所有dll文件拷贝到工程目录的debug

写代码:

在imageprocess的基础上做修改

1.读入图片:

Image<Bgr, byte>image;

image = new Image<Bgr,byte>(curFileName); 

private void open_Click(object sender, EventArgse)

        {

            OpenFileDialogopnDlg =newOpenFileDialog();

            opnDlg.Filter = "所有图像文件 | *.bmp; *.pcx; *.png; *.jpg;*.gif;" +

                "*.tif;*.ico; *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf|" +

                "位图( *.bmp; *.jpg; *.png;...) | *.bmp;*.pcx; *.png; *.jpg; *.gif; *.tif; *.ico|" +

                "矢量图( *.wmf; *.eps; *.emf;...) | *.dxf;*.cgm; *.cdr; *.wmf; *.eps; *.emf";

            opnDlg.Title = "打开图像文件";

            opnDlg.ShowHelp = true;

            if(opnDlg.ShowDialog() ==DialogResult.OK)

            {

                imgFormsForm =newimgForm();

                sForm.MdiParent = this;

                stringcurFileName = opnDlg.FileName;

                sForm.CurFileName =curFileName;

                Image<Bgr,byte> image;

                try

                {

                   image = new Image<Bgr,byte>(curFileName);

                    sForm.CurImage = image;

                    //设置窗口尺寸

                    sForm.ClientSize = new System.Drawing.Size(image.Width,image.Height);

                    //设置窗口标题

                    sForm.Text = curFileName;

                    sForm.Name = curFileName;

                }

                catch(Exception exp)

                {

                    MessageBox.Show(exp.Message);

                }

                //sForm.ShowDialog();

                sForm.Show();

                sForm.Invalidate();             

            }

            if(this.MdiChildren.Length > 0)

            {

                this.save.Enabled=true;

              

            }          

        }

2.显示图片:

private voidimgForm_Paint(object sender, PaintEventArgs e)

        {

            if(image != null)

            {

                imageBox1.Width = image.Width;

                imageBox1.Height =image.Height;

                imageBox1.Image = image;            

 

            }

        }

3.人脸检测:

        privateImage<Bgr,byte> image;

        privatestring haarXmlPath ="E:\\FrontalFace10\\haarcascade_frontalface_alt_tree.xml";

        privateCascadeClassifier haar =null;

        List<PictureBox> faceHistory =newList<PictureBox>();

  private void initiateHaar(stringfileName)

        {

            if(haar != null)

                return;

            showMessage("Loading HaarCascade data...");

            haar = newHaarCascade(fileName);

            showMessage("HaarCascadedata loaded.");

        }

        // analyzeone frame

        privateMCvAvgComp[] getFaces(Image<Bgr,byte> img,HaarCascadehaar)

        {

            if(haar == null || img == null)

                returnnull;

 

            MCvAvgComp[]faces = haar.Detect(img.Convert<Gray,byte>(), 1.4, 1, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,newSize(20, 20));

            if(faces != null)

            {

                resultText.Text = string.Format("Found{0} face(s).", faces.Length);

            }

 

            returnfaces;

        }

 

        // mark faces

        privatevoid markFaces(MCvAvgComp[]faces)

        {

            foreach(MCvAvgComp faceinfaces)

            {

                currentImage.Draw(face.rect, new Bgr(Color.Lime), 2);

                PictureBoxpic =newPictureBox();

                pic.Image =currentImage.Copy(face.rect).ToBitmap();

                pic.Width = pic.Image.Width;

                pic.Height = pic.Image.Height;

                faceHistory.Add(pic);

            }

            imageBox.Image = currentImage;         


        }


你可能感兴趣的:(image,image,C#,C#)