Filling holes in binary objects

This function fills the holes in binary objects. The following code block demonstrates the application of the function with different structuring element sizes on an input binary image:

from scipy.ndimage.morphology import binary_fill_holesim = rgb2gray(imread('../images/text1.png'))im[im <= 0.5] = 0im[im > 0.5] = 1pylab.figure(figsize=(20,15))pylab.subplot(221), pylab.imshow(im), pylab.title('original', size=20),pylab.axis('off')i = 2for n in [3,5,7]:    pylab.subplot(2, 2, i)    im1 = binary_fill_holes(im, structure=np.ones((n,n)))    pylab.imshow(im1), pylab.title('binary_fill_holes with structure square side ' + str(n), size=20)    pylab.axis('off')    i += 1pylab.show()

The following screenshot shows the output of the ...

Get Hands-On Image Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.