【问题标题】:remove a contour from image using skimage使用 skimage 从图像中删除轮廓
【发布时间】:2016-09-18 11:34:01
【问题描述】:

我想从图像中删除一些轮廓,但我不知道如何使用skimage 来实现它?我在OpenCV 中使用drawContour 做了类似的事情,但我在skimage 中找不到等价物。

假设我有一个简单的图像,例如:

0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0

0 0 1 0 0 1 0 0

0 0 1 1 1 1 0 0

0 0 0 0 0 0 0 0

只有一个连通分量。

我需要通过掩盖它来删除它。

最终结果将是一个 8 * 5 的零矩阵!

a = '''0 0 0 0 0 0 0 0                                             
0 0 1 1 1 1 0 0
0 0 1 0 0 1 0 0
0 0 1 1 1 1 0 0
0 0 0 0 0 0 0 0'''
np.array([int(i) for i in a.split()], dtype=bool).reshape(5, 8)
cc = measure.regionprops(measure.label(a))[0]
# here is what I do for removing cc

我应该怎么做才能使用skimage删除cc连接的组件?

【问题讨论】:

    标签: python image-processing scikit-image


    【解决方案1】:

    在这种情况下,您需要将图像转换为二进制图像(如您所愿),然后按照以下步骤操作:

    1 - 填充孔。

    2 - 识别轮廓。

    3 - 使用识别的轮廓创建黑色蒙版(false 或 0)。

    from skimage import measure, draw
    from scipy import ndimage as ndi
    
    
    # I use this function "ndi.distance_transform_edt(thresh)" to turn my image to a binary image
    def remove_contours(binary_image):
        binary_image = ndi.binary_fill_holes(binary_image)
        contours= measure.find_contours(binary_image, 0.9, fully_connected='high',
                                         positive_orientation='low')
        #Fill contours with False. (it could be 0 as well)
        for n, contour in enumerate(contours):
            draw.set_color(binary_image , draw.polygon(contour[:, 0], contour[:, 1]), False)
        return binary_image
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-23
      • 2020-06-14
      • 2011-08-18
      • 1970-01-01
      • 2021-06-16
      • 2021-12-21
      相关资源
      最近更新 更多