【问题标题】:OpenCV find biggest contours?OpenCV找到最大的轮廓?
【发布时间】:2011-11-27 08:21:28
【问题描述】:

我有一个图像,我在其上使用了 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

【问题讨论】:

    标签: c++ opencv video image-recognition


    【解决方案1】:

    CV_RETR_EXTERNAL 仅检索极端外部轮廓。 对于您的算法,您应该使用 CV_RETR_LIST。 尝试将值 0 用作 cvDrawContours 函数的 max_level,因为值 1 绘制当前轮廓以及与其处于同一级别的所有其他轮廓。 你也可以优化你的条件(有点乱),也可以将区域存储在一些局部变量中,以免调用 cvContourArea 这么多。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-14
      • 1970-01-01
      • 2016-12-10
      • 2019-07-04
      • 1970-01-01
      • 2021-09-01
      • 2017-04-06
      • 1970-01-01
      相关资源
      最近更新 更多