使用haar-cascades从脸部检测眼睛和嘴巴

时间:2013-01-12 15:08:50

标签: c# .net winforms emgucv

我从脸上提取了眼睛和嘴巴,但是想要从眼睛和嘴巴中提取情感。但是,嘴巴没有被正确检测到.. 这是我的代码..

private void timer1_Tick(object sender, EventArgs e)
{
    using (Image<Bgr, byte> nextFrame = cap.QueryFrame())
    {
        if (nextFrame != null)
        {
            // there's only one channel (greyscale), hence the zero index
            //var faces = nextFrame.DetectHaarCascade(haar)[0];
            Image<Gray, byte> grayframe = nextFrame.Convert<Gray, byte>();
            Image<Gray, Byte> gray = nextFrame.Convert<Gray, Byte>();
            Image<Gray, Byte> gray1 = nextFrame.Convert<Gray, Byte>();

            var faces = grayframe.DetectHaarCascade(
                            haar, 1.4, 4,
                            HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                            new Size(nextFrame.Width / 8, nextFrame.Height / 8)
                            )[0];

            MCvAvgComp[][] eyes = gray.DetectHaarCascade(eye, 1.1, 1, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
            gray.ROI = Rectangle.Empty;

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

            foreach (MCvAvgComp mouthsnap in mouthsDetected[0])
            {
                Rectangle mouthRect = mouthsnap.rect;
                // mouthRect.Offset(f.rect.X, f.rect.Y);
                nextFrame.Draw(mouthRect, new Bgr(Color.Red), 2);
                detectedmouth = mouthRect;

            }
            foreach (MCvAvgComp eyesnap in eyes[0])
            {
                Rectangle eyeRect = eyesnap.rect;
                // mouthRect.Offset(f.rect.X, f.rect.Y);
                nextFrame.Draw(eyeRect, new Bgr(Color.Green), 2);
            }
            foreach (var face in faces)
            {
                nextFrame.Draw(face.rect, new Bgr(Color.LightGreen), 3);
                facesnap = face.rect;
            }

            pictureBox1.Image = nextFrame.ToBitmap();
        }
    }

}

private void Form1_Load(object sender, EventArgs e)
{
    cap = new Capture(0);
    // adjust path to find your xml
    //haar = new HaarCascade("haarcascade_frontalface_alt2.xml");
    haar = new HaarCascade("haarcascade_frontalface_alt_tree.xml");
    mouth = new HaarCascade("Mouth.xml");

    eye = new HaarCascade("haarcascade_eye_tree_eyeglasses.xml");
}

private void button1_Click(object sender, EventArgs e)
{
    Image snap = pictureBox1.Image;

    snap.Save("c:\\snapshot.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

    pictureBox2.Image = snap;
    pictureBox3.Image = cropImage(snap,facesnap);
    pictureBox4.Image = cropImage(snap, detectedmouth);

}

private static Image cropImage(Image img, Rectangle croparea)
{
    Bitmap bmpImage = new Bitmap(img);
    Bitmap bmpCrop = bmpImage.Clone(croparea, bmpImage.PixelFormat);
    return (Image)(bmpCrop);
}

请使用c#帮助我进行情绪检测和更好的口腔检测。

2 个答案:

答案 0 :(得分:3)

我会尝试在脸部矩形中寻找一个嘴,而不是检查孔图片。

var faces = grayframe.DetectHaarCascade(
                            haar, 1.4, 4,
                            HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                            new Size(nextFrame.Width / 8, nextFrame.Height / 8)
                            )[0];
 foreach (var f in faces)
 {
    //draw the face detected in the 0th (gray) channel with blue color
    image.Draw(f.rect, new Bgr(Color.Blue), 2);


     //Set the region of interest on the faces
     gray.ROI = f.rect;
     var 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 (var m in mouthsDetected [0])
     {
          Rectangle mouthRect = m.rect;
          mouthRect.Offset(f.rect.X, f.rect.Y);
          image.Draw(mouthRect , new Bgr(Color.Red), 2);
     }
   }

答案 1 :(得分:0)

我将面部区域分为2个矩形顶部和底部......并将底部矩形应用于灰色.ROI。它的工作原理..这是两个矩形的代码..

 int halfheight = facesnap.Height/2;
                    int start = facesnap.X;
                    int start1 = facesnap.Y;

                    Rectangle top = new Rectangle(start,start1,facesnap.Width,halfheight);
                    int start2 = top.Bottom;

                    Rectangle bottom = new Rectangle(start, start2, facesnap.Width, halfheight);
                    nextFrame.Draw(bottom, new Bgr(Color.Yellow), 2);
                    //Set the region of interest on the faces
                    gray.ROI = bottom;
                    MCvAvgComp[][] mouthsDetected = gray.DetectHaarCascade(mouth,
                                                    1.1, 10,
                                                  Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                                    new Size(20, 20));