3
\$\begingroup\$
Image<Bgr, Byte> frame = _capture.QueryFrame();
Image<Gray, Byte> grayFrame = frame.Convert<Gray, Byte>();
grayFrame._EqualizeHist();
MCvAvgComp[][] facesDetected = grayFrame.DetectHaarCascade(_faces, 1.1, 1, 
    Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.FIND_BIGGEST_OBJECT, new Size(20, 20));

if (facesDetected[0].Length == 1)
{
    MCvAvgComp face = facesDetected[0][0];

    #region Search ROI based on Face Metric Estimation -

    Int32 yCoordStartSearchEyes = face.rect.Top + (face.rect.Height * 3 / 11);
    Point startingPointSearchEyes = new Point(face.rect.X, yCoordStartSearchEyes);
    Size searchEyesAreaSize = new Size(face.rect.Width, (face.rect.Height * 3 / 11));
    Rectangle possibleROI_eyes = new Rectangle(startingPointSearchEyes, searchEyesAreaSize);

    #endregion

    int widthNav = (frame.Width / 10 * 2);
    int heightNav = (frame.Height / 10 * 2);
    Rectangle nav = new Rectangle(new Point(frame.Width / 2 - widthNav / 2, frame.Height / 2 - heightNav / 2), 
        new Size(widthNav, heightNav));
    frame.Draw(nav, new Bgr(Color.Lavender), 3);
    Point cursor = new Point(face.rect.X + searchEyesAreaSize.Width / 2, 
        yCoordStartSearchEyes + searchEyesAreaSize.Height / 2);

    grayFrame.ROI = possibleROI_eyes;
    MCvAvgComp[][] eyesDetected = grayFrame.DetectHaarCascade(_eyes, 1.15, 3, 
        Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
    grayFrame.ROI = Rectangle.Empty;

    if (eyesDetected[0].Length != 0)
    {
        frame.Draw(face.rect, new Bgr(Color.Yellow), 1);

        foreach (MCvAvgComp eye in eyesDetected[0])
        {
            Rectangle eyeRect = eye.rect;
            eyeRect.Offset(possibleROI_eyes.X, possibleROI_eyes.Y);
            grayFrame.ROI = eyeRect;
            frame.Draw(eyeRect, new Bgr(Color.DarkSeaGreen), 2);

            frame.Draw(possibleROI_eyes, new Bgr(Color.DeepPink), 2);

            if (nav.Left < cursor.X && cursor.X < (nav.Left + nav.Width) 
                && nav.Top < cursor.Y && cursor.Y < nav.Top + nav.Height)
            {
                LineSegment2D CursorDraw = new LineSegment2D(cursor, new Point(cursor.X, cursor.Y + 1));
                frame.Draw(CursorDraw, new Bgr(Color.White), 3);

                int xCoord = (frame.Width * (cursor.X - nav.Left)) / nav.Width;
                int yCoord = (frame.Height * (cursor.Y - nav.Top)) / nav.Height;

                Cursor.Position = new Point(xCoord, yCoord);
            }
        }
    }
    imageBoxFrame.Image = frame;
}
\$\endgroup\$
1
  • \$\begingroup\$ You could using bit-shifting instead of division by 2 / 2^x, though the compiler may do this optimisation anyway. \$\endgroup\$ Jun 27, 2012 at 12:15

1 Answer 1

7
\$\begingroup\$

I think your code is already quite good. My guess is that the calls to DetectHaarCascade are the majority of the time. Can you run a profiler on it? The Equatec profiler is free. As for the lower loop you could preallocate your colors (although I assume they're structs). Also, I don't see where possibleROI_eyes changes, so do you really need to draw that every time through the loop? Or is it drawing at the cursor position?

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.