c# 人脸检测(典型方法)

来源:互联网 发布:2016手机推荐 知乎 编辑:程序博客网 时间:2024/06/07 03:54
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using System.IO;
using System.Xml;


namespace projectname
{
    public partial classForm1 : Form
    {
       int abc = 0;

       public Form1()
       {
          InitializeComponent();
       }

       private Capture _capture;

       private bool _captureInProgress;


       private void ProcessFrame(object sender,EventArgs arg)
       {


          Image<Bgr, Byte> frame =_capture.QueryFrame();      
           frame =frame.Flip(Emgu.CV.CvEnum.FLIP.HORIZONTAL);
          Image<Gray, Byte> gray =frame.Convert<Gray, Byte>();//Convert it to Grayscale
          gray._EqualizeHist();
          HaarCascade face = newHaarCascade("haarcascade_frontalface_alt2.xml");
          MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(face, 1.1, 2,Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(30,30));

           foreach(MCvAvgComp f in facesDetected[0])
           {
              frame.Draw(f.rect, newBgr(Color.Red), 3);
           }

          imageBox1.Image = frame;

       }


       private void buttonOpenCapture_Click(objectsender, EventArgs e)
       {

           #region ifcapture is not created, create it now
           if(_capture == null)
           {
              try
              {
                 _capture = new Capture(0);

              }
              catch (NullReferenceExceptionexcpt)
              {
                 MessageBox.Show(excpt.Message);
              }
           }



          #endregion

           if(_capture != null)
           {
              if (_captureInProgress)
              {  //stopthe capture
                 buttonOpenCapture.Text = "打开视频";
                 Application.Idle -= ProcessFrame;
              }
              else
              {
                 //start the capture
                 buttonOpenCapture.Text = "关闭视频";
                 Application.Idle += ProcessFrame;
              }

              _captureInProgress =!_captureInProgress;
           }
       }






    }
}

0 0
原创粉丝点击