人臉檢測的harr檢測函式

wishchin發表於2013-07-17

眼球追蹤需要對人臉進行識別,然後再對人眼進行識別,判斷人眼張合度,進而判斷疲勞...

解析:人臉檢測的harr檢測函式使用方法

程式碼理解:

       利用訓練集,檢測出臉部,畫出框

void CAviTestDlg::HaarFaceDetect( IplImage* TheImage, CvBox2D* faceBox)
{
    if( !cascade ) {  return ;  }
            storage = cvCreateMemStorage(0);
            int scale = 1;   int i;
            IplImage* temp = cvCreateImage( cvSize(TheImage->width/scale,TheImage->height/scale), 8, 3 );//多餘!
            CvPoint pt1, pt2;

           if( cascade ){
                CvSeq* faces = cvHaarDetectObjects( TheImage, cascade, storage,
                    1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
                    cvSize(40, 40) );
                for( i = 0; i < (faces ? faces->total : 0); i++ ){
                    CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
                    pt1.x = r->x*scale;
                    pt2.x = (r->x+r->width)*scale;
                    pt1.y = r->y*scale;
                    pt2.y = (r->y+r->height)*scale;
                    //cvRectangle( TheImage, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );// 畫矩形,P1,P2,矩形對角線上的兩個頂點。。。
                }

                if ( faces->total == 0 ) { MessageBox("人臉檢測丟失,請重新載入","人臉檢測丟失丟失",0);  return;}//
          
                else {
            faceBox->center.x = (float)(pt1.x+pt2.x)/2.0;
            faceBox->center.y = (float)(pt1.y+pt2.y)/2;
            faceBox->size.width = (float)(pt2.x - pt1.x);
            faceBox->size.height = (float)(pt2.y - pt1.y);
                }
            }
            cvReleaseImage( &temp );

}


相關文章