Search code examples
c++opencvcomputer-visionface-recognition

Opencv 2.4.2 Code Explanation-Face Recognition


I have referred the documentation provided by OpenCV to make a face recognition program, it recognizes multiple faces and is working normally. In the documentation they have made ellipses to highlight the face. What I don't understand is how they have calculated the center of the ellipse which they have calculated as follows

for( int i = 0; i < faces.size(); i++ )
{
   Point center(faces[i].x+faces[i].width*0.5,faces[i].y+faces[i].height*0.5);
   //more code follows drawing the ellipse

The faces vector that they are using is produced as follows

face_cascade.detectMultiScale(frame_gray,faces,1.1,2,0|CV_HAAR_SCALE_IMAGE,cv::Size(30,30))

The documentation i.e. the program is given in the link

http://docs.opencv.org/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html

I want to know how they are calculating the center of the ellipse and if I want to draw a rectangle instead of a circle, what do I have to do?


Solution

  • Detected faces are returned as a set of rectangles surrounding the faces. As documentation says, output is Vector of rectangles where each rectangle contains the detected object.

    So one rectangle is comprised of [ initial x, initial y, width, height ]. So you can find its center by ( x + width*0.5 , y + height*0.5 ). This center is same for the ellipse also.

    If you want to draw rectangles, use rectangle function. See the Documentation.

    Arguments in the function will be as follows :

    pt1 = ( x , y )
    
    pt2 = ( x + width , y + height )
    

    Change the line drawing ellipse to following line :

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

    It gives the result as follows :

    enter image description here