您的位置:首页 > 运维架构

OpenCV2.4.4实现HOG行人检测

2014-04-08 09:15 543 查看

利用OpenCV中默认的SVM参数进行HOG行人检测,默认参数是根据Dalal的方法训练的。

[cpp]
view plaincopyprint?





#include <iostream>
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/ml/ml.hpp>

using namespace std;
using namespace cv;

int main()
{

Mat src = imread("5.png");
HOGDescriptor hog;//HOG特征检测器
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());//设置SVM分类器为默认参数
vector<Rect> found, found_filtered;//矩形框数组
hog.detectMultiScale(src, found, 0, Size(8,8), Size(32,32), 1.05, 2);//对图像进行多尺度检测,检测窗口移动步长为(8,8)

cout<<"矩形个数:"<<found.size()<<endl;
//找出所有没有嵌套的矩形框r,并放入found_filtered中,如果有嵌套的话,则取外面最大的那个矩形框放入found_filtered中
for(int i=0; i < found.size(); i++)
{
Rect r = found[i];
int j=0;
for(; j < found.size(); j++)
if(j != i && (r & found[j]) == r)
break;
if( j == found.size())
found_filtered.push_back(r);
}
cout<<"过滤后矩形的个数:"<<found_filtered.size()<<endl;

//画矩形框,因为hog检测出的矩形框比实际人体框要稍微大些,所以这里需要做一些调整
for(int i=0; i<found_filtered.size(); i++)
{
Rect r = found_filtered[i];
r.x += cvRound(r.width*0.1);
r.width = cvRound(r.width*0.8);
r.y += cvRound(r.height*0.07);
r.height = cvRound(r.height*0.8);
rectangle(src, r.tl(), r.br(), Scalar(0,255,0), 3);
}

imwrite("ImgProcessed.jpg",src);
namedWindow("src",0);
imshow("src",src);
waitKey();//注意:imshow之后一定要加waitKey,否则无法显示图像

system("pause");
}

#include <iostream>
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/ml/ml.hpp>

using namespace std;
using namespace cv;

int main()
{

Mat src = imread("5.png");
HOGDescriptor hog;//HOG特征检测器
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());//设置SVM分类器为默认参数
vector<Rect> found, found_filtered;//矩形框数组
hog.detectMultiScale(src, found, 0, Size(8,8), Size(32,32), 1.05, 2);//对图像进行多尺度检测,检测窗口移动步长为(8,8)

cout<<"矩形个数:"<<found.size()<<endl;
//找出所有没有嵌套的矩形框r,并放入found_filtered中,如果有嵌套的话,则取外面最大的那个矩形框放入found_filtered中
for(int i=0; i < found.size(); i++)
{
Rect r = found[i];
int j=0;
for(; j < found.size(); j++)
if(j != i && (r & found[j]) == r)
break;
if( j == found.size())
found_filtered.push_back(r);
}
cout<<"过滤后矩形的个数:"<<found_filtered.size()<<endl;

//画矩形框,因为hog检测出的矩形框比实际人体框要稍微大些,所以这里需要做一些调整
for(int i=0; i<found_filtered.size(); i++)
{
Rect r = found_filtered[i];
r.x += cvRound(r.width*0.1);
r.width = cvRound(r.width*0.8);
r.y += cvRound(r.height*0.07);
r.height = cvRound(r.height*0.8);
rectangle(src, r.tl(), r.br(), Scalar(0,255,0), 3);
}

imwrite("ImgProcessed.jpg",src);
namedWindow("src",0);
imshow("src",src);
waitKey();//注意:imshow之后一定要加waitKey,否则无法显示图像

system("pause");
}


HOG多尺度检测函数说明:

void detectMultiScale(constGpuMat& img, vector<Rect>& found_locations, double hit_threshold= 0,

Sizewin_stride = Size(), Size padding = Size(), double scale0=1.05,intgroup_threshold=2 );

img:待检测的图像,支持CV_8UC1或CV_8UC4类型

found_locations:检测到的目标的包围框数组

hit_threshold:检测到的特征到SVM分类超平面的距离,一般是设为0,在分类器参数中指明。

win_stride:检测窗口每次移动的距离,必须是块移动的整数倍

padding:保持CPU接口兼容性的虚参数,必须是(0,0)。但网上下载的例子里是(32,32)

scale0:滑动窗口每次增加的比例

group_threshold:组阈值,即校正系数,当一个目标被多个窗口检测出来时,该参数此时就起了调节作用, 为0时表示不起调节作用。

效果:









可以看到没有误报,但又很多漏检。

Navneet Dalal在CVPR2005上的HOG原论文翻译见:/article/2630213.html

源码下载,环境为VS2010 + OpenCV2.4.4

http://download.csdn.net/detail/masikkk/6547451
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: