using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
namespace CameraCapture
{
public partial class CameraCapture : Form
{
Image<Bgr, Byte> image;
private Capture _capture;
private bool _captureInProgress;
public CameraCapture()
{
InitializeComponent();
}
private void ProcessFrame(object sender, EventArgs arg)
{
image = _capture.QueryFrame();
Run();
captureImageBox.Image = image;
}
public void Run()
{
Image<Gray, Byte> gray = image.Convert<Gray, Byte>();
gray._EqualizeHist();
HaarCascade face = new HaarCascade("haarcascade_frontalface_alt_tree.xml");
MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
face,
1.1,
10,
Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size(20, 20));
foreach (MCvAvgComp f in facesDetected[0])
{
image.Draw(f.rect, new Bgr(Color.Blue), 2);
}
}
private void captureButtonClick(object sender, EventArgs e)
{
#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)
{
captureButton.Text = "Start Capture";
Application.Idle -= ProcessFrame;
}
else
{
captureButton.Text = "Stop";
Application.Idle += ProcessFrame;
}
_captureInProgress = !_captureInProgress;
}
}
private void ReleaseData()
{
if (_capture != null)
_capture.Dispose();
}
}
}