#include "StdAfx.h"
#include<iostream>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/objdetect/objdetect.hpp>
#include"cv.h"
#include"highgui.h"
#include<stdio.h>
using namespace cv;
using namespace std;
string face_cascade_name = "haarcascade_frontalface_alt2.xml";
CascadeClassifier face_cascade;
string window_name = "人脸识别";
void detectAndDisplay(Mat frame)
{
std::vector<Rect>faces;
Mat frame_gray;
cvtColor(frame,frame_gray,CV_BGR2GRAY);
equalizeHist(frame_gray,frame_gray);
face_cascade.detectMultiScale(frame_gray,faces,1.1,2,0|CV_HAAR_SCALE_IMAGE,Size(30,30));
for(int i = 0;i<faces.size();i++)
{
Point center(faces.x + faces.width*0.5,faces.y + faces.height*0.5);
ellipse(frame,center,Size(faces.width*0.5,faces.height*0.5),0,0,360,Scalar(255,0,255),4,8,0);
}
imshow(window_name,frame);
}
int _tmain(int argc,_TCHAR*argv[])
{
VideoCapture cap(0);
if(!cap.isOpened())
return -1;
Mat edges;
if(!face_cascade.load(face_cascade_name))
{
printf("[error]无法加载级联分类器文件!\n");
return -1;
}
int nTick = 0;
for(;;)
{
if(!cap.isOpened())
{
continue;
}
Mat frame;
nTick = getTickCount();
cap>>frame;
if(frame.data == NULL)
{
continue;
}
cvtColor(frame,edges,CV_BGR2BGRA);
detectAndDisplay(edges);
if(waitKey(30)>=0)break;
}
return 0;
} |