人脸识别分为两大步骤
1.人脸检测 这个是首要实现的,你得实现人脸显示的时候把人脸框出来,当然算法很多,还有一些人眼检测鼻子检测什么的
主要用的是这个
const char *faceCascadeFilename = "haarcascade_frontalface_alt.xml";
detect_and_draw(IplImageBuffer,storage,cascade);
这个函数就是检测人脸的并画框效果如下
主要代码如下
void Chenaini::detect_and_draw(IplImage* img,CvMemStorage* storage, CvHaarClassifierCascade* cascade)
{
double scale=1.2;
static CvScalar colors[] = {
{{0,0,255}},{{0,128,255}},{{0,255,255}},{{0,255,0}},
{{255,128,0}},{{255,255,0}},{{255,0,0}},{{255,0,255}}
};//Just some pretty colors to draw with
IplImage* gray = cvCreateImage(cvSize(img->width,img->height),8,1);
IplImage* small_img=cvCreateImage(cvSize(cvRound(img->width/scale),cvRound(img->height/scale)),8,1);
cvCvtColor(img,gray, CV_BGR2GRAY);
cvResize(gray, small_img, CV_INTER_LINEAR);
cvEqualizeHist(small_img,small_img); cvClearMemStorage(storage);
double t = (double)cvGetTickCount();
CvSize min=cvSize(0,0);
CvSize max=cvSize(100,100);
CvSeq *objects = cvHaarDetectObjects( small_img,
cascade,storage,
1.1,
3, 0/*CV_HAAR_DO_CANNY_PRUNING*/,
min,
max
);
t = (double)cvGetTickCount() - t;
printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
//Loop through found objects and draw boxes around them
for(int i=0;i<(objects? objects->total:0);++i)
{
CvRect* r=(CvRect*)cvGetSeqElem(objects,i);
cvRectangle(img, cvPoint(r->x*scale,r->y*scale), cvPoint((r->x+r->width)*scale,(r->y+r->height)*scale), colors[i%8]);
}
for( int i = 0; i < (objects? objects->total : 0); i++ )
{
CvRect* r = (CvRect*)cvGetSeqElem( objects, i );
CvPoint center;
int radius;
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);
cvCircle( img, center, radius, colors[i%8], 3, 8, 0 );
}
QImage image(( uchar*)img->imageData, img->width, img->height, QImage::Format_RGB888);
ui->label_shipin->clear();
ui->label_shipin->setScaledContents(true);
ui->label_shipin->setPixmap(QPixmap::fromImage(image));
rs = vd->unget_frame();
cvReleaseImage(&img);
}