gpt4 book ai didi

Python 相当于灰度的 Matlab 函数 'imfill'?

转载 作者:太空狗 更新时间:2023-10-30 01:00:17 35 4
gpt4 key购买 nike

是否有使用OpenCV或scikit-image的实现相当于Matlab的灰度图像imfill函数(即灰度孔填充)?

请参阅以下示例链接中的灰度填充部分 (I2= imfill(I)) matlab_imfill .或者看图片:matlab_tire_ex

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

tire

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

我正在使用 Python 3.5。

最佳答案

Matlab infill() 依次使用函数 IM = imreconstruct(marker,mask)

Scikit-image 有类似的功能... skimage.morphology.reconstruction(seed, mask, method='dilation', selem=None, offset=None)

算法详见Soille, P.,形态图像分析:原理和应用,Springer-Verlag,1999,第 208-209 页。第 6.3.7 节“填充孔”

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

关于Python 相当于灰度的 Matlab 函数 'imfill'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36294025/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com