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

此程序是用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;
}


效果:



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

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

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

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 21
    评论
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值