opencv视频行人检测1(HOG+SVM)_C/C++_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > C/C++ > opencv视频行人检测1(HOG+SVM)

opencv视频行人检测1(HOG+SVM)

 2019/1/8 20:32:27  唯爱酥酥  程序员俱乐部  我要评论(0)
  • 摘要:#include<opencv2/core/core.hpp>#include<opencv2/highgui/highgui.hpp>#include<opencv2/imgproc/imgproc.hpp>#include<opencv2/objdetect.hpp>#include<iostream>usingnamespacestd;usingnamespacecv;//定义一个方法detectAndDraw
  • 标签:SVM 检测
class="视频行人检测" name="code">#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/objdetect.hpp> 
#include<iostream>

using namespace std;
using namespace cv;

//定义一个方法detectAndDraw,两个参数hog和img
void detectAndDraw(HOGDescriptor &hog, Mat &img)
{
	//矩形框数组
	vector<Rect> found, found_filtered;
	//调用GetTickCount()函数返回(retrieve)从操作系统启动所经过(elapsed)的毫秒数(可以不要)
	double t = (double)getTickCount();
	//多尺度检测目标,返回的矩形从大到小排列
	hog.detectMultiScale(img, found, 0, Size(8, 8), Size(32, 32), 1.05, 2);
	//每测一次的毫秒数
	t = (double)getTickCount() - t;
	//输出getTickFrequency()函数:返回每秒的计时周期数
	cout << "detection time = " << (t*1000. / cv::getTickFrequency()) << " ms" << endl;
	cout << "detection result = " << found.size() << " Rects" << 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 << "Real detection result = " << found_filtered.size() << " Rects" << endl;
	//画出矩形框
	for (int i = 0; i<found_filtered.size(); i++)
	{
		Rect r = found[i];
		rectangle(img, r.tl(), r.br(), Scalar(0, 255, 0), 3);
	}
}


int main(int argc, char **argv)
{
	//HOG特征检测器  
	HOGDescriptor hog;
	//设置分类器
	hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); 
	//通过VideoCaptrue类对视频进行读取操作以及调用摄像头										   
	VideoCapture vc;
	
	Mat frame;
	//加载视频
	vc.open("D://xingren.avi");
	//如果打不开视频,则。。。。
	if (!vc.isOpened())
		throw runtime_error(string("can't open video file: " ));
	//
	while (1)
	{
		vc >> frame;//等价于vc.read(frame);从视频中获取图片
		if (frame.empty()) //如果某帧为空则退出循环
		    break;
		//调用detectAndDraw()函数,并传参
		detectAndDraw(hog, frame);
		//创建窗口,显示结果图片
		namedWindow("frame", WINDOW_NORMAL);
		imshow("frame", frame);

		//这个函数是在一个给定的时间内(单位ms)等待用户按键触发;如果用户没有按下键,则接续等待(循环)
		int c = waitKey(vc.isOpened() ? 30 : 0) & 255;
		if (c == 'q' || c == 'Q' || c == 27)
			break;
		//waitKey(0);//每帧延时20毫秒

		/*while (waitKey(10) != 27);
		destroyWindow("frame");*/
	}
	//vc.release();//释放资源
	return 0;
}

????????小编在研究完图像行人检测后,又完成了简单的视频行人检测。代码附上。

??????? 小编先定义了一个方法,把画矩形框的代码放到detectAndDraw()方法中,再在主函数main中调用,这样主函数的代码就能看的清晰一些。

??????? 在主函数main中,先用VideoCapture读取视频,然后在图片中获取一帧的图片,对这张图片进行行人检测,再画矩形框。最后设置一个循环,不断地从视频中获取图片。

??????? 不过现在,视频的行人检测比较速度比较慢,而且检测结果也不准确。

?????? 小编需要再去研究研究,下面附上行人视频。

  • xingren.zip (5 MB)
  • 下载次数: 0
上一篇: C++ 对象内存模型 下一篇: 没有下一篇了!
发表评论
用户名: 匿名