Python中与Matlab函数'imfill'相对应的灰度图像填充功能是什么?

6

是否有使用OpenCV或scikit-image实现的功能与Matlab的灰度图像imfill函数等效(即灰度孔填充)?

请参见以下示例链接matlab_imfill中的灰度(I2 = imfill(I))部分的imfill。或查看图像:matlab_tire_ex

这是示例中轮胎图像的链接

tire

我一直在尝试使用 scipy.ndimage.grey_closing 函数来复制 Matlab 输出,但是通过改变大小参数并没有成功。
我正在使用 Python 3.5。

请查看此链接,以查看其Python实现。 - Billal Begueradj
2
@BillBEGUERADJ 这只适用于二值图像。OP 想要对灰度图像进行操作。 - rayryeng
cv2.floodfill() 或许是你想要的。 - fmw42
2个回答

8

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"一书第6.3.7节"Fillhole"部分,Springer-Verlag,1999年,第208-209页。

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

链接到轮胎图片和加气的图片


大致相同的想法在scikit image文档的示例中传达。 - jadelord

3

这里使用Python实现了两个版本的泛洪填充算法:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接