补洞寻峰

我们使用腐蚀形态重建来填充图像中的空洞(即孤立的黑点)。侵蚀会扩展种子图像的最小值,直到它遇到遮罩图像。因此,种子图像和掩模图像表示重建图像的最大和最小可能值。

我们从一张既包含峰又包含洞的图像开始:

import matplotlib.pyplot as plt

from skimage import data
from skimage.exposure import rescale_intensity

image = data.moon()
# Rescale image intensity so that we can see dim features.
image = rescale_intensity(image, in_range=(50, 200))

现在我们需要创建种子图像,其中的最小值表示侵蚀的起点。为了填充空洞,我们将种子图像初始化为原始图像的最大值。然而,在边框上,我们使用图像的原始值。这些边界像素将成为侵蚀过程的起点。然后,我们通过将蒙版设置为原始图像的值来限制侵蚀。

import numpy as np
from skimage.morphology import reconstruction

seed = np.copy(image)
seed[1:-1, 1:-1] = image.max()
mask = image

filled = reconstruction(seed, mask, method='erosion')

如上所述,从边缘向内侵蚀可以去除孔洞,因为(根据定义)孔洞被更亮的像素包围。最后,我们可以通过从原始图像中减去重建图像来分离暗区域。

或者,我们可以使用膨胀的形态重建来发现图像中的亮点。膨胀是侵蚀的反面,它使 最大 种子图像的值,直到它遇到遮罩图像。因为这是一个逆操作,所以我们将种子图像初始化为最小图像强度而不是最大图像强度。该过程的其余部分是相同的。

seed = np.copy(image)
seed[1:-1, 1:-1] = image.min()
rec = reconstruction(seed, mask, method='dilation')

fig, ax = plt.subplots(2, 2, figsize=(5, 4), sharex=True, sharey=True)
ax = ax.ravel()

ax[0].imshow(image, cmap='gray')
ax[0].set_title('Original image')
ax[0].axis('off')

ax[1].imshow(filled, cmap='gray')
ax[1].set_title('after filling holes')
ax[1].axis('off')

ax[2].imshow(image-filled, cmap='gray')
ax[2].set_title('holes')
ax[2].axis('off')

ax[3].imshow(image-rec, cmap='gray')
ax[3].set_title('peaks')
ax[3].axis('off')
plt.show()
Original image, after filling holes, holes, peaks

脚本的总运行时间: (0分0.258秒)

Gallery generated by Sphinx-Gallery