首页 > 代码库 > opencv2实现人脸眼鼻口检测_不大稳定
opencv2实现人脸眼鼻口检测_不大稳定
#include <opencv2/core/core.hpp> #include <opencv2/highgui//highgui.hpp> #include <opencv2/objdetect/objdetect.hpp> #include <string> #include <vector> using namespace std; int main() { cv::CascadeClassifier mFaceDetector; cv::CascadeClassifier mEyeDetector; cv::CascadeClassifier mMouthDetector; cv::CascadeClassifier mNoseDetector; //载入四个人脸特征分类器文件,可以从opencv的安装目录中找到 if( mFaceDetector.empty() ) mFaceDetector.load( "haarcascade_frontalface_default.xml" ); if( mEyeDetector.empty() ) mEyeDetector.load( "haarcascade_mcs_eyepair_big.xml" ); if( mNoseDetector.empty() ) mNoseDetector.load("haarcascade_mcs_nose.xml" ); if( mMouthDetector.empty() ) mMouthDetector.load( "haarcascade_mcs_mouth.xml" ); //打开视频文件 //cv::VideoCapture capture("bike.avi"); //0 open default camera cv::VideoCapture capture(0); //检查视频是否打开 if(!capture.isOpened()) return 1; // 得到帧率 double rate= capture.get(CV_CAP_PROP_FPS); bool stop(false); cv::Mat frame; // 现在的视频帧 cv::Mat mElabImage;//备份frame图像 cv::namedWindow("Extracted Frame"); // 两帧之间的间隔时间 int delay= 1000/rate; // 循环播放所有的帧 while (!stop) { // 读下一帧 if (!capture.read(frame)) break; frame.copyTo( mElabImage ); //检测脸 //缩放因子 float scaleFactor = 3.0f; vector< cv::Rect > faceVec; mFaceDetector.detectMultiScale( frame, faceVec, scaleFactor ); int i, j; for( i=0; i<faceVec.size(); i++ ) { cv::rectangle( mElabImage, faceVec[i], CV_RGB(255,0,0), 2 ); cv::Mat face = frame( faceVec[i] ); //检测眼睛 vector< cv::Rect > eyeVec; mEyeDetector.detectMultiScale( face, eyeVec ); for( j=0; j<eyeVec.size(); j++ ) { cv::Rect rect = eyeVec[j]; rect.x += faceVec[i].x; rect.y += faceVec[i].y; cv::rectangle( mElabImage, rect, CV_RGB(0,255,0), 2 ); } //检测鼻子 vector< cv::Rect > noseVec; mNoseDetector.detectMultiScale( face, noseVec, 3 ); for( j=0; j<noseVec.size(); j++ ) { cv::Rect rect = noseVec[j]; rect.x += faceVec[i].x; rect.y += faceVec[i].y; cv::rectangle( mElabImage, rect, CV_RGB(0,0,255), 2 ); } //检测嘴巴 vector< cv::Rect > mouthVec; cv::Rect halfRect = faceVec[i]; halfRect.height /= 2; halfRect.y += halfRect.height; cv::Mat halfFace = frame( halfRect ); mMouthDetector.detectMultiScale( halfFace, mouthVec, 3 ); for( j=0; j<mouthVec.size(); j++ ) { cv::Rect rect = mouthVec[j]; rect.x += halfRect.x; rect.y += halfRect.y; cv::rectangle( mElabImage, rect, CV_RGB(255,255,255), 2 ); } } //在窗口中显示图像 cv::imshow("Extracted Frame",mElabImage); // 按任意键停止视频播放 //if (cv::waitKey(delay)>=0) // stop= true; cv::waitKey(20); } // 关闭视频文件 capture.release(); return 0; }
opencv2实现人脸眼鼻口检测_不大稳定
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。