1. 窗体增加Timer,PictureBox控件。设定Timer1的Enable=True
2. 参加引用
using Emgu.CV; using Emgu.CV.Structure; using Emgu.CV.CvEnum; using Emgu.Util;
3. 添加如下相关代码
private Capture cap; private HaarCascade haar; private void timer1_Tick(object sender, EventArgs e) { using (Image<Bgr, byte> nextFrame = cap.QueryFrame()) { if (nextFrame != null) { Image<Gray, byte> grayframe = nextFrame.Convert<Gray, byte>(); var faces = haar.Detect(grayframe, 1.4, 4, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(nextFrame.Width / 8, nextFrame.Height / 8)); 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) { cap = new Capture(0); haar = new HaarCascade("C:\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt2.xml"); }
4. new HaarCascade里的xml根据自己电脑的实际目录修改。
5. data\haarcascades\haarcascade*.xml说明
5.1. haarcascade_frontalface_*.xml识别脸部
5.2. haarcascade_eye*.xml眼睛识别
5.3. haarcascade_mcs_mouth.xml嘴巴识别
5.4. haarcascade_mcs_nose.xml鼻子识别
5.5. 其他xml也可测试看看,蛮好玩的。