【发布时间】:2021-10-24 22:04:33
【问题描述】:
我正在尝试从高速摄像机图像中提取悬浮在玻璃棒上的燃烧液滴的直径。因此,我使用 skimage 库中的几个函数将原始图像分解为具有最重要数据的二进制图像。典型的图像如下所示:
典型的缩小图像
为了区分玻璃棒和水滴,我想使用分水岭算法,效果很好。我使用this 代码这样做,但距离图没有显示:
import numpy as np
from skimage.morphology import watershed
from skimage.feature import peak_local_max
from skimage import measure
from skimage.segmentation import random_walker
import matplotlib.pyplot as plt
from scipy import ndimage
# Generate an initial image with two overlapping circles
x, y = np.indices((80, 80))
x1, y1, x2, y2 = 28, 28, 44, 52
r1, r2 = 16, 20
mask_circle1 = (x - x1) ** 2 + (y - y1) ** 2 < r1 ** 2
mask_circle2 = (x - x2) ** 2 + (y - y2) ** 2 < r2 ** 2
image = np.logical_or(mask_circle1, mask_circle2)
# Now we want to separate the two objects in image
# Generate the markers as local maxima of the distance
# to the background
distance = ndimage.distance_transform_edt(image)
local_maxi = peak_local_max(
distance, indices=False, footprint=np.ones((3, 3)), labels=image)
markers = measure.label(local_maxi)
labels_ws = watershed(-distance, markers, mask=image)
markers[~image] = -1
labels_rw = random_walker(image, markers)
plt.figure(figsize=(12, 3.5))
plt.subplot(141)
plt.imshow(image, cmap='gray', interpolation='nearest')
plt.axis('off')
plt.title('image')
plt.subplot(142)
plt.imshow(-distance, interpolation='nearest')
plt.axis('off')
plt.title('distance map')
plt.subplot(143)
plt.imshow(labels_ws, cmap='nipy_spectral', interpolation='nearest')
plt.axis('off')
plt.title('watershed segmentation')
plt.subplot(144)
plt.imshow(labels_rw, cmap='nipy_spectral', interpolation='nearest')
plt.axis('off')
plt.title('random walker segmentation')
plt.tight_layout()
plt.show()
但是,分割按预期工作。它产生以下输出:
输出
它还会产生以下错误:
> <ipython-input-42-514af2735e9b>:20: FutureWarning: indices argument is
> deprecated and will be removed in version 0.20. To avoid this warning,
> please do not use the indices argument. Please see peak_local_max
> documentation for more details. local_maxi = peak_local_max(
> C:\ProgramData\Anaconda3\lib\site-packages\skimage\morphology\_deprecated.py:5:
> skimage_deprecation: Function ``watershed`` is deprecated and will be
> removed in version 0.19. Use ``skimage.segmentation.watershed``
> instead. def watershed(image, markers=None, connectivity=1,
> offset=None, mask=None,
> C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\image.py:449:
> RuntimeWarning: overflow encountered in double_scalars newmin = vmid
> - dv * fact
> C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\image.py:454:
> RuntimeWarning: overflow encountered in double_scalars newmax = vmid
> + dv * fact
> C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\image.py:506:
> RuntimeWarning: invalid value encountered in multiply A_resampled *=
> ((a_max - a_min) / frac)
> C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\image.py:507:
> RuntimeWarning: invalid value encountered in multiply vrange *=
> ((a_max - a_min) / frac)
第二个和后面的错误很容易解决,我发现第一个更难解决。 为了解决有关 peak_local_max 函数中的 indices 参数的第一个错误,我偶然发现了 scikit-skimage github 页面上的 this 对话。他们替换了函数本身中的两行,但这并没有解决我的问题。变化来自
if indices:
return coordinates
else:
out = np.zeros_like(image, dtype=np.bool)
out[tuple(coordinates.T)] = True
return out
到
if indices:
return coordinates
else:
mask = np.zeros(im.shape, dtype=bool)
mask[tuple(indices.T)] = True
return mask
我在 skimage 本身的另一个 example 中发现了相同的两行,但这对我也不起作用。我还尝试将缩小的图像从二进制转换为浮点,结果相同。
我觉得有一个非常简单的解决方法,我只是太笨了,无法自己完成。
我使用的是 skimage 0.18.2 版和 JupyterLab 6.3.0 版。
提前感谢您的帮助!
【问题讨论】:
标签: python scikit-image euclidean-distance indices