Is Python equivalent to matlab funciton 'imfill' for grayscale?

Is there an implementation using OpenCV or a scikit image that is equivalent to the effect of Matlab grayscale image imfill (i.e. shading in grayscale)?

See the imfill section for grayscale ( I2 = imfill (I) ) in the following matlab_imfill example link, Or look at the image: matlab_tire_ex

Here is a link to the tire image in the example

tire

I am trying to replicate Matlab output using the scipy.ndimage.grey_closing function with changing the size parameter, but was not successful.

I am using Python 3.5.

+5
source share
2 answers

Python implements two versions of the fill-fill algorithm:

http://arcgisandpython.blogspot.de/2012/01/python-flood-fill-algorithm.html

The first, simpler one contains two undefined variables, but here is the 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 
+1
source

Matlab infill (), in turn, uses the 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 described in detail in Soille, P., Morphological Image Analysis: Principles and Applications, Springer-Verlag, 1999, pp. 208-209. section 6.3.7 section "Filling"

 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 tire image and filled image

+1
source

Source: https://habr.com/ru/post/1246020/


All Articles