您的位置:首页 > 编程语言 > C#

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

2014-09-25 13:29 232 查看
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 class
Form1 : 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 = new
HaarCascade("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, new
Bgr(Color.Red), 3);

}

imageBox1.Image = frame;

}

private void buttonOpenCapture_Click(object
sender, EventArgs e)

{

#region if
capture is not created, create it now

if
(_capture == null)

{

try

{

_capture = new Capture(0);

}

catch (NullReferenceException
excpt)

{

MessageBox.Show(excpt.Message);

}

}

#endregion

if
(_capture != null)

{

if (_captureInProgress)

{ //stop
the capture

buttonOpenCapture.Text = "打开视频";

Application.Idle -= ProcessFrame;

}

else

{

//start the capture

buttonOpenCapture.Text = "关闭视频";

Application.Idle += ProcessFrame;

}

_captureInProgress =
!_captureInProgress;

}

}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: