title: Eye and Mouth detection from face using haar-cascades
body: <p>I have extracted eyes and mouth from the face, but want to extract emotions from eyes and mouth.. However, mouth is not detected properly..
  This is my code..</p>
  
  <pre><code>private void timer1_Tick(object sender, EventArgs e)
  {
      using (Image&lt;Bgr, byte&gt; nextFrame = cap.QueryFrame())
      {
          if (nextFrame != null)
          {
              // there's only one channel (greyscale), hence the zero index
              //var faces = nextFrame.DetectHaarCascade(haar)[0];
              Image&lt;Gray, byte&gt; grayframe = nextFrame.Convert&lt;Gray, byte&gt;();
              Image&lt;Gray, Byte&gt; gray = nextFrame.Convert&lt;Gray, Byte&gt;();
              Image&lt;Gray, Byte&gt; gray1 = nextFrame.Convert&lt;Gray, Byte&gt;();
  
              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);
  }
  </code></pre>
  
  <p>Please help me in emotion detection and better mouth detection using c#.</p>