Is there an implementation using OpenCV or scikit-image that is equivalent to Matlab's grayscale image imfill funciton (i.e. grayscale hole filling)?
See the imfill section for grayscale (I2= imfill(I)) in the following example link matlab_imfill. Or see image: matlab_tire_ex
Here's a link to the tire image in the example
I've been trying to replicate the Matlab output using scipy.ndimage.grey_closing function with varying the size parameter, but have not been successful.
I'm using Python 3.5.
Matlab infill() in turn uses a function IM = imreconstruct(marker,mask)
Scikit-image has a similar function... skimage.morphology.reconstruction(seed, mask, method='dilation', selem=None, offset=None)
The algorithm is detailed in Soille, P., Morphological Image Analysis: Principles and Applications, Springer-Verlag, 1999, pp. 208-209. section 6.3.7 section "Fillhole"
import numpy as np
from skimage.morphology import reconstruction
import matplotlib.pyplot as plt
from skimage.io import imread, imsave
# Use the matlab reference Soille, P., Morphological Image Analysis: Principles and Applications, Springer-Verlag, 1999, pp. 208-209.
# 6.3.7 Fillhole
# The holes of a binary image correspond to the set of its regional minima which
# are not connected to the image border. This definition holds for grey scale
# images. Hence, filling the holes of a grey scale image comes down to remove
# all minima which are not connected to the image border, or, equivalently,
# impose the set of minima which are connected to the image border. The
# marker image 1m used in the morphological reconstruction by erosion is set
# to the maximum image value except along its border where the values of the
# original image are kept:
img = imread("tyre.jpg")
seed = np.ones_like(img)*255
img[ : ,0] = 0
img[ : ,-1] = 0
img[ 0 ,:] = 0
img[ -1 ,:] = 0
seed[ : ,0] = 0
seed[ : ,-1] = 0
seed[ 0 ,:] = 0
seed[ -1 ,:] = 0
fill = reconstruction(seed, img, method='erosion')
f, (ax0, ax1) = plt.subplots(1, 2,
subplot_kw={'xticks': [], 'yticks': []},
figsize=(12, 8))
ax0.imshow(img)
ax1.imshow(fill)
plt.show()
Link to tyre image and filled image
Two versions of the flood-fill algorithm have been implemented in Python here:
http://arcgisandpython.blogspot.de/2012/01/python-flood-fill-algorithm.html
The first, simpler one contained two undefined variables, but here is a working version:
import numpy as np
import scipy as sp
import scipy.ndimage
def flood_fill(test_array,h_max=255):
input_array = np.copy(test_array)
el = sp.ndimage.generate_binary_structure(2,2).astype(np.int)
inside_mask = sp.ndimage.binary_erosion(~np.isnan(input_array), structure=el)
output_array = np.copy(input_array)
output_array[inside_mask]=h_max
output_old_array = np.copy(input_array)
output_old_array.fill(0)
el = sp.ndimage.generate_binary_structure(2,1).astype(np.int)
while not np.array_equal(output_old_array, output_array):
output_old_array = np.copy(output_array)
output_array = np.maximum(input_array,sp.ndimage.grey_erosion(output_array, size=(3,3), footprint=el))
return output_array
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With