//***************************************
/*
*
*@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(), 000 );
    cascadeNose 
= (CvHaarClassifierCascade*)cvLoad( cascade_nose.c_str(), 000 );
    cascadeMouse 
= (CvHaarClassifierCascade*)cvLoad( cascade_mouse.c_str(), 000 );

    
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)),
                     
81 );
    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.120/*CV_HAAR_DO_CANNY_PRUNING*/,
                                            cvSize(
3030) );

        
for( i = 0; i < (faces1 ? faces1->total : 0); i++ )
        {
            CvRect
* r = (CvRect*)cvGetSeqElem( faces1, i );
            CvPoint center;
            
int radius;
            center.x 
= cvRound((r->+ r->width*0.5)*scale);
            center.y 
= cvRound((r->+ 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.120/*CV_HAAR_DO_CANNY_PRUNING*/,
                                            cvSize(
3030) );

        
for( i = 0; i < (faces2 ? faces2->total : 0); i++ )
        {
            CvRect
* r = (CvRect*)cvGetSeqElem( faces2, i );
            CvPoint center;
            
int radius;
            center.x 
= cvRound((r->+ r->width*0.5)*scale);
            center.y 
= cvRound((r->+ 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.120/*CV_HAAR_DO_CANNY_PRUNING*/,
                                            cvSize(
3030) );

        
for( i = 0; i < (faces3 ? faces3->total : 0); i++ )
        {
            CvRect
* r = (CvRect*)cvGetSeqElem( faces3, i );
            CvPoint center;
            
int radius;
            center.x 
= cvRound((r->+ r->width*0.5)*scale);
            center.y 
= cvRound((r->+ 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 );
}

 

posted on 2011-08-22 16:33  Steven  阅读(378)  评论(0编辑  收藏  举报