OpenCV 实现HOG行人检测

来源:互联网 发布:站长工具 网站数据 编辑:程序博客网 时间:2024/05/20 18:01

转载自:masikkk

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

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <string>  
  3. #include <opencv2/core/core.hpp>  
  4. #include <opencv2/highgui/highgui.hpp>  
  5. #include <opencv2/imgproc/imgproc.hpp>  
  6. #include <opencv2/objdetect/objdetect.hpp>  
  7. #include <opencv2/ml/ml.hpp>  
  8.   
  9. using namespace std;  
  10. using namespace cv;  
  11.   
  12. int main()  
  13. {  
  14.   
  15.     Mat src = imread("5.png");  
  16.     HOGDescriptor hog;//HOG特征检测器  
  17.     hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());//设置SVM分类器为默认参数  
  18.     vector<Rect> found, found_filtered;//矩形框数组  
  19.     hog.detectMultiScale(src, found, 0, Size(8,8), Size(32,32), 1.05, 2);//对图像进行多尺度检测,检测窗口移动步长为(8,8)  
  20.   
  21.     cout<<"矩形个数:"<<found.size()<<endl;  
  22.     //找出所有没有嵌套的矩形框r,并放入found_filtered中,如果有嵌套的话,则取外面最大的那个矩形框放入found_filtered中  
  23.     for(int i=0; i < found.size(); i++)  
  24.     {  
  25.         Rect r = found[i];  
  26.         int j=0;  
  27.         for(; j < found.size(); j++)  
  28.             if(j != i && (r & found[j]) == r)  
  29.                 break;  
  30.         if( j == found.size())  
  31.             found_filtered.push_back(r);  
  32.     }  
  33.     cout<<"过滤后矩形的个数:"<<found_filtered.size()<<endl;  
  34.   
  35.     //画矩形框,因为hog检测出的矩形框比实际人体框要稍微大些,所以这里需要做一些调整  
  36.     for(int i=0; i<found_filtered.size(); i++)  
  37.     {  
  38.         Rect r = found_filtered[i];  
  39.         r.x += cvRound(r.width*0.1);  
  40.         r.width = cvRound(r.width*0.8);  
  41.         r.y += cvRound(r.height*0.07);  
  42.         r.height = cvRound(r.height*0.8);  
  43.         rectangle(src, r.tl(), r.br(), Scalar(0,255,0), 3);  
  44.     }  
  45.   
  46.     imwrite("ImgProcessed.jpg",src);  
  47.     namedWindow("src",0);  
  48.     imshow("src",src);  
  49.     waitKey();//注意:imshow之后一定要加waitKey,否则无法显示图像  
  50.       
  51.   
  52.   
  53.     system("pause");  
  54. }  

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原论文翻译见:http://blog.csdn.net/masibuaa/article/details/14056807


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

http://download.csdn.net/detail/masikkk/6547451


0 0