OpenCV находит самые большие контуры? - PullRequest
1 голос
/ 29 сентября 2011

У меня есть изображение, на котором я использовал 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

1 Ответ

1 голос
/ 30 сентября 2011

CV_RETR_EXTERNAL извлекает только экстремальные внешние контуры. Для вашего алгоритма вы должны использовать CV_RETR_LIST. Попробуйте использовать как max_level для функции cvDrawContours значение 0, так как значение 1 рисует текущий контур и все остальные, которые находятся на одном уровне с ним. Также вы можете много оптимизировать свои условия (что-то вроде беспорядка), а также сохранять области в некоторых локальных переменных, чтобы не вызывать cvContourArea так много.

...