OpenCV4入门教程131:人脸检测

索引地址:系列索引

就是使用级联检测器将画面中的人脸和眼睛表示出来,不过模型使用的是官方自带的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* @file ObjectDetection.cpp
* @author A. Huaman ( based in the classic facedetect.cpp in samples/c )
* @brief A simplified version of facedetect.cpp, show how to load a cascade classifier and how to
* find objects (Face + eyes) in a video stream
*/

//---------------------------------【头文件、命名空间包含部分】----------------------------
// 描述:包含程序所使用的头文件和命名空间
//-------------------------------------------------------------------------------------------------
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

void detectAndDisplay(Mat frame);

//--------------------------------【全局变量声明】----------------------------------------------
// 描述:声明全局变量
//-------------------------------------------------------------------------------------------------
//注意,需要把"haarcascade_frontalface_alt.xml"和"haarcascade_eye_tree_eyeglasses.xml"这两个文件复制到工程路径下
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
string window_name = "Capture - Face detection";
RNG rng(12345);

//-----------------------------------【main( )函数】--------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始
//-------------------------------------------------------------------------------------------------
int main(void) {
VideoCapture capture;
Mat frame;

//-- 1. 加载级联(cascades)
if (!face_cascade.load(face_cascade_name)) {
printf("--(!)Error loading\n");
return -1;
};
if (!eyes_cascade.load(eyes_cascade_name)) {
printf("--(!)Error loading\n");
return -1;
};

//-- 2. 读取视频
capture.open(0);

if (capture.isOpened()) {
for (;;) {
capture >> frame;

//-- 3. 对当前帧使用分类器(Apply the classifier to the frame)
if (!frame.empty()) {
detectAndDisplay(frame);
} else {
printf(" --(!) No captured frame -- Break!");
break;
}

int c = waitKey(10);
if ((char)c == 'c') {
break;
}
}
}
return 0;
}

void detectAndDisplay(Mat frame) {
std::vector<Rect> faces;
Mat frame_gray;

cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);

//-- 人脸检测
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));

for (size_t i = 0; i < faces.size(); i++) {
Point center(faces[ i ].x + faces[ i ].width / 2, faces[ i ].y + faces[ i ].height / 2);
ellipse(frame, center, Size(faces[ i ].width / 2, faces[ i ].height / 2), 0, 0, 360,
Scalar(255, 0, 255), 2, 8, 0);

Mat faceROI = frame_gray(faces[ i ]);
std::vector<Rect> eyes;

//-- 在脸中检测眼睛
eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));

for (size_t j = 0; j < eyes.size(); j++) {
Point eye_center(faces[ i ].x + eyes[ j ].x + eyes[ j ].width / 2,
faces[ i ].y + eyes[ j ].y + eyes[ j ].height / 2);
int radius = cvRound((eyes[ j ].width + eyes[ j ].height) * 0.25);
circle(frame, eye_center, radius, Scalar(255, 0, 0), 3, 8, 0);
}
}
//-- 显示最终效果图
imshow(window_name, frame);
}

效果为:

face


OpenCV4入门教程131:人脸检测
https://feater.top/opencv/opencv-face-detection/
作者
JackeyLea
发布于
2020年11月10日
许可协议