#빌드환경
- Windows7 64bit + VS2010 + OpenCV 2.4.3
#include "stdafx.h"
#include <iostream>
#include <opencv2⁄core⁄core.hpp>
#include <opencv2⁄contrib⁄contrib.hpp>
#include <opencv2⁄highgui⁄highgui.hpp>
#include <opencv2⁄imgproc⁄imgproc.hpp>
#include <opencv2⁄objdetect⁄objdetect.hpp>
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
⁄⁄haar xml분류기
const char *classifer = "C:⁄opencv 2.4.3⁄data⁄haarcascades⁄haarcascade_frontalface_default.xml";
const char *classifer1 = "C:⁄opencv 2.4.3⁄data⁄haarcascades⁄haarcascade_eye.xml";
CvHaarClassifierCascade* cascade_face = 0;
cascade_face = (CvHaarClassifierCascade*) cvLoad(classifer, 0, 0, 0 );
CvHaarClassifierCascade* cascade_eye = 0;
cascade_eye = (CvHaarClassifierCascade*) cvLoad(classifer1, 0, 0, 0 );
if(!cascade_face||!cascade_eye){
std::cerr<<"error: cascade error!!"<<std::endl;
return -1;
}
CvMemStorage* storage_face = 0;
storage_face = cvCreateMemStorage(0);
CvMemStorage* storage_eye = 0;
storage_eye = cvCreateMemStorage(0);
if(!storage_face||!storage_eye){
std::cerr<<"error: storage error!!"<<std::endl;
return -2;
}
⁄⁄cam
IplImage* image = 0;
CvCapture* cap = cvCaptureFromCAM(0);
cvNamedWindow("cam",0);
cvResizeWindow("cam", 640,480);
while(1)
{
cvGrabFrame(cap);
image = cvRetrieveFrame(cap);
⁄⁄ face detection
CvSeq *faces = 0;
faces = cvHaarDetectObjects(image, cascade_face, storage_face, 2.0, 5, 0);
⁄⁄검출된 모든 face에 대한 반복문
for(int i=0; i<faces->total; i++){
⁄⁄face 영역 가져오기
CvRect *r = 0;
r = (CvRect*) cvGetSeqElem(faces, i);
⁄⁄frame에 face 영역 그리기
cvRectangle(image, cvPoint(r->x, r->y), cvPoint(r->x+r->width, r->y+r->height), cvScalar(0,255,0), 3, CV_AA, 0);
CvRect rect;
rect.x = r->x;
rect.y = r->y;
rect.width = r->width;
rect.height = r->height;
⁄⁄ eye detection
cvSetImageROI(image, rect);
IplImage* fimg = cvCreateImage(cvSize(r->width, r->height), image->depth, image->nChannels);
cvCopy(image, fimg);
cvResetImageROI(image);
CvSeq *eyes = 0;
eyes = cvHaarDetectObjects(fimg, cascade_eye, storage_eye, 1.5, 5, 0);
for(int j=0; j<eyes->total; j++){
CvRect *r1 = 0;
r1 = (CvRect*) cvGetSeqElem(eyes, j);
⁄⁄frame에 eye 영역 그리기
cvRectangle(image, cvPoint(r1->x+r->x, r1->y+r->y), cvPoint(r1->x+r->x+r1->width, r1->y+r->y+r1->height), cvScalar(0,0,255), 3, CV_AA, 0);
}
cvReleaseImage(&fimg);
}
cvShowImage("cam", image);
⁄⁄
if(cvWaitKey(30)>=0)
break;
}
cvReleaseMemStorage(&storage_face);
cvReleaseMemStorage(&storage_eye);
cvReleaseCapture(&cap);
cvDestroyWindow("cam");
cvReleaseHaarClassifierCascade(&cascade_eye);
cvReleaseHaarClassifierCascade(&cascade_face);
return 0;
}