简体   繁体   English

Opencv 2.4.2代码解释 - 面部识别

[英]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. 我已经参考了OpenCV提供的文档来制作人脸识别程序,它识别多个面孔并且正常工作。 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 ie the program is given in the link 文档即程序在链接中给出

http://docs.opencv.org/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html 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. 正如文档所述,输出是Vector of rectangles where each rectangle contains the detected object.

So one rectangle is comprised of [ initial x, initial y, width, height ] . 因此,一个矩形由[ initial x, initial y, width, height ] So you can find its center by ( x + width*0.5 , y + height*0.5 ) . 所以你可以通过( 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. 如果要绘制矩形,请使用rectangle功能。 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. 顺便说一句,OpenCV 2.4.2包括面部识别。 Here is a tutorial and full source code sample for combining face detection (with cv::CascadeClassifier) and face recognition (with cv::FaceRecognizer): 这是一个教程和完整的源代码示例,用于组合面部检测(使用cv :: CascadeClassifier)和面部识别(使用cv :: FaceRecognizer):

Since you asked for face recognition. 既然你要求面部识别。 It also shows how to do face detection, so it may be interesting as well. 它还展示了如何进行人脸检测,因此它也可能很有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM