在这节教程中您将学到:
- 使用 CascadeClassifier 类来检测视频流中的物体. 特别地, 我们将使用函数:
- load 来加载一个 .xml 分类器文件. 它既可以是Haar特征也可以是LBP特征的分类器.
- detectMultiScale 来进行图像的多尺度检测.
#include "opencv2/objdetect/objdetect.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include#include 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 loadingn"); return -1; }; if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loadingn"); 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 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 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 ); }
-
下图就是使用上述代码对内置摄像头的视频流进行人脸检测的结果图像:
注意复制分类器文件 haarcascade_frontalface_alt.xml 和 haarcascade_eye_tree_eyeglasses.xml 到你的当前目录下. 他们在OpenCV安装文件夹 opencv/data/haarcascades 里面. -
下图是使用分类器文件 lbpcascade_frontalface.xml (LBP特征训练的) 进行的检测结果. 对于双眼的检测依旧使用刚才使用过的分类器.
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2300
- 用户1336
- 访客10859210
每日一句
True success inspires others to act.
真正的成功是激励他人行动。
真正的成功是激励他人行动。
语法错误: 意外的令牌“标识符”
全面理解Gradle - 定义Task
Motrix全能下载工具 (支持 BT / 磁力链 / 百度网盘)
谷歌Pixel正在开始起飞?
获取ElementUI Table排序后的数据
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is
亲测!虚拟机VirtualBox安装MAC OS 10.12图文教程
华为手机app闪退重启界面清空log日志问题
android ndk开发之asm/page.h: not found
手机屏幕碎了怎么备份操作?
免ROOT实现模拟点击任意位置
新手必看修改DSDT教程
thinkpad t470p装黑苹果系统10.13.2
新会员