OpenCV计算机视觉编程攻略之行人检测,OpenCV 提供了一个基于HOG 和SVM且经过训练的行人检测器,可以用这个SVM 分类器以不同尺度的窗口扫描图像,在完整的图像中检测特定物体。
原图如下:
检测效果图如下:
源码如下:
#include <opencv2/opencv.hpp>
int main()
{
// People detection
cv::Mat myImage = imread("demo.jpg", cv::IMREAD_GRAYSCALE);
// create the detector
std::vector<cv::Rect> peoples;
cv::HOGDescriptor peopleHog;
peopleHog.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector());
// detect peoples oin an image
peopleHog.detectMultiScale(myImage, // input image
peoples, // ouput list of bounding boxes
0, // threshold to consider a detection to be positive
cv::Size(4, 4), // window stride
cv::Size(32, 32), // image padding
1.05, // scale factor
2); // grouping threshold (0 means no grouping)
// draw detections on image
std::cout << "Number of peoples detected: " << peoples.size() << std::endl;
for (int i = 0; i < peoples.size(); i++)
cv::rectangle(myImage, peoples[i], cv::Scalar(255, 255, 255), 2);
cv::imshow("People detection", myImage);
cv::waitKey();
return 0;
}