【问题标题】:OpenCV: The result of findContours cannot be filledOpenCV:findContours的结果无法填充
【发布时间】:2020-09-10 12:10:40
【问题描述】:

我使用OpenCV和findContours的方法来寻找可以填充的多边形的轮廓。基本上结果图像应该看起来与输入相同。我提供的图像已经是黑白的,所以不需要转换为灰度。

我遇到的问题是findContours 的结果不能总是被填充。 drawContours 可以绘制漂亮的轮廓,但厚度 = FILLED 仅在某些时候有效(这意味着它在给出的示例中不起作用,但它可以在类似的输入上起作用)。最初模糊图像,增加成功的机会,但它仍然不是一个非常可靠的解决方案。

// blur( src_gray, src_gray, Size(2,2) ); - blurring an image with different kernel sizes might help sometimes

Canny( src_gray, canny_output, thresh, thresh*2, 3 );
findContours( canny_output, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0) );

Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
for(int i = 0; i <contours.size(); i++ ) {
    drawContours( drawing, contours, i, Scalar(255, 255, 255), FILLED, 8);
}

在使用 BOOST 库将结果轮廓转换为多边形后,我尝试验证结果轮廓有什么问题,它告诉我Geometry has invalid self-intersections 或它有spikes。在这种情况下,它无法更正。

假设只绘制轮廓真的很好,有没有更好的解决方案可以给我填充形状?

【问题讨论】:

    标签: c++ opencv contour


    【解决方案1】:

    如果将 drawContours() 函数中的 i 变量替换为 -1,则可以得到一个填充的多边形。你必须删除循环。

    Canny( src_gray, canny_output, thresh, thresh*2, 3 );
    findContours( canny_output, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0) );
    
    Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
    drawContours( drawing, contours, -1, Scalar(255, 255, 255), FILLED, 8);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-01
      • 2017-08-26
      • 2016-04-03
      • 2019-07-28
      • 1970-01-01
      • 2020-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多