Integrate to examples to generate face detection in continous images as follows:
1. Create a Windowform application
2. Add reference
3. Add using namespace
//--------
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.UI;
4. Add a form load event method
private Capture _capture;
private bool _captureInProgress;
ImageBox box = new ImageBox();
private void Form1_Load(object sender, EventArgs e)
{
box.Width = 800;
box.Height = 600;
this.Controls.Add(box);
//Application.Idle += new EventHandler(Application_Idle);
#region if capture is not created, create it now
if (_capture == null)
{
try
{
_capture = new Capture();
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
#endregion
if (_capture != null)
{
if (_captureInProgress)
{ //stop the capture
Application.Idle -= Application_Idle;
}
else
{
//start the capture
//captureButton.Text = "Stop";
Application.Idle += Application_Idle;
}
_captureInProgress = !_captureInProgress;
}
}
void Application_Idle(object sender, EventArgs e)
{
Image<Bgr, Byte> frame = _capture.QueryFrame();
box.Image = frame;
//Image<Bgr, Byte> image = new Image<Bgr, byte>("lena.jpg"); //Read the files as an 8-bit Bgr image
Image<Gray, Byte> gray = frame.Convert<Gray, Byte>(); //Convert it to Grayscale
//normalizes brightness and increases contrast of the image
gray._EqualizeHist();
//Read the HaarCascade objects
HaarCascade face = new HaarCascade("../../haarcascade_frontalface_alt_tree.xml");
HaarCascade eye = new HaarCascade("../../haarcascade_eye.xml");
//Detect the faces from the gray scale image and store the locations as rectangle
//The first dimensional is the channel
//The second dimension is the index of the rectangle in the specific channel
MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(face, 1.1, 1, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
foreach (MCvAvgComp f in facesDetected[0])
{
//Set the region of interest on the faces
gray.ROI = f.rect;
MCvAvgComp[][] eyesDetected = gray.DetectHaarCascade(eye, 1.1, 1, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
gray.ROI = Rectangle.Empty;
//if there is no eye in the specific region, the region shouldn't contains a face
//note that we might not be able to recoginize a person who ware glass in this case
if (eyesDetected[0].Length == 0) continue;
//draw the face detected in the 0th (gray) channel with blue color
frame.Draw(f.rect, new Bgr(Color.Blue), 2);
foreach (MCvAvgComp ey in eyesDetected[0])
{
if (ey.neighbors > 100)
{
Rectangle eyeRect = ey.rect;
eyeRect.Offset(f.rect.X, f.rect.Y);
frame.Draw(eyeRect, new Bgr(Color.Red), 2);
}
}
}
//display the image
box.Image = frame;
}
沒有留言:
張貼留言