【问题标题】:Failed to remove noise by remove_small_objects无法通过 remove_small_objects 去除噪音
【发布时间】:2019-07-30 02:05:33
【问题描述】:

我有一张黑白图像。我尝试通过remove_small_objects 消除噪音。

import cv2 as cv
import numpy as np
from skimage import morphology

img = np.array([[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
                [255, 255,   0, 255,   0,   0,   0,   0, 255, 255, 255],
                [255, 255, 255, 255,   0,   0,   0,   0, 255,   0,   0],
                [255, 255,   0,   0,   0,   0,   0,   0,   0,   0,   0],
                [255, 255,   0,   0,   0,   0,   0, 255,   0,   0,   0],
                [255, 255,   0,   0,   0,   0,   0,   0,   0,   0,   0],
                [255, 255, 255,   0,   0,   0,   0,   0,   0,   0,   0],
                [255, 255,   0,   0,   0,   0,   0,   0,   0,   0,   0],
                [255, 255,   0,   0,   0,   0,   0,   0,   0,   0,   0]])

cleaned = morphology.remove_small_objects(img, min_size=10, connectivity=1)
print(cleaned)

while True:
    cv.imshow('Demo', cleaned.astype(np.uint8))
    if cv.waitKey(1) & 0xFF == 27:
        break

cv.destroyAllWindows()

但是,它并没有像我预期的那样工作。中间的白色像素255还在。

我做错了吗?谢谢

【问题讨论】:

    标签: python opencv image-processing scikit-image image-morphology


    【解决方案1】:

    来自docs(强调我的):

    skimage.morphology.remove_small_objects(ar, min_size=64, connectivity=1, in_place=False)

    删除小于指定大小的对象。

    期望 ar 是一个带有标签对象的数组,并删除小于 min_size 的对象。 如果 ar 为 bool,则首先标记图像。这会导致 bool 和 0-and-1 数组的行为可能不同。

    import numpy as np
    from skimage import io, morphology
    import matplotlib.pyplot as plt
    
    img = np.array([[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
                    [255, 255,   0, 255,   0,   0,   0,   0, 255, 255, 255],
                    [255, 255, 255, 255,   0,   0,   0,   0, 255,   0,   0],
                    [255, 255,   0,   0,   0,   0,   0,   0,   0,   0,   0],
                    [255, 255,   0,   0,   0,   0,   0, 255,   0,   0,   0],
                    [255, 255,   0,   0,   0,   0,   0,   0,   0,   0,   0],
                    [255, 255, 255,   0,   0,   0,   0,   0,   0,   0,   0],
                    [255, 255,   0,   0,   0,   0,   0,   0,   0,   0,   0],
                    [255, 255,   0,   0,   0,   0,   0,   0,   0,   0,   0]])
    
    arr = img > 0
    cleaned = morphology.remove_small_objects(arr, min_size=2)
    cleaned = morphology.remove_small_holes(cleaned, min_size=2)
    
    fig, axs = plt.subplots(1, 2)
    axs[0].imshow(img, cmap='gray')
    axs[0].set_title('img')
    axs[1].imshow(cleaned, cmap='gray')
    axs[1].set_title('cleaned')
    plt.show(fig)
    

    【讨论】:

    • 有效!我想知道为什么当 ndarray 是 int 数组时,它不起作用.. 文档说它可以是 arbitrary shape, int or bool type. The array containing the objects of interest. If the array type is int, the ints must be non-negative.
    • 重点是如果数组是bool的,就先标注。如果它是 int,那么每个值都被认为是一个不同的对象。在这种情况下,该函数假定小 blob 仍然是名为“255”的对象的一部分。除了转换为布尔值,您还可以在图像上运行skimage.measure.label(或scipy.ndimage.label),然后将其用作输入。
    猜你喜欢
    • 2015-07-10
    • 1970-01-01
    • 2015-09-16
    • 1970-01-01
    • 2023-03-15
    • 2018-07-15
    • 2018-07-18
    • 2012-03-01
    • 2020-05-03
    相关资源
    最近更新 更多