OpenCV教程(41) 人脸特征检测

      在OpenCV中,自带着Harr分类器人脸特征训练的文件,利用这些文件,我们可以很方面的进行人脸,眼睛,鼻子,表情等的检测。

     人脸特征文件目录: ../opencv2.46/opencv/data/haarcascades

人脸检测Harr分类器的介绍:http://www.cnblogs.com/mikewolf2002/p/3437883.html

分类器的训练步骤:http://note.sonots.com/SciSoftware/haartraining.html

本文中,我们通过代码了解一下在OpenCV中如何通过harr分类器进行人脸特征检测。

#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;
}

程序运行效果:

 

image

代码文件:工程FirstOpenCV36

你可能感兴趣的:(opencv)