0
我想寫一些簡單的實時人臉檢測代碼,但不知何故它不起作用。 (我嘗試在圖像上的臉部檢測代碼,它的工作原理,但下面的代碼我得到一個灰色的圖像在屏幕上,代碼失敗)OpenCV中的實時人臉檢測
這裏是我試過的代碼(它打印'檢測到的臉'輸出窗口)
CvHaarClassifierCascade *cascade;
CvMemStorage *storage;
char *face_cascade="haarcascade_frontalface_alt2.xml";
CvRect* r;
const CvArr* img_size;
IplImage *grayscale;
void detectFacialFeatures(IplImage *img)
{
grayscale = cvCreateImage(cvGetSize(img), 8, 1);
cvCvtColor(img, grayscale, CV_BGR2GRAY);
CvMemStorage* storage=cvCreateMemStorage(0);
cvClearMemStorage(storage);
cvEqualizeHist(grayscale, grayscale);
cascade = (CvHaarClassifierCascade*)cvLoad(face_cascade, 0, 0, 0);
CvSeq* faces = cvHaarDetectObjects(grayscale, cascade, storage, 1.1, 3, CV_HAAR_DO_CANNY_PRUNING, cvSize(50, 50));
if(faces)
{
printf("face detected!");
r = (CvRect*)cvGetSeqElem(faces, 0);
cvRectangle(img,cvPoint(r->x, r->y),cvPoint(r->x + r->width, r->y + r->height), CV_RGB(255, 0, 0), 1, 8, 0);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int c;
IplImage* color_img;
CvCapture* cv_cap = cvCreateCameraCapture(0);
cvSetCaptureProperty(cv_cap, CV_CAP_PROP_FRAME_WIDTH, 640);
cvSetCaptureProperty(cv_cap, CV_CAP_PROP_FRAME_HEIGHT, 480);
cvNamedWindow("Video",1); // create window
for(;;) {
color_img = cvQueryFrame(cv_cap); // get frame
if(color_img==0)
break;
cvFlip(color_img, 0, 1); //mirror image
detectFacialFeatures(color_img);
cvShowImage("Video", color_img); // show frame
c = cvWaitKey(10); // wait 10 ms or for key stroke
if(c == 27)
break; // if ESC, break and quit
}
/* clean up */
cvReleaseCapture(&cv_cap);
cvDestroyWindow("Video");
}