opencv中有训练好的hog行人检测器,可以直接用来做行人检测,下面是代码:
#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <vector>
using std::vector;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
Mat frame;
Mat img;
VideoWriter writer("VideoTest.avi", CV_FOURCC('D', 'I', 'V', 'X'), 25.0, Size(768, 576));
VideoCapture cap;
vector<Rect> people;
//定义HOG对象,采用默认参数,或者按照下面的格式自己设置
HOGDescriptor defaultHog;
//设置SVM分类器,用默认分类器
defaultHog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
int j = 0;
cap.open("./vtest.avi");
if( !cap.isOpened() )
{
return -1;
}
while(1)
{
j++;
if(j>100)
{
break;
}
cap >> frame;
if( frame.empty() )
break;
frame.copyTo(img);
//对图像进行多尺度行人检测,返回结果为矩形框
defaultHog.detectMultiScale(img, people,0,Size(8,8),Size(0,0),1.03,2);
//画矩形,框出行人
for (int i = 0; i < people.size(); i++)
{
Rect r = people[i];
rectangle(img, r.tl(), r.br(), Scalar(0, 0, 255), 3);
}
writer << img;
}
waitKey();
return 0;
}
实际效果强差人意,而且感觉速度有点慢啊,debug运行了好久。