簡體   English   中英

OpenCV找到最大的輪廓?

[英]OpenCV find biggest contours?

我有一個圖像,在上面使用了cvFindContours()-將結果存儲在imageContours中。 然后,我試圖找到兩個最大的輪廓並僅顯示它們。 但是由於某種原因,它的顯示方式超過了2個輪廓。 我的算法有什么問題? 謝謝

編輯:哦,還有,在控制台中,如果沒有一個選項為真,我會不斷收到“錯誤”消息,該消息是我在下面編寫的。 有人知道為什么嗎?

cvFindContours (binMask, imageContoursMem, &imageContours, sizeof (CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); //find the contours in binMask, and store results in ImageContours

    //following conditions used to set f_handContour to the bigger contour for the first time, and s_handContour to the smaller contour for the first time
    if ( (cvContourArea (imageContours, CV_WHOLE_SEQ)) > (cvContourArea((imageContours -> h_next), CV_WHOLE_SEQ)))
    { //begin first contour bigger condition

        f_handContour = imageContours;
        s_handContour = (imageContours -> h_next);

    } //end second contour bigger condition

    else if ( (cvContourArea (imageContours, CV_WHOLE_SEQ)) < (cvContourArea((imageContours -> h_next), CV_WHOLE_SEQ)))
    { //begin second contour bigger condition

        f_handContour = (imageContours -> h_next);
        s_handContour = imageContours;

    } //begin second contour biggger condition

    else if  ( (cvContourArea (imageContours, CV_WHOLE_SEQ)) == (cvContourArea((imageContours -> h_next), CV_WHOLE_SEQ)))
    { //begin contours equal condition

        f_handContour = imageContours; //if contours equal assignment of contours doesn't matter
        s_handContour = (imageContours -> h_next);

    } //end contours equal condition

    else
    { //begin error condition

        cout<<"Error";

    } //end error condition

    while ((imageContours -> h_next) != NULL)
    { //start of biggest/second biggest contour recognition loop

        imageContours = (imageContours -> h_next);

        if ( cvContourArea (f_handContour, CV_WHOLE_SEQ) < cvContourArea (imageContours, CV_WHOLE_SEQ))
        { //start of next contour bigger than f_handContour condition

            s_handContour = f_handContour;
            f_handContour = imageContours;

        } //end start of next contour bigger than f_handContour condition

        else if ( cvContourArea (f_handContour, CV_WHOLE_SEQ) > cvContourArea (imageContours, CV_WHOLE_SEQ))
        { //start of next contour smaller than f_handContour condition

            if ( cvContourArea (s_handContour, CV_WHOLE_SEQ) < cvContourArea (imageContours, CV_WHOLE_SEQ))
            { //startof next contour bigger than s_handContour condition

              s_handContour = imageContours;

            } //end of next contour bigger than s_handContour condition

            else if ( cvContourArea (s_handContour, CV_WHOLE_SEQ) > cvContourArea (imageContours, CV_WHOLE_SEQ))
            { //start of next contour smaller than s_handContour condition

                //just pass on contour

            } //end of next contour smaller than s_handContour condition

            else if ( cvContourArea (s_handContour, CV_WHOLE_SEQ) == cvContourArea (imageContours, CV_WHOLE_SEQ))
            { //start of next contour equal to s_handContour condition

                //just pass on contour

            } // end of next contour equal to s_handContour condition

            else
            { //start of error condition

                cout<<"Error";

            } //end of error condition

        } //end of next contour smaller than f_handContour Condition

        else if ( cvContourArea (f_handContour, CV_WHOLE_SEQ) == cvContourArea (imageContours, CV_WHOLE_SEQ))
        { //start of next contour equal to f_handContour condition

            if ( cvContourArea (s_handContour, CV_WHOLE_SEQ) < cvContourArea (imageContours, CV_WHOLE_SEQ))
            { //startof next contour bigger than s_handContour condition

              s_handContour = imageContours;

            } //end of next contour bigger than s_handContour condition

            else
            { //start of error condition

                cout<<"Error";

            } //end of error condition

        } //end of next contour equal to f_handContour condition

        else
        { //start of error condition

            cout<<"Error";

        } //end of error condition

    } //end of biggest/second biggest contour recognition loop

    cvDrawContours (output, f_handContour, cvScalar (0,255,0), cvScalar (0,255,255), 1, 3,8, cvPoint (0,0)); //draws the first hand contour derived from binMask on ouput
    cvDrawContours (output, s_handContour, cvScalar (0,255,0), cvScalar (0,255,255), 1, 3,8, cvPoint (0,0)); //draws the second hand contour derived from binMask on ouput

CV_RETR_EXTERNAL僅檢索極端的外部輪廓。 對於您的算法,您應該使用CV_RETR_LIST。 嘗試將cvDrawContours函數的值0用作max_level,因為值1繪制當前輪廓以及與之處於同一水平的所有其他輪廓。 另外,您可以優化條件(有點混亂),還可以將區域存儲在一些局部變量中,以免調用cvContourArea太多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM