Search code examples
c#opencvemgucv

Snap Shot of Individual Eyes


I have detected eyes using this code:

MCvAvgComp[][] eyes = gray1.DetectHaarCascade(eye, 1.1, 1, 
       Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
gray1.ROI = Rectangle.Empty;
foreach (MCvAvgComp eyesnap in eyes[0])
{
    Rectangle eyeRect = eyesnap.rect;
    eyeRect.Offset(f.rect.X, f.rect.Y);
    nextFrame.Draw(eyeRect, new Bgr(Color.Green), 2);
}

I want to take snapshot of both eyes in different picturebox. Can any one help me understand how I can take a snapshot of individual eyes?


Solution

  • You can use GetSubRect function to get sub image:

    IImage eyeImg = nextFrame.GetSubRect(eyeRect);
    

    Or

    Bitmap eyeBmp = nextFrame.GetSubRect(eyeRect).Bitmap;
    

    Edit

    Load HaarCascade for left eye:

    HaarCascade leftEye = new HaarCascade("leftEye.xml");
    
    MCvAvgComp[][] foundLeftEyes = gray1.DetectHaarCascade(leftEye, 1.1, 1, 
           Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
    gray1.ROI = Rectangle.Empty;
    foreach (MCvAvgComp eyesnap in foundLeftEyes[0])
    {
        Rectangle eyeRect = eyesnap.rect;
        eyeRect.Offset(f.rect.X, f.rect.Y);
        nextFrame.Draw(eyeRect, new Bgr(Color.Green), 2);
    }
    

    Same will be for right eye...

    As an example you can use:

    Left eye HaarCascade and Right eye HaarCascade