是否有使用OpenCV或scikit-image的实现等效于Matlab的灰度图像imfill函数(即灰度孔填充)?
有关灰度(I2 = imfill(I))的imfill部分,请参阅以下示例链接matlab_imfill。或者看图像:matlab_tire_ex
这是示例中轮胎图像的链接
我一直在尝试使用scipy.ndimage.grey_closing函数复制Matlab输出,并改变大小参数,但是没有成功。
我正在使用Python 3.5。
这里有两个版本的泛洪填充算法:
http://arcgisandpython.blogspot.de/2012/01/python-flood-fill-algorithm.html
第一个,更简单的一个包含两个未定义的变量,但这是一个工作版本:
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
Matlab infill()依次使用函数IM = imreconstruct(marker,mask)
Scikit-image具有类似的功能...... skimage.morphology.reconstruction(seed, mask, method='dilation', selem=None, offset=None)
该算法在Soille,P.,Morphological Image Analysis:Principles and Applications,Springer-Verlag,1999,pp.208-209中有详细描述。第6.3.7节“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()