参考:http://blog.sina.com.cn/s/blog_593c85f20100ncnj.html
OpenCV的库中带有检测正面人脸的 Haar迭代算法Haar Cascade Face Detector (also known as the Viola-Jones method)。Shervin Emami在他的blog(http://www.shervinemami./introToOpenCV.html)中介绍了相关function的使用。他的检测代码教简单快速,但是没有提供直观的人脸检测后识别人脸的矩形框图,因此,下面结合他的程序加入检测后图像显示功能,可较为直观感受OpenCV带来的便捷。
如何新建一个工程和C++文件,已在《OpenCV入门一:安装配置(with VC++ 2008/2010 Express)》中介绍过,这里不再重复。建好所需的工程和C++文件后,按照下面几个步骤,课生成如图一所示的人脸检测图像:
步骤一:C++代码
新建faceDetector工程,任意取名其中包含的C++文件名,实用下列代码:
#include <stdio.h> // For printf() #include <cv.h> // Main OpenCV library. #include <highgui.h> // OpenCV functions for files and graphical windows. using namespace std; using namespace cv; // Initialize a sub funcion using after main CvRect detectFaceInImage( IplImage *inputImg, CvHaarClassifierCascade* cascade); int main(int argc, char* argv[]) { CvPoint pt1, pt2; // For draw rectangle // Check the input is correct when call executable file if (argc != 2) { printf("Usage: faceDetector.exe <imagename>\n"); exit(-1); } char * imgName = argv[1]; // Copy the second input as the image name // Load image IplImage* inputImg = cvLoadImage(imgName, CV_LOAD_IMAGE_UNCHANGED); if (!inputImg) { printf("Error: Could not open the image file! \n"); exit(-1); } // Haar Cascade file, used for Face Detection. // Note: you could change this directory as your installed OpenCV2.1 location char *faceCascadeFilename = "C:/OpenCV2.1/data/haarcascades/haarcascade_frontalface_alt.xml"; // Load the HaarCascade classifier for face detection. CvHaarClassifierCascade* faceCascade; faceCascade = (CvHaarClassifierCascade*)cvLoad(faceCascadeFilename, 0, 0, 0); if( !faceCascade ) { printf("Couldnt load Face detector '%s'\n", faceCascadeFilename); exit(-1); } // Perform face detection on the input image, using the given Haar classifier CvRect faceRect = detectFaceInImage(inputImg, faceCascade); // Make sure a valid face was detected then draw the rect location. if (faceRect.width > 0) { printf("Detected a face at (%d,%d)!\n", faceRect.x, faceRect.y); // Get the pointer of the face rectangle pt1.x = faceRect.x; pt2.x = faceRect.x + faceRect.width; pt1.y = faceRect.y; pt2.y = faceRect.y + faceRect.height; // Draw the rectangle in the input image cvRectangle( inputImg, pt1, pt2, CV_RGB(255,0,0), 2, 8, 0 ); // Show the detected face image on the screen. cvNamedWindow("Detected face", CV_WINDOW_AUTOSIZE); // Show the image in the window named "Detected face", you could change as you like cvShowImage( "Detected face", inputImg ); // Wait for the user to press something on the graphical window. // Note: cvWaitKey() is needed for time to draw on the screen. cvWaitKey(0); // Free the resources. cvDestroyWindow("Detected face"); cvReleaseImage( &inputImg ); } // Free the Face Detector resources when the program is finished cvReleaseHaarClassifierCascade( &faceCascade ); return 0; } // Perform face detection on the input image, using the given Haar Cascade. // Returns a rectangle for the detected region in the given image. CvRect detectFaceInImage(IplImage *inputImg, CvHaarClassifierCascade* cascade) { // Smallest face size. CvSize minFeatureSize = cvSize(20, 20); // Only search for 1 face. int flags = CV_HAAR_FIND_BIGGEST_OBJECT | CV_HAAR_DO_ROUGH_SEARCH; // How detailed should the search be. float search_scale_factor = 1.1f; IplImage *detectImg; IplImage *greyImg = 0; CvMemStorage* storage; CvRect rc; //double t; CvSeq* rects; CvSize size; int nFaces; //int i, ms; storage = cvCreateMemStorage(0); cvClearMemStorage( storage ); // If the image is color, use a greyscale copy of the image. detectImg = (IplImage*)inputImg; if (inputImg->nChannels > 1) { size = cvSize(inputImg->width, inputImg->height); greyImg = cvCreateImage(size, IPL_DEPTH_8U, 1 ); cvCvtColor( inputImg, greyImg, CV_BGR2GRAY ); detectImg = greyImg; // Use the greyscale image. } // Detect all the faces in the greyscale image. //t = (double)cvGetTickCount(); rects = cvHaarDetectObjects( detectImg, cascade, storage, search_scale_factor, 3, flags, minFeatureSize); //t = (double)cvGetTickCount() - t; //ms = cvRound( t / ((double)cvGetTickFrequency() * 1000.0) ); nFaces = rects->total; //printf("Face Detection took %d ms and found %d objects\n", ms, nFaces); // Get the first detected face (the biggest). if (nFaces > 0) rc = *(CvRect*)cvGetSeqElem( rects, 0 ); else rc = cvRect(-1,-1,-1,-1); // Couldn't find the face. if (greyImg) { cvReleaseImage( &greyImg ); } cvReleaseMemStorage( &storage ); return rc; // Return the biggest face found, or (-1,-1,-1,-1). }
步骤二:编译(只是编译 ,这一步还看不到图片)
如果编译出错,比如找不到cv打头的function(比如CvHaarClassifierCascade等),或者找不到头文件、资源文件等,都有可能是工程配置不对。请参考之前提到的配置文章查看自己的配置。
Debug之后,在此新建工程中Debug文件夹里找到可执行文件。如果你新建的工程名是faceDetector,无论你的C++文件名是什么,debug之后可执行文件的名字都是faceDetector.exe。
步骤三:调用可执行文件
Start -> Run 在输入框中输入 cmd 然后回车,出现dos命令行窗口。
按图二所示,输入faceDetector工程Debug文件夹的地址,输入 “可执行文件名(红色框内)图片名”,然后回车,就会得到检测出人脸的图像(图一)以及图中矩形框左上角的其实坐标。
注意: 需要依赖的lib库如下 项目/属性/连接器
opencv_core247d.lib
opencv_highgui247d.lib
opencv_imgproc247d.lib
opencv_features2d247d.lib
opencv_calib3d247d.lib
opencv_objdetect247d.lib