Filling holes in objects that touch the border of an image

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.

Answers


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


Need Your Help

On Idle Hide Mouse System-Wide, Show on Movement, from a C# Application

c# winforms mouse hook mouse-cursor

I'm trying to develop an application that, when running, will hide the mouse cursor wherever it happens to be on the screen after 5 seconds of inactivity, even if my app isn't in the foreground. Th...

Regular expression with brackets

php regex expression

How to get 'text' from this string: