3

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

Steph
  • 609
  • 3
  • 13
  • 32

1 Answers1

1

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);
Dabo
  • 2,371
  • 2
  • 18
  • 26
  • the rectangle is now around the eye pair :) however,the rectangle does not stop exactly at the corners of the eye pair.Is there anything that can be done? – Steph Mar 03 '14 at 09:05
  • 1
    you drawing only one rectangle around both eyes ? if so, you need to adjust width and height of the rectangle accordingly. I suggest you to try it your self, it is not complicated. If you don't manage, comment again, i will help you – Dabo Mar 03 '14 at 09:23
  • actually for some face images,it detects the proper size and for some it's too big.But anyway,it works,thanks again – Steph Mar 03 '14 at 09:41
  • 1
    If you want good eyes detection, you should make some preprocessing, actually in real world you should look for eyes only in relevant areas of the face. For better understanding i suggest you to read MasteringOpenCv chapter 8, here is the implementation https://github.com/MasteringOpenCV/code/tree/master/Chapter8_FaceRecognition – Dabo Mar 03 '14 at 09:50