现在的位置: 首页 > 综合 > 正文

行人检测 opencv

2017年11月11日 ⁄ 综合 ⁄ 共 1554字 ⁄ 字号 评论关闭
#include "OpenCV.h"
#include "std.h"

int main(int argc, char** argv)
{
	char* filename = "1.jpg";
	
	//带有检测目标的源图像
	Mat img;

	//定义梯度方向直方图
	HOGDescriptor hog;

	//读取一块特征数组,也就是训练出的检测目标的特征,例如人,车
	hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());

	namedWindow("people detector", 1);
/*
	VideoCapture CameraCap = VideoCapture(0);
	CameraCap.set(CV_CAP_PROP_FRAME_WIDTH,720);
	CameraCap.set(CV_CAP_PROP_FRAME_HEIGHT,480);
*/
	for(;;)
	{
//		CameraCap >> img;
		img = imread( filename );
//		printf( "%s:\n", filename );
		if(!img.data)
			break;

		//存放已找到的类似检测目标的区域范围
		vector< Rect > found, found_filtered;
		double t = ( double )getTickCount();
		// run the detector with default parameters. to get a higher hit-rate
		// (and more false alarms, respectively), decrease the hitThreshold and
		// groupThreshold (set groupThreshold to 0 to turn off the grouping completely).

		//检测目标,倒数第二个参数是步进级别,倒数第一个是候选区域,0的话所有区域都是独立的
		hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.06, 2);
		t = (double)getTickCount() - t;
		printf("tdetection time = %gms\n", t*1000./cv::getTickFrequency());
		int i, j;

		//去掉重复的区域
		for( i = 0; i < found.size(); i++ )
		{
			Rect r = found[i];
			for( j = 0; j < found.size(); j++ )
				if( j != i && (r & found[j]) == r)
					break;
			if( j == found.size() )
				found_filtered.push_back(r);
		}
		for( i = 0; i < found_filtered.size(); i++ )
		{
			Rect r = found_filtered[i];
			// the HOG detector returns slightly larger rectangles than the real objects.
			// so we slightly shrink the rectangles to get a nicer output.
			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(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3);
		}
		imshow("people detector", img);
//		cvWaitKey(1);
		int c = waitKey(1);// & 255;
		if( c == 'q' || c == 'Q' )
			break;
	}

	return 0;
}

抱歉!评论已关闭.