Demo3:视频人体检测



#include <iostream>
#include <string>
#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 = imread("10.jpg");
	HOGDescriptor hog;//HOG特征检测器
	hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());//设置SVM分类器为默认参数
	vector<Rect> found, found_filtered;//矩形框数组
	hog.detectMultiScale(src, found, 0, Size(8,8), Size(32,32), 1.05, 2);//对图像进行多尺度检测,检测窗口移动步长为(8,8)
 
	cout<<"矩形个数:"<<found.size()<<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<<"过滤后矩形的个数:"<<found_filtered.size()<<endl;
 
	//画矩形框,因为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);
	}
 
	imwrite("第三个结果图\\10.jpg",src);
	namedWindow("src",0);
	imshow("src",src);
	waitKey(2000);//注意:imshow之后一定要加waitKey,否则无法显示图像
}

猜你喜欢

转载自blog.csdn.net/u011473714/article/details/88244777