OpenCV学习笔记:人脸检测(级联分类器) 妖狐艹你老母 2024-04-18 14:32 16阅读 0赞 在这节教程中您将学到: * 使用 [CascadeClassifier][] 类来检测视频流中的物体. * 特别地, 我们将使用函数:[load][] 来加载一个 .xml 分类器文件. 它既可以是Haar特征也可以是LBP特征的分类器. * [detectMultiScale][] 来进行图像的多尺度检测. 代码 ## ## 本教程的代码如下所示. 你也可以 [点这里][Link 1] 下载. 第二个版本 (使用LBP进行人脸检测) 可以 [从这里][Link 2] 找到. #include "opencv2/objdetect/objdetect.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> #include <stdio.h> using namespace std; using namespace cv; /** 函数声明 */ void detectAndDisplay( Mat frame ); /** 全局变量 */ string face_cascade_name = "haarcascade_frontalface_alt.xml"; string eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml"; CascadeClassifier face_cascade; CascadeClassifier eyes_cascade; string window_name = "Capture - Face detection"; RNG rng(12345); /** @主函数 */ int main( int argc, const char** argv ) { CvCapture* capture; Mat frame; //-- 1. 加载级联分类器文件 if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; }; if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; }; //-- 2. 打开内置摄像头视频流 capture = cvCaptureFromCAM( -1 ); if( capture ) { while( true ) { frame = cvQueryFrame( capture ); //-- 3. 对当前帧使用分类器进行检测 if( !frame.empty() ) { detectAndDisplay( frame ); } else { printf(" --(!) No captured frame -- Break!"); break; } int c = waitKey(10); if( (char)c == 'c' ) { break; } } } return 0; } /** @函数 detectAndDisplay */ void detectAndDisplay( Mat frame ) { std::vector<Rect> faces; Mat frame_gray; cvtColor( frame, frame_gray, CV_BGR2GRAY ); equalizeHist( frame_gray, frame_gray ); //-- 多尺寸检测人脸 face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) ); for( int i = 0; i < faces.size(); i++ ) { Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 ); ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 ); Mat faceROI = frame_gray( faces[i] ); std::vector<Rect> eyes; //-- 在每张人脸上检测双眼 eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) ); for( int j = 0; j < eyes.size(); j++ ) { Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 ); int radius = cvRound( (eyes[j].width + eyes[i].height)*0.25 ); circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 ); } } //-- 显示结果图像 imshow( window_name, frame ); } ## ## ## 结果 ## 1. 下图就是使用上述代码对内置摄像头的视频流进行人脸检测的结果图像: ![../../../../\_images/Cascade\_Classifier\_Tutorial\_Result\_Haar.jpg][.._.._.._.._images_Cascade_Classifier_Tutorial_Result_Haar.jpg] 注意复制分类器文件 *haarcascade\_frontalface\_alt.xml* 和 *haarcascade\_eye\_tree\_eyeglasses.xml* 到你的当前目录下. 他们在OpenCV安装文件夹*opencv/data/haarcascades* 里面. 2. 下图是使用分类器文件 *lbpcascade\_frontalface.xml* (LBP特征训练的) 进行的检测结果. 对于双眼的检测依旧使用刚才使用过的分类器. ![../../../../\_images/Cascade\_Classifier\_Tutorial\_Result\_LBP.jpg][.._.._.._.._images_Cascade_Classifier_Tutorial_Result_LBP.jpg] [CascadeClassifier]: http://opencv.willowgarage.com/documentation/cpp/objdetect_cascade_classification.html#cascadeclassifier [load]: http://opencv.willowgarage.com/documentation/cpp/objdetect_cascade_classification.html#cv-cascadeclassifier-load [detectMultiScale]: http://opencv.willowgarage.com/documentation/cpp/objdetect_cascade_classification.html#cv-cascadeclassifier-detectmultiscale [Link 1]: https://code.ros.org/svn/opencv/trunk/opencv/samples/cpp/tutorial_code/objectDetection/objectDetection.cpp [Link 2]: https://code.ros.org/svn/opencv/trunk/opencv/samples/cpp/tutorial_code/objectDetection/objectDetection2.cpp [.._.._.._.._images_Cascade_Classifier_Tutorial_Result_Haar.jpg]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/15/741dbacf7d1f40119363aedaf075054c.jpg [.._.._.._.._images_Cascade_Classifier_Tutorial_Result_LBP.jpg]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/15/34cae77e33724f08b8457d44679c33eb.jpg
还没有评论,来说两句吧...