1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| #include "stdafx.h"
#include <cv.h>
#include <stdio.h>
#include <cxcore.h>
#include <highgui.h>
using namespace std;
CvHaarClassifierCascade* cascade = 0;
CvMemStorage* storage = 0;
int _tmain(int argc, _TCHAR* argv[])
{ int i;
for (int k=0;k<100;k++)
{
std::string varimg;
char format[] = "franck_000%d.jpg";
char filename[sizeof format+100];
sprintf(filename,format,k);
varimg = filename ;
IplImage*imgw = cvLoadImage( varimg.c_str() );
IplImage* gray;
/* Load the face detector and create memory storage
`cascade` and `storage` are global variables */
if (!cascade) {
char* file = "C:/OpenCV-2.1.0/data/haarcascades/haarcascade_frontalface_alt.xml";
cascade = (CvHaarClassifierCascade*) cvLoad(file, 0, 0, 0);
storage = cvCreateMemStorage(0);
}
/* Convert multichannel to 1-channel for faster processing */
if (imgw->nChannels == 1) {
gray == cvClone(imgw);
} else {
gray = cvCreateImage(cvGetSize(imgw), imgw->depth, 1);
cvCvtColor(imgw, gray, CV_RGB2GRAY);
}
/* detect faces */
CvSeq* faces = cvHaarDetectObjects(gray,cascade,storage,1.1,3,CV_HAAR_DO_CANNY_PRUNING,cvSize(20, 20));
int i;
/* Draw red boxes on the faces found */
for( i = 0; i < (faces ? faces->total : 0); i++ ) {
CvRect* r = (CvRect*)cvGetSeqElem(faces, i);
cvRectangle(imgw,cvPoint(r->x, r->y),cvPoint(r->x + r->width, r->y + r->height),CV_RGB(255, 0, 0),3, 8, 0);
}
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1",imgw);
cvWaitKey(1);
cvReleaseImage(&imgw);
}
return 0;
} |
Partager