2

輪郭を検出するために使用したコードは次のとおりです。

IplImage* DetectAndDrawQuads(IplImage* img)

{
CvSeq* contours;
CvSeq* result;
CvMemStorage *storage = cvCreateMemStorage(0);

IplImage* ret = cvCreateImage(cvGetSize(img), 8, 3);
IplImage* temp = cvCreateImage(cvGetSize(img), 8, 1);

cvCvtColor(img, temp, CV_BGR2GRAY);

cvFindContours(temp, storage, &contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));

while(contours)
{
    result = cvApproxPoly(contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.10, 0); //*0.2


    if((result->total) == 4)  
    {
        CvPoint *pt[4];
        for(int i=0;i<4;i++)
            pt[i] = (CvPoint*)cvGetSeqElem(result, i);

            cvLine(ret, *pt[0], *pt[1], cvScalar(255));
            cvLine(ret, *pt[1], *pt[2], cvScalar(255));
            cvLine(ret, *pt[2], *pt[3], cvScalar(255));
            cvLine(ret, *pt[3], *pt[0], cvScalar(255));


    }

    contours = contours->h_next;

}

cvReleaseImage(&temp);
cvReleaseMemStorage(&storage);

return ret;
}


int main()

{

    IplImage* img = cvLoadImage("D:\\Database\\eye2.jpg");
IplImage* contourDrawn = 0;
cvNamedWindow("original");
cvShowImage("original", img);

contourDrawn = DetectAndDrawQuads(img);
cvNamedWindow("contours");
cvShowImage("contours", contourDrawn);

cvWaitKey(0);
return 0;
}

そして、これはがプログラムをテストするために使用した写真です:入力

入力された顔の表情を見つけるための準備段階として、輪郭を取得しようとしています。そして、これはプログラムを実行しようとしたときの結果です (元の[左] と出力[右]): 結果

ご覧のとおり、バイナリ イメージにノイズが残っているようです (これは、輪郭検索プログラム (上記のコード) に入力する前に実際に前処理しました)。

私の質問は:



  1. 輪郭のポイントを見つける方法 (例: 上、下、中央、左端、右端 --> 顔の表情を決定するための幾何学的計算を行うための重要なポイント)。

あなたが私を助けてくれるなら、どうもありがとう。これまでのところ、これは輪郭の検索に関して生成できる最高の出力です。また、輪郭をより正確に抽出するのを手伝っていただければ、非常に感謝しています。ありがとうございました。:)

4

1 に答える 1

2
cvPoint rightMost = (0, 0); 
cvPoint leftMost = (gray->width, 0);
cvPoint bottom =  (0, gray->height);
cvPoint top = (0, 0);;   
for( CvSeq* current = contours; current != NULL; current = current->h_next )
{
    for( int i = 0; i < current->total; i++ )
    {
         CvPoint* pt = (CvPoint*)cvGetSeqElem( current, i );
         int pixVal = (int)(gray->imageData + pt->x * gray->widthStep)[pt->y];
        //find the point, which coordinate X is the biggest
         if( pt->x > rightMost ) 
         {
            rightMost->x = pt->x;  
            rightMost->y = pt->y;
         }  
        //find the point, which coordinate X is the smallest
         if( pt->x < leftMost ) 
         {
            leftMost->x = pt->x;  
            leftMost->y = pt->y;
         }  
        //find the point, which coordinate Y is the biggest
         if( pt->y > top )
         {
            top->x = pt->x;  
            top->y = pt->y;
         }    
        //find the point, which coordinate Y is the smallest
         if( pt->x < bottom ) 
         {
            bottom->x = pt->x;  
            bottom->y = pt->y;
         }  

    }
}
cvPoint ptCenter(cvRound(( rightMost + leftMost ) / 2), ( top + bottom ) / 2 );

このコードはまだ試していませんが、役に立つと思います!

于 2013-01-29T12:45:15.130 に答える