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?
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 :
By the way, OpenCV 2.4.2 has face recognition included. Here is a tutorial and full source code sample for combining face detection (with cv::CascadeClassifier) and face recognition (with cv::FaceRecognizer):
Since you asked for face recognition. It also shows how to do face detection, so it may be interesting as well.
来源:https://stackoverflow.com/questions/11355738/opencv-2-4-2-code-explanation-face-recognition