OpenCV读入图片序列进行HOG行人检测并保存为视频

来源:互联网 发布:投资数据库终端 编辑:程序博客网 时间:2024/05/18 16:40

OpenCV读入图片序列进行HOG行人检测并保存为视频

http://blog.csdn.net/masibuaa/article/details/16084467

 4273人阅读 评论(17) 收藏 举报
 分类:
 
  

此程序是用OpenCV的默认SVM参数进行检测,若图片过大过多,处理起来会比较慢。


[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <iostream>  
  3. #include <fstream>  
  4. #include <opencv2/core/core.hpp>  
  5. #include <opencv2/highgui/highgui.hpp>  
  6. #include <opencv2/imgproc/imgproc.hpp>  
  7. #include <opencv2/objdetect/objdetect.hpp>  
  8. #include <opencv2/ml/ml.hpp>  
  9.   
  10. using namespace std;  
  11. using namespace cv;  
  12.   
  13. int main()  
  14. {  
  15.     Mat src;  
  16.     string ImgName;//图片文件名  
  17.     //ifstream fin("Seq4List.txt");//打开图片序列的文件列表  
  18.     ifstream fin("subset.txt");  
  19.   
  20.     namedWindow("ImageSeq",0);  
  21.   
  22.     VideoWriter videoWriter;//视频写入器  
  23.     videoWriter.open("Seq6.avi", CV_FOURCC('x','v','I','D'),25,Size(1292,964));//注意若图片尺寸与写入器的尺寸不同的话可能失败  
  24.     if(!videoWriter.isOpened()) cout<< "创建VideoWriter失败"<<endl;  
  25.   
  26.     HOGDescriptor people_detect_hog; //HOG特征检测器  
  27.     //采用默认的已经训练好了的SVM系数作为检测的模型  
  28.     people_detect_hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());  
  29.       
  30.     while(getline(fin,ImgName))  
  31.     {  
  32.         cout<<"处理:"<<ImgName<<endl;  
  33.         string fullName = "D:\\TrackingTool-Matlab\\seq4\\" + ImgName;//加上路径名,"\\"表示转义字符"\"  
  34.         src = imread(fullName);  
  35.   
  36.         vector<Rect> found, found_filtered; //矩形框数组  
  37.         //对输入的图片进行多尺度行人检测,检测窗口移动步长为(8,8)  
  38.         people_detect_hog.detectMultiScale(src, found, 0, Size(8, 8), Size(32, 32), 1.05, 2);  
  39.         //找出所有没有嵌套的矩形框r,并放入found_filtered中,如果有嵌套的话,则取外面最大的那个矩形框放入found_filtered中  
  40.         for(int i=0; i < found.size(); i++)  
  41.         {  
  42.             Rect r = found[i];  
  43.             int j=0;  
  44.             for(; j < found.size(); j++)  
  45.                 if(j != i && (r & found[j]) == r)  
  46.                     break;  
  47.             if( j == found.size())  
  48.                 found_filtered.push_back(r);  
  49.         }  
  50.   
  51.         //画矩形框,因为hog检测出的矩形框比实际人体框要稍微大些,所以这里需要做一些调整  
  52.         for(int i=0; i<found_filtered.size(); i++)  
  53.         {  
  54.             Rect r = found_filtered[i];  
  55.             r.x += cvRound(r.width*0.1);  
  56.             r.width = cvRound(r.width*0.8);  
  57.             r.y += cvRound(r.height*0.07);  
  58.             r.height = cvRound(r.height*0.8);  
  59.             rectangle(src, r.tl(), r.br(), Scalar(0,255,0), 3);  
  60.         }  
  61.   
  62.         videoWriter << src;//写入一帧到文件  
  63.   
  64.         imshow("ImageSeq",src);  
  65.         waitKey(50);//注意:imshow之后必须有waitKey(),否则无法显示图像  
  66.     }  
  67.   
  68.     videoWriter.release();  
  69.     //system("pause");  
  70.     return 0;  
  71. }  


效果:



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

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

0 0
原创粉丝点击