2

我有一个黑白图像。我尝试消除噪音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 仍然存在。

我做错什么了吗?谢谢

在此处输入图像描述

4

1 回答 1

5

文档(强调我的):

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

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

期望 ar 是一个带有标签对象的数组,并删除小于 min_size 的对象。如果 ar 是 bool,则首先标记图像。这导致 bool 和 0 和 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)

阴谋

于 2019-03-08T09:47:20.593 回答