Filling holes in objects that touch the border of an image

后端 未结 1 1316
逝去的感伤
逝去的感伤 2021-02-08 06:58

I\'m trying to fill holes in the below image.

\"image

When I use SciPy\'s binary_fill_holes

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-08 07:19

    This assumes that there is more background than other stuff. It basically does a connected component analysis on the image. Extract the largest component (assumed to be the background), and sets everything else to white.

    import numpy as np
    import matplotlib.pyplot as plt
    import skimage.morphology, skimage.data
    
    img = skimage.data.imread('j1ESv.png', 1)
    labels = skimage.morphology.label(img)
    labelCount = np.bincount(labels.ravel())
    background = np.argmax(labelCount)
    img[labels != background] = 255
    plt.imshow(img, cmap=plt.cm.gray)
    plt.show()
    

    enter image description here

    0 讨论(0)
提交回复
热议问题