Ask Your Question
0

Save detected eyes in form of images

asked 2016-01-12 22:25:56 -0600

Sanman gravatar image

updated 2016-01-14 02:23:52 -0600

tomnjerry gravatar image

I have been working on eye detection and I want to detect and then extract the eyes from video feed at specific interval. I want to store the detected eyes in form of images. I have done the detection of eyes using haar cascade. Now I just want to store the detected eyes in form of images. Can anyone tell me what I can do to for the problem ? The detection code is as follows


int main()
{
CascadeClassifier face, eye;
if(!face.load("C:\\HAAR\\haarcascade_frontalcatface.xml")){

    printf("Error Loading Face Cascade");
        return -1;
}

    if(!eye.load("C:\\HAAR\\haarcascade_eye_tree_eyeglasses.xml")){

    printf("Error Loading Eye Cascade");
        return -1;
} 
    VideoCapture capture(0);
    if(!capture.isOpened())
    {

        printf("Error opening Video Stream");
        return -1;
    }

    Mat capimg,greyimg;
    vector<Rect> faces,eyes;

    while(1)
    {

        capture>>capimg;
        waitKey(10);            
            cvtColor(capimg, greyimg, CV_BGR2GRAY);
            cv::equalizeHist(greyimg,greyimg);
        face.detectMultiScale(greyimg, faces, 1.1, 10, CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING, cvSize(0,0), cvSize(300,300));

        for(int i=0; i < faces.size(); i++)
        {

            Point pt1(faces[i].x+faces[i].width,faces[i].y+faces[i].height);
            Point pt2(faces[i].x,faces[i].y);
            Mat faceroi=greyimg(faces[i]);
            eye.detectMultiScale(faceroi, eyes,1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30,30));
            for(size_t j=0; j<eyes.size(); j++)
            {

                Point center(faces[i].x+eyes[j].x+eyes[j].width*0.5,faces[i].y+eyes[j].y+eyes[j].height*0.5);
                int radius = cvRound((eyes[j].width+eyes[j].height)*0.25);
                circle(capimg, center, radius, Scalar(255,0,0),2,8,0);
            }
            rectangle(capimg, pt1, pt2, cvScalar(0,255,0),2,8,0);
        }
        imshow("Result",capimg);
        waitKey(3);
        char c= waitKey(3);
        if (c==27)
        break;

    }
return 0;

}


edit retag flag offensive close merge delete

Comments

Sorry for the formatting of the question. I am new here . . .

Sanman gravatar imageSanman ( 2016-01-12 22:38:06 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-01-13 02:55:49 -0600

tomnjerry gravatar image

updated 2016-01-14 02:24:45 -0600

You are using 'haarcascades_eye_tree_eyeglasses.xml' which returns information of individual eyes. SInce, images cannot be round, you cannot save the individual eyes in circular form. But, you can extract the individual eyes as you did for extracting face. For this you can use eyes[j].x , eyes[j].y, eyes[j].width and eyes[j].height to extract rectangular part of eyes. Once you have the extracted part of eyes, you can use imwrite() to save it using the name you would like to assign as shown below

char filename[120];
while(1)
{
    capture>>capimg;
cvtColor(capimg, greyimg, CV_BGR2GRAY);
cv::equalizeHist(greyimg,greyimg);
face.detectMultiScale(greyimg, faces, 1.1, 10, CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING, cvSize(0,0), cvSize(300,300));
for(int i=0; i < faces.size(); i++)
{
    Point pt1(faces[i].x+faces[i].width,faces[i].y+faces[i].height);
    Point pt2(faces[i].x,faces[i].y);
    Mat faceroi=greyimg(faces[i]);
    imshow("faceRegion",faceroi);
    waitKey(10000);
    eye.detectMultiScale(faceroi, eyes,1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30,30));
    cout<<eyes.size()<<endl;
    for(size_t j=0; j<eyes.size(); j++)
    {
        Mat eyeRoi=faceroi(eyes[j]);
        sprintf(filename,"eye_%d.jpg",j);  
        bool write_success = imwrite(filename, eyeRoi);
    }
    rectangle(capimg, pt1, pt2, cvScalar(0,255,0),2,8,0);
}
imshow("Result",capimg);
waitKey(1000);
return 0;}

Alternatively, you can also use haarcascade_mcs_eyepair_big_xml to extract information about both eyes together.

edit flag offensive delete link more

Comments

Thank you for the response. I will try the solution.

Sanman gravatar imageSanman ( 2016-01-15 03:49:06 -0600 )edit

The solution Works.

Sanman gravatar imageSanman ( 2016-01-27 01:36:05 -0600 )edit
1

In that case, mark the answer as correct answer so that the topic is closed and correct answer is visible to others too.

tomnjerry gravatar imagetomnjerry ( 2016-02-01 04:38:41 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-01-12 22:25:56 -0600

Seen: 486 times

Last updated: Jan 14 '16