Opencv 2.4.2 Code Explanation - Face Recognition

I handed over the documentation provided by OpenCV to make a face recognition program, it recognizes several faces and works fine. In the documentation, they made ellipses to emphasize the face. I do not understand how they calculated the center of the ellipse, which they 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 vector of faces that they use is created as follows

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

Documentation, i.e. the program is listed in the link

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

I want to know how they calculate the center of an ellipse, and if I want to draw a rectangle instead of a circle, what should I do?

+6
source share
2 answers

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

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

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

The arguments to the function will be as follows:

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

Change the line drawing ellipse to the 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

+13
source

By the way, OpenCV 2.4.2 includes face recognition. Here is a sample tutorial and full source code for combining face recognition (with cv :: CascadeClassifier) ​​and face recognition (using cv :: FaceRecognizer):

Since you asked to recognize the face. It also shows how to perform face recognition, so it can be interesting.

+4
source

Source: https://habr.com/ru/post/919814/


All Articles