Emgu CV微笑检测

时间:2016-04-20 15:14:11

标签: c# emgucv

我正在做一个项目,当你微笑时拍照,但我没有做微笑检测本身

(我的项目是在你微笑的时候拍照)

如何进行微笑检测,以便拍摄照片?

这是我的项目源代码:

public partial class Form1 : Form
{
    private Capture capture;        
    private bool captureInProgress;
    private HaarCascade haar;
    private HaarCascade mouth;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        haar = new HaarCascade("haarcascade_frontalface_alt_tree.xml");
        mouth = new HaarCascade("haarcascade_mcs_mouth.xml");

    }
    private void ProcessFrame(object sender, EventArgs arg)
    {
        Image<Bgr, Byte> ImageFrame = capture.QueryFrame();

        if (ImageFrame !=null)
        {
            Image<Gray, byte> grayFrame = ImageFrame.Convert<Gray, byte>();
            Image<Gray, Byte> gray = ImageFrame.Convert<Gray, Byte>();
            var faces = grayFrame.DetectHaarCascade(haar, 1.4, 4, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(25, 25))[0];
            foreach (var face in faces)
            {
                ImageFrame.Draw(face.rect, new Bgr(Color.Green), 3);

                MCvAvgComp[][] mouthsDetected = gray.DetectHaarCascade(mouth, 1.1, 10, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
                gray.ROI = Rectangle.Empty;

                foreach (MCvAvgComp e in mouthsDetected[0])
                {
                    Rectangle mouthRect = e.rect;
                    mouthRect.Offset(face.rect.X, face.rect.Y);
                    ImageFrame.Draw(mouthRect, new Bgr(Color.Red), 2);
                }
            }

        }

        CamImageBox.Image = ImageFrame;       

    }

    private void btnstart_Click(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)
            {  //if camera is getting frames then stop the capture and set button Text
                // "Start" for resuming capture
                btnStart.Text = "Başlat"; //
                Application.Idle -= ProcessFrame;
            }
            else
            {
                //if camera is NOT getting frames then start the capture and set button
                // Text to "Stop" for pausing capture
                btnStart.Text = "Durdur";
                Application.Idle += ProcessFrame;
            }

            captureInProgress = !captureInProgress;
        }
    }
    private void ReleaseData()
    {
        if (capture != null)
            capture.Dispose();
    }  
  }
}

1 个答案:

答案 0 :(得分:1)

如果您要求从处理中查看相应的图像,

Emgu CV建议使用ImageBox控件进行显示,原因如下。这可以与上面代码示例中的ImageFrame(Image)对象一起使用。

ImageBox是一种用于显示图像的高性能控件。只要有可能,它会显示一个与Image对象共享内存的Bitmap,因此不需要内存副本(非常快)。

用户将能够在显示图像时检查图像像素值,视频帧率,颜色类型。

只需点击几下鼠标即可轻松执行简单的图像操作。

转换为位图

Image类有一个 ToBitmap()函数,它返回一个Bitmap对象,可以使用Windows Form轻松地在PictureBox控件上显示。

检测嘴/微笑

您需要为嘴巴提供Haarcascade XML,并相应地进行捕获。请参阅下面的code,它可以在嘴周围画一个矩形,

CascadeClassifier mouth = new CascadeClassifier(Application.StartupPath + "/haarcascade_mcs_mouth.xml");
Image<Bgr, Byte> currentframe= null;
Image<Gray, byte> grayFrame = null;
Capture grabber = new Capture();

currentframe = grabber.QueryFrame().Resize(500, 320, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);

            if (currentframe != null)
            {
                grayFrame = currentframe.Convert<Gray, Byte>();

                Rectangle[] mouthDetected = mouth.DetectMultiScale(grayFrame, 1.1, 10, Size.Empty, Size.Empty);

                // to draw rectangle 
                foreach (Rectangle mouthFound in mouthDetected)
                {
                   ...
                }
            }