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

时间:2021-05-26 21:38:02
#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;
}