这是一个简单的人脸识别程序

知识点说明:string 字符串类型

官方训练好的模型  haarcascade_frontalface_alt.xml

 

绘制矩形框

C:       void cvRectangle(CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int line_type=8, int shift=0 )

C++: void rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )
参数介绍:

img 图像. pt1 矩形的一个顶点。 pt2 矩形对角线上的另一个顶点 color 线条颜色 (RGB) 或亮度(灰度图像 )(grayscale image)。 thickness 组成矩形的线条的粗细程度。取负值时(如 CV_FILLED)函数绘制填充了色彩的矩形。 line_type 线条的类型。见cvLine的描述 shift 坐标点的小数点位数。

绘制椭圆圆弧和椭圆扇形。

void cvEllipse( CvArr* img, CvPoint center, CvSize axes, double angle,
double start_angle, double end_angle, CvScalar color,
int thickness=1, int line_type=8, int shift=0 );
C++: void ellipse(Mat&img, Pointcenter, Size axes, doubleangle, doublestartAngle, double endAngle, const Scalar&color, intthickness=1, int lineType=8, intshift=0)

C++: void ellipse(Mat&img, const RotatedRect&box, const Scalar& color, int thickness=1, intlineType=8)

img图像。center椭圆圆心坐标。axes轴的长度。angle偏转的角度。start_angle圆弧起始角的角度。.end_angle圆弧终结角的角度。color线条的颜色。thickness线条的粗细程度。line_type线条的类型,见CVLINE的描述。shift圆心坐标点和数轴的精度。

rectangle

 

 1 #include "opencv2/core/core.hpp"
 2 #include "opencv2/objdetect/objdetect.hpp" 
 3 #include "opencv2/highgui/highgui.hpp" 
 4 #include "opencv2/imgproc/imgproc.hpp" 
 5 
 6 #include <iostream> 
 7 #include <stdio.h> 
 8 
 9 using namespace std;
10 using namespace cv;
11 string face_cascade_name = "D:\\haarcascade_frontalface_alt.xml";
12 //该文件存在于OpenCV安装目录下的\sources\data\haarcascades内,需要将该xml文件复制到当前工程目录下
13 CascadeClassifier face_cascade;
14 void detectAndDisplay(Mat frame);
15 int main(int argc, char** argv) {
16     Mat image;
17     image = imread("D:/P.png", 1);  //当前工程的image目录下的mm.jpg文件,注意目录符号
18     if (!face_cascade.load(face_cascade_name)) {
19         printf("级联分类器错误,可能未找到文件,拷贝该文件到工程目录下!\n");
20         return -1;
21     }
22     detectAndDisplay(image); //调用人脸检测函数
23     waitKey(0);
24     //暂停显示一下。
25 }
26 
27 void detectAndDisplay(Mat face) {
28     std::vector<Rect> faces;
29     Mat face_gray;
30     cvtColor(face, face_gray, CV_BGR2GRAY);  //rgb类型转换为灰度类型
31     equalizeHist(face_gray, face_gray);   //直方图均衡化
32 
33     face_cascade.detectMultiScale(face_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(1, 1));
34 
35     //用椭圆框画出
36     for (int i = 0; i < faces.size(); i++){
37         Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
38         ellipse(face, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 0), 2, 7, 0);
39     }
40     //用矩形框画出
41     /*for (int i = 0; i < faces.size(); ++i)
42     {
43         rectangle(face, Rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height), Scalar(0, 0, 255), 3);
44     }*/
45     namedWindow("人脸检测", CV_WINDOW_NORMAL);
46     imshow("人脸检测", face);
47 }

动态处理

 1 #include "opencv2/core/core.hpp"
 2 #include "opencv2/objdetect/objdetect.hpp" 
 3 #include "opencv2/highgui/highgui.hpp" 
 4 #include "opencv2/imgproc/imgproc.hpp" 
 5 
 6 #include <iostream> 
 7 #include <stdio.h> 
 8 
 9 #define WINDOW "人脸检测"
10 using namespace std;
11 using namespace cv;
12 VideoCapture capture(0);
13 string face_cascade_name = "D:\\haarcascade_frontalface_alt.xml";
14 //该文件存在于OpenCV安装目录下的\sources\data\haarcascades内,需要将该xml文件复制到当前工程目录下
15 CascadeClassifier face_cascade;
16 void detectAndDisplay(Mat frame);
17 int main(int argc, char** argv) {
18     Mat image,frame;    
19     //image = imread("D:/P.png", 1);  //当前工程的image目录下的mm.jpg文件,注意目录符号
20     if (!face_cascade.load(face_cascade_name)) {
21         printf("级联分类器错误,可能未找到文件,拷贝该文件到工程目录下!\n");
22         return -1;
23     }
24     while (capture.read(frame)) {
25         detectAndDisplay(frame); //调用人脸检测函数
26         waitKey(30);
27         //暂停显示一下。
28     }
29 }
30 
31 void detectAndDisplay(Mat face) {
32     std::vector<Rect> faces;
33     Mat face_gray;
34     cvtColor(face, face_gray, CV_BGR2GRAY);  //rgb类型转换为灰度类型
35     equalizeHist(face_gray, face_gray);   //直方图均衡化
36 
37     face_cascade.detectMultiScale(face_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(1, 1));
38 
39     //用椭圆框画出
40     for (int i = 0; i < faces.size(); i++){
41         Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
42         ellipse(face, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 0), 2, 7, 0);
43     }
44     //用矩形框画出
45     /*for (int i = 0; i < faces.size(); ++i)
46     {
47         rectangle(face, Rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height), Scalar(0, 0, 255), 3);
48     }*/
49     namedWindow(WINDOW, CV_WINDOW_NORMAL);
50     imshow(WINDOW, face);
51 }

 

 

版权声明:本文为fanxiaohao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/fanxiaohao/p/11991549.html