人脸跟踪检测代码注释

来源:互联网 发布:软件项目管理实用教程 编辑:程序博客网 时间:2024/05/16 12:30

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.Util;



using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
namespace FaceDecetion
{
    public partial class Form1 : Form
    {
        private Capture cap;//操作视频流的对象
        private HaarCascade haar;//用于目标检测
        public Form1()
        {  
            InitializeComponent();
        }
        //利用时钟控件的Tick事件代替while
        private void timer1_Tick(object sender, EventArgs e)
        {
            //每次获取一帧图片
            using (Image<Bgr, byte> nextFrame = cap.QueryFrame())
            {
                if (nextFrame != null)
                {
                    // there's only one channel (greyscale), hence the zero index
                    //var faces = nextFrame.DetectHaarCascade(haar)[0];
                    //Convert the current image to the specific color and depth   Bgr to gray
                    Image<Gray, byte> grayframe = nextFrame.Convert<Gray, byte>();//
                    var faces = haar.Detect(grayframe);//处理效果好,有点滞延??
                        /*
                            grayframe.DetectHaarCascade(
                                    haar, 1.4, 4,
                                    HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                    new Size(nextFrame.Width / 8, nextFrame.Height / 8)
                                    )[0];
                       */
                    foreach (var face in faces)
                    {
                        nextFrame.Draw(face.rect, new Bgr(0, double.MaxValue, 0), 3);
                    }
                    pictureBox1.Image = nextFrame.ToBitmap();
                }
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // passing 0 gets zeroth webcam
            cap = new Capture(0);
            // adjust path to find your xml
            haar = new HaarCascade(
            "C:\\Emgu\\emgucv-windows-x86 2.3.0.1416\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt.xml");

        }
    }
}

原创粉丝点击