OpenCV填充孔给出白色图像

时间:2020-09-11 09:52:07

标签: python flood-fill opencv-python

我的目标是在图像中用白色像素填充黑洞。 例如,在这张图片上,我用红色指出了我想用白色填充的黑洞。

objective

我正在使用这段代码来完成它。

im_floodfill = im_in.copy()
h, w = im_floodfill.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)
cv2.floodFill(im_floodfill, mask, (0,0), 255)
im_floodfill_inv = cv2.bitwise_not(im_floodfill)
im_out = im_in | im_floodfill_inv

它适用于大多数图像,但有时会给出白色图像。

此输入示例:

im_in im_in

im_floodfill_inv im_floodfill_inv

im_out im_out

您能帮助我理解为什么有时候我有一个白色的im_out以及如何解决吗?

1 个答案:

答案 0 :(得分:1)

我使用了另一种方法,即在图像上找到轮廓,然后使用层次结构确定所找到的轮廓是否是子轮廓(它们内部没有孔/轮廓),然后使用这些轮廓填充孔。我使用了您在此处上传的屏幕截图,请下次尝试上传您正在使用的实际图像而不是屏幕截图。

add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
    // HERE define your product categories (can be IDs, slugs or names)
    $product_categories = array('cheese');

    // HERE define your products Ids to be excluded
    $excluded_ids = array(37, 57);

    if( has_term( $product_categories, 'product_cat', $product->get_id() ) 
    && ! in_array( $product->get_id(), $excluded_ids ) ) {
        $price .= ' ' . __('per kg');
    }
    
    return $price;
}

结果图像的填充区域的轮廓显示为红色:

enter image description here

您所显示的部分放大了

enter image description here