Search code examples
c++opencvfeature-detectionhaar-classifier

Rectangle not around detected eye pair


I am using opencv 2.4.2 and c++. I have a minor problem regarding the rectangle which detects eye pair. The rectangle does not appear around the eye pair but outside the detected face rectangle. I think I may not be getting the parameters right.

Here is the piece of code

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

        rectangle(frame,faces[i],Scalar(255,0,0),1,8,0);

        Mat face  = frame(faces[i]);
        cvtColor(face,face,CV_BGR2GRAY);
        imwrite("C:/Users/DELL/Documents/Visual Studio 2010/Projects/Haarcascade/Haarcascade/fot.jpg",face);
        vector<Rect> eyes;
        eye.detectMultiScale(face,eyes);

        for( size_t j = 0; j < eyes.size(); j++ ){

            rectangle(frame,eyes[i],Scalar(255,0,0),4,8,0);

        }

}

Can anyone help please?thanks


Solution

  • You are looking for eyes in range from 0 to face->width and from 0 to face->height, so you get eyes coordinates relatively to face boundaries, and then you draw eyes on original frame. You need to add coordinates of face in frame, something like that :

    Rect r(faces[i].x + eyes[i].x, faces[i].y + eyes[i].y, eyes[i].width,eyes[i].height );
    rectangle(frame,r,Scalar(255,0,0),4,8,0);