opencv-091-对象检测(HAAR级联检测器使用)

知识点

HAAR级联检测器,OpenCV中的HAAR级联检测器支持人脸检测、微笑、眼睛与嘴巴检测等,通过加载这些预先训练的HAAR模型数据可以实现相关的对象检测。

API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void cv::CascadeClassifier::detectMultiScale(
InputArray image,
std::vector< Rect > & objects,
double scaleFactor = 1.1,
int minNeighbors = 3,
int flags = 0,
Size minSize = Size(),
Size maxSize = Size()
)
各个参数解释如下:
Image:输入图像
Objects 人脸框
ScaleFactor 放缩比率
minNeighbors 表示最低相邻矩形框
flags 标志项OpenCV3.x以后不用啦,
minSize 可以检测的最小人脸
maxSize 可以检测的最大人脸

代码(c++,python)

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
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;
CascadeClassifier faceDetector;
String haar_data_file = "D:/opencv-4.0.0/opencv/build/etc/haarcascades/haarcascade_frontalface_alt_tree.xml";
int main(int artc, char** argv) {
Mat frame, gray;
vector<Rect> faces;
VideoCapture capture(0);
faceDetector.load(haar_data_file);
namedWindow("frame", WINDOW_AUTOSIZE);
while (true) {
capture.read(frame);
cvtColor(frame, gray, COLOR_BGR2GRAY);
equalizeHist(gray, gray);
faceDetector.detectMultiScale(gray, faces, 1.2, 1, 0, Size(30, 30), Size(400, 400));
for (size_t t = 0; t < faces.size(); t++) {
rectangle(frame, faces[t], Scalar(0, 0, 255), 2, 8, 0);
}
char c = waitKey(10);
if (c == 27) {
break;
}
imshow("frame", frame);
}

waitKey(0);
return 0;
}
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
"""
对象检测(HAAR级联检测器使用)
"""

import cv2 as cv

capture = cv.VideoCapture(0)
detector = cv.CascadeClassifier(cv.data.haarcascades + "haarcascade_frontalface_alt.xml")

while True:
ret, image = capture.read()
if not ret:
break

faces = detector.detectMultiScale(image, scaleFactor=1.05,
minNeighbors=1, minSize=(30, 30), maxSize=(200, 200))
for x, y, width, height in faces:
cv.rectangle(image, (x, y), (x + width, y + height), (0, 0, 255),
2, cv.LINE_8, 0)
cv.imshow("faces", image)

c = cv.waitKey(50)
if c == 27:
break

cv.destroyAllWindows()

结果

代码地址

github