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

来源:互联网 发布:倩女幽魂手游开挂软件 编辑:程序博客网 时间:2024/05/23 00:05

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

#include <stdio.h>#include <iostream>#include <fstream>#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;string ImgName;//图片文件名//ifstream fin("Seq4List.txt");//打开图片序列的文件列表ifstream fin("subset.txt");namedWindow("ImageSeq",0);VideoWriter videoWriter;//视频写入器videoWriter.open("Seq6.avi", CV_FOURCC('x','v','I','D'),25,Size(1292,964));//注意若图片尺寸与写入器的尺寸不同的话可能失败if(!videoWriter.isOpened()) cout<< "创建VideoWriter失败"<<endl;HOGDescriptor people_detect_hog; //HOG特征检测器//采用默认的已经训练好了的SVM系数作为检测的模型people_detect_hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());while(getline(fin,ImgName)){cout<<"处理:"<<ImgName<<endl;string fullName = "D:\\TrackingTool-Matlab\\seq4\\" + ImgName;//加上路径名,"\\"表示转义字符"\"src = imread(fullName);vector<Rect> found, found_filtered; //矩形框数组//对输入的图片进行多尺度行人检测,检测窗口移动步长为(8,8)people_detect_hog.detectMultiScale(src, found, 0, Size(8, 8), Size(32, 32), 1.05, 2);//找出所有没有嵌套的矩形框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);}//画矩形框,因为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);}videoWriter << src;//写入一帧到文件imshow("ImageSeq",src);waitKey(50);//注意:imshow之后必须有waitKey(),否则无法显示图像}videoWriter.release();//system("pause");return 0;}
转载:http://blog.csdn.net/masibuaa/article/details/16084467

0 0
原创粉丝点击