HOG特征+SVM行人检测


API介绍:

1 #include <opencv2/opencv.hpp>
2 #include <iostream>
3
4 using namespace cv;
5 using namespace std;
6
7 int main(int argc, char** argv) {
8 Mat src = imread("L:/opencv_picture/ren4.jpg");
9 if (src.empty()) {
10 printf("could not load image...\n");
11 return -1;
12 }
13 namedWindow("input image", CV_WINDOW_AUTOSIZE);
14 imshow("input image", src);
15
16 /*
17 Mat dst, dst_gray;
18 resize(src, dst, Size(64, 128));
19 cvtColor(dst, dst_gray, COLOR_BGR2GRAY);
20 HOGDescriptor detector(Size(64, 128), Size(16, 16), Size(8, 8), Size(8, 8), 9);
21
22 vector<float> descriptors;
23 vector<Point> locations;
24 detector.compute(dst_gray, descriptors, Size(0, 0), Size(0, 0), locations);
25 printf("number of HOG descriptors : %d", descriptors.size());
26 */
27 HOGDescriptor hog = HOGDescriptor(); //将hog作为HOGDescriptor的一个函数名
28 hog.setSVMDetector(hog.getDefaultPeopleDetector());
29 //让hog函数内的setSVMDetector参数为getDefaultPeopleDetector()做行人检测
30
31 vector<Rect> foundLocations; //定个一个变量行人矩形框
32 hog.detectMultiScale(src, foundLocations, 0, Size(2, 2), Size(32, 32), 1.05, 2);
33 //多个尺度的行人的寻找,采用不同的开窗大小检测。
34 //参数:1.检测图像 2.检测结果 3.默认为0 4.滑框每次移动步长 5.图像扩充大小 6.图像尺度(高斯金字塔) 7.默认
35 // 第四个参数越大,检测窗口移动的步长越大,检测的目标个数越小
36 //第五个参数:pad size 有(8, 8), (16, 16), (24, 24), (32, 32).适当的pad可以提高检测的准确率
37 //6.scale参数可以具体控制金字塔的层数,参数越小,层数越多,检测时间也长。通常scale在1.01-1.5这个区间
38 Mat result = src.clone();
39 for (size_t t = 0; t < foundLocations.size(); t++) {
40 rectangle(result, foundLocations[t], Scalar(0, 0, 255), 2, 8, 0);
41 }
42 namedWindow("HOG SVM Detector Demo", CV_WINDOW_AUTOSIZE);
43 imshow("HOG SVM Detector Demo", result);
44
45 waitKey(0);
46 return 0;
47 }
我这里的 pad size取的size(2,2)的检测效果:
原图: 效果图:
