基于OpenCV harr级联分类器的人脸及眼睛、鼻子、嘴巴检测程序

#include 
#include 
#include 
#include 
#include 
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 eyeVec;
            mEyeDetector.detectMultiScale( face, eyeVec );

            for( j=0; j noseVec;

            mNoseDetector.detectMultiScale( face, noseVec, 3 );

            for( j=0; j 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=0)
        //    stop= true;
        cv::waitKey(20);
        }
    // 关闭视频文件
    capture.release();
    return 0;
    }


 

你可能感兴趣的:(计算机图形学,C/C++)