opencv检测视频中的人脸,并标记画框,同时输出检测结果到文件夹

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>

using namespace cv;
using namespace std;


#include "stdio.h"


void detectAndDraw(Mat & img,CascadeClassifier & cascade,
						CascadeClassifier & nestedCascde,
						double scale,bool tryflip,int _iPicNum) 
{
	int i = 0;
	double t = 0;
	vector<Rect> faces,faces2;

	const static Scalar colors [] = {
	CV_RGB(0,0,255),
	CV_RGB(0,128,255),
	CV_RGB(0,255,255),
	CV_RGB(255,128,0),
	CV_RGB(255,2555,0),
	CV_RGB(255,0,0),
	CV_RGB(255,0,255)
	};

	Mat gray,smallImg(cvRound(img.rows/scale),cvRound(img.cols/scale),CV_8UC1);

	cvtColor(img,gray,CV_BGR2GRAY);
	
	resize(gray,smallImg,smallImg.size(),0,0,INTER_LINEAR);	

	equalizeHist(smallImg,smallImg);

	//t = (double)cvGetTickCount();

	
	int iEnable = 0;
	cascade.detectMultiScale(smallImg,faces,1.1,2,0 | CV_HAAR_SCALE_IMAGE,Size(10,10));

	for(vector<Rect>::const_iterator r = faces.begin();r != faces.end();r++,i++)
	{
		Mat smallImgROL;

		vector<Rect> nestedObjects;
		Point center;

		Scalar color = colors[i%8];

		int radius;
		double aspect_ratio = (double)r->width/r->height;
		if(0.75 < aspect_ratio && aspect_ratio < 1.3)
		{
			iEnable  = 1;
			center.x = cvRound((r->x + r->width * 0.5) * scale);
			center.y = cvRound ((r->y + r->height * 0.5) * scale);
			radius = cvRound((r->width + r->height) * 0.25 * scale);

			Rect rect1(center.x - radius,center.y - radius,radius * 2,radius * 2);

			Mat roi1;


			img(rect1).copyTo(roi1);
			char cbuf[12] = {0};

			sprintf(cbuf,"ceshijieguo/%d_%d.jpg",_iPicNum,i);

			imwrite(cbuf,roi1);

		//	circle(img,center,radius,color,3,8,0);


			//rectangle(img,center.x  ,center.y,color ,3,8,0);
			
			rectangle(img,cvPoint(cvRound(r->x * scale),cvRound(r->y * scale)),
				cvPoint(cvRound((r->x + r->width -1 )*scale),cvRound((r->y + r->height - 1)* scale)),
				color ,3,8,0);
			
		}
		else
		{
			rectangle(img,cvPoint(cvRound(r->x * scale),cvRound(r->y * scale)),
				cvPoint(cvRound((r->x + r->width -1 )*scale),cvRound((r->y + r->height - 1)* scale)),
				color ,3,8,0);


						//视频画框
					//	for (vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++)
					//			rectangle(img, cvPoint(r->x, r->y), cvPoint(r->x + r->width - 1, r->y + r->height - 1), Scalar(255, 0, 255), 3, 8, 0);
						
						//文字标注
					//	putText(imgForShow, p.first, Point(BBox.x, BBox.y), FONT_HERSHEY_PLAIN, 2, Scalar(255, 0, 0));

		}
		if( iEnable)
		{
			char cbuf[18] = {0};
			sprintf(cbuf,"ceshijieguo/%d.jpg",_iPicNum);
			imwrite(cbuf,img);
			
			imshow("video",img);
			

		}

	

	}
	imshow("video",img);
	cvWaitKey(1);


}

int main()
{

	Mat frame;
	Mat edges;

	CascadeClassifier cascade,nestedCascade;
	bool stop = false;

	cascade.load("haarcascade_frontalface_alt.xml");
	nestedCascade.load("haarcascade_eye.xml");

	VideoCapture capture;

	capture.open("ceshi.mkv");

	double rate = capture.get(CV_CAP_PROP_FPS);


	int i = 0;
	while(capture.read(frame))
	{
		detectAndDraw(frame,cascade,nestedCascade,4,0,i+ 1);
		i++;
	}

	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值