OpenCV 正面臉部識別代碼

OpenCV 正面臉部識別代碼:


//***************************************
/**

*@brief fnDetectFaceObj 正面臉部識別
*@parma filenameNorm 給定正面證件照原始文件
*/

void fnDetectFaceObj(AnsiString filenameNorm, S3DPos &p3LeftEyePos, S3DPos &p3RightEyePos, S3DPos &p3NosePos, S3DPos &p3MousePos)
{
    static CvScalar colors[] =
    {
        {{0,0,255}},
        {{0,128,255}},
        {{0,255,255}},
        {{0,255,0}},
        {{255,128,0}},
        {{255,255,0}},
        {{255,0,0}},
        {{255,0,255}}
    };

    IplImage* img1 = cvLoadImage( filenameNorm.c_str());
    CvSize sz;
    float scale1 =(float) ((float)MAX_WIDTH / img1->width);
    sz.width = img1->width*scale1;
    sz.height = img1->height*scale1;
    IplImage* img = cvCreateImage(sz,img1->depth,img1->nChannels);
    cvResize(img1,img,CV_INTER_CUBIC);

    CvMemStorage* storage =  cvCreateMemStorage(0);
    CvHaarClassifierCascade* cascadeEye = 0;
    CvHaarClassifierCascade* cascadeNose = 0;
    CvHaarClassifierCascade* cascadeMouse = 0;

    //    AnsiString cascade_name =ExtractFilePath(ParamStr(0))+ "haarcascade_frontalface_default.xml";     //臉正面還ok
    AnsiString cascade_mouse =ExtractFilePath(ParamStr(0))+ "haarcascade_mcs_mouth.xml";               //大概還能用, 要判斷位置及大小
    AnsiString cascade_nose =ExtractFilePath(ParamStr(0))+ "haarcascade_mcs_nose.xml";
    AnsiString cascade_eye =ExtractFilePath(ParamStr(0))+ "haarcascade_mcs_lefteye.xml";

    cascadeEye = (CvHaarClassifierCascade*)cvLoad( cascade_eye.c_str(), 0, 0, 0 );
    cascadeNose = (CvHaarClassifierCascade*)cvLoad( cascade_nose.c_str(), 0, 0, 0 );
    cascadeMouse = (CvHaarClassifierCascade*)cvLoad( cascade_mouse.c_str(), 0, 0, 0 );

    double scale = 1.3;
    int i;
    IplImage* gray = cvCreateImage( cvSize(img->width,img->height),IPL_DEPTH_8U, 1 );
    IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),
                         cvRound (img->height/scale)),
                     8, 1 );
    cvCvtColor( img, gray, CV_BGR2GRAY );
    cvResize( gray, small_img, CV_INTER_LINEAR );
    cvEqualizeHist( small_img, small_img );
    cvClearMemStorage( storage );

    int iEyeNo = 0;
    S3DPos p3Eye[2];

    p3Eye[0].x = -1;
    p3Eye[1].x = -1;
    p3LeftEyePos.x = -1;
    p3RightEyePos.x = -1;
    p3NosePos.x = -1;
    p3MousePos.x = -1;

    if( cascadeEye )
    {
        CvSeq* faces1 = cvHaarDetectObjects( small_img, cascadeEye, storage,
                                            1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/,
                                            cvSize(30, 30) );

        for( i = 0; i < (faces1 ? faces1->total : 0); i++ )
        {
            CvRect* r = (CvRect*)cvGetSeqElem( faces1, i );
            CvPoint center;
            int radius;
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.25*scale);
            if ( (center.y>img->height/5) && (center.y<img->height*3/5) && (radius<img->height/5) )
            {
                p3Eye[iEyeNo].x = center.x;
                p3Eye[iEyeNo].y = center.y;
                p3Eye[iEyeNo].z = radius;

                iEyeNo++;
                if (iEyeNo>=2)
                    break;
            }
        }
        delete faces1;
        if ((p3Eye[0].x!=-1) && (p3Eye[1].x != -1))
        {
              if (p3Eye[0].x < p3Eye[1].x)
              {
                  p3LeftEyePos = p3Eye[0];
                  p3RightEyePos = p3Eye[1];
              }
              else
              {
                  p3LeftEyePos = p3Eye[1];
                  p3RightEyePos = p3Eye[0];
              }
        }
    }
    if( cascadeNose )
    {
        CvSeq* faces2 = cvHaarDetectObjects( small_img, cascadeNose, storage,
                                            1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/,
                                            cvSize(30, 30) );

        for( i = 0; i < (faces2 ? faces2->total : 0); i++ )
        {
            CvRect* r = (CvRect*)cvGetSeqElem( faces2, i );
            CvPoint center;
            int radius;
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.25*scale);
           if ( (center.y>img->height*2/5) && (center.y<img->height*4/5)&& (radius<img->height/5))
           {
               p3NosePos.x = center.x;
               p3NosePos.y = center.y;
               p3NosePos.z = radius;
               break;
           }
        }
        delete faces2;

    }

    if( cascadeMouse )
    {
        CvSeq* faces3 = cvHaarDetectObjects( small_img, cascadeMouse, storage,
                                            1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/,
                                            cvSize(30, 30) );

        for( i = 0; i < (faces3 ? faces3->total : 0); i++ )
        {
            CvRect* r = (CvRect*)cvGetSeqElem( faces3, i );
            CvPoint center;
            int radius;
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.25*scale);
            if ((center.y>img->height*3/5) && (radius<img->height/5))
            {
                 p3MousePos.x = center.x;
                 p3MousePos.y = center.y;
                 p3MousePos.z = radius;
                 break;
            }
        }
        delete faces3;
    }
    delete cascadeMouse;
    delete cascadeNose;
    delete cascadeEye;

    cvReleaseImage( &img1 );
    cvReleaseImage( &img );

    cvReleaseImage( &gray );
    cvReleaseImage( &small_img );
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值