Filling holes in objects that touch the border of an image

不羁的心 提交于 2020-01-12 04:58:08

问题


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

When I use SciPy's binary_fill_holes(), I am generally successful, with the exception of objects that touch the image's border.

Are there any existing Python functions that can fill holes in objects that touch the border? I tried adding a white border around the image, but that just resulted in the entire image being filled.


回答1:


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()



来源:https://stackoverflow.com/questions/22310489/filling-holes-in-objects-that-touch-the-border-of-an-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!