形态学蛇#

形态学蛇 [1] 是一组用于图像分割的方法。它们的行为类似于活动轮廓(例如,测地活动轮廓 [2]无边缘活动轮廓 [3])。然而,形态学蛇 使用二值数组上的形态学算子(如膨胀或腐蚀),而不是在浮点数组上求解偏微分方程,这是活动轮廓的标准方法。这使得 形态学蛇 比传统的对应方法更快且数值上更稳定。

在这个实现中有两种 形态学蛇 方法可用:形态学测地活动轮廓 (MorphGAC,在函数 morphological_geodesic_active_contour 中实现) 和 无边缘的形态学活动轮廓 (MorphACWE,在函数 morphological_chan_vese 中实现)。

MorphGAC 适用于具有可见轮廓的图像,即使这些轮廓可能嘈杂、杂乱或部分不清晰。然而,它要求对图像进行预处理以突出轮廓。这可以通过使用 inverse_gaussian_gradient 函数来完成,尽管用户可能希望定义自己的版本。MorphGAC 分割的质量在很大程度上取决于这一预处理步骤。

相反,MorphACWE 在待分割对象的内部和外部区域的像素值具有不同平均值时表现良好。与 MorphGAC 不同,MorphACWE 不要求对象的轮廓定义清晰,并且它直接在原始图像上工作,无需任何预处理。这使得 MorphACWEMorphGAC 更易于使用和调整。

参考文献#

Morphological ACWE segmentation, Morphological ACWE evolution, Morphological GAC segmentation, Morphological GAC evolution
/Users/cw/baidu/code/fin_tool/github/scikit-image/doc/examples/segmentation/plot_morphsnakes.py:95: MatplotlibDeprecationWarning:

The collections attribute was deprecated in Matplotlib 3.8 and will be removed in 3.10.

/Users/cw/baidu/code/fin_tool/github/scikit-image/doc/examples/segmentation/plot_morphsnakes.py:97: MatplotlibDeprecationWarning:

The collections attribute was deprecated in Matplotlib 3.8 and will be removed in 3.10.

/Users/cw/baidu/code/fin_tool/github/scikit-image/doc/examples/segmentation/plot_morphsnakes.py:99: MatplotlibDeprecationWarning:

The collections attribute was deprecated in Matplotlib 3.8 and will be removed in 3.10.

/Users/cw/baidu/code/fin_tool/github/scikit-image/doc/examples/segmentation/plot_morphsnakes.py:133: MatplotlibDeprecationWarning:

The collections attribute was deprecated in Matplotlib 3.8 and will be removed in 3.10.

/Users/cw/baidu/code/fin_tool/github/scikit-image/doc/examples/segmentation/plot_morphsnakes.py:135: MatplotlibDeprecationWarning:

The collections attribute was deprecated in Matplotlib 3.8 and will be removed in 3.10.

/Users/cw/baidu/code/fin_tool/github/scikit-image/doc/examples/segmentation/plot_morphsnakes.py:137: MatplotlibDeprecationWarning:

The collections attribute was deprecated in Matplotlib 3.8 and will be removed in 3.10.

import numpy as np
import matplotlib.pyplot as plt
from skimage import data, img_as_float
from skimage.segmentation import (
    morphological_chan_vese,
    morphological_geodesic_active_contour,
    inverse_gaussian_gradient,
    checkerboard_level_set,
)


def store_evolution_in(lst):
    """Returns a callback function to store the evolution of the level sets in
    the given list.
    """

    def _store(x):
        lst.append(np.copy(x))

    return _store


# Morphological ACWE
image = img_as_float(data.camera())

# Initial level set
init_ls = checkerboard_level_set(image.shape, 6)
# List with intermediate results for plotting the evolution
evolution = []
callback = store_evolution_in(evolution)
ls = morphological_chan_vese(
    image, num_iter=35, init_level_set=init_ls, smoothing=3, iter_callback=callback
)

fig, axes = plt.subplots(2, 2, figsize=(8, 8))
ax = axes.flatten()

ax[0].imshow(image, cmap="gray")
ax[0].set_axis_off()
ax[0].contour(ls, [0.5], colors='r')
ax[0].set_title("Morphological ACWE segmentation", fontsize=12)

ax[1].imshow(ls, cmap="gray")
ax[1].set_axis_off()
contour = ax[1].contour(evolution[2], [0.5], colors='g')
contour.collections[0].set_label("Iteration 2")
contour = ax[1].contour(evolution[7], [0.5], colors='y')
contour.collections[0].set_label("Iteration 7")
contour = ax[1].contour(evolution[-1], [0.5], colors='r')
contour.collections[0].set_label("Iteration 35")
ax[1].legend(loc="upper right")
title = "Morphological ACWE evolution"
ax[1].set_title(title, fontsize=12)


# Morphological GAC
image = img_as_float(data.coins())
gimage = inverse_gaussian_gradient(image)

# Initial level set
init_ls = np.zeros(image.shape, dtype=np.int8)
init_ls[10:-10, 10:-10] = 1
# List with intermediate results for plotting the evolution
evolution = []
callback = store_evolution_in(evolution)
ls = morphological_geodesic_active_contour(
    gimage,
    num_iter=230,
    init_level_set=init_ls,
    smoothing=1,
    balloon=-1,
    threshold=0.69,
    iter_callback=callback,
)

ax[2].imshow(image, cmap="gray")
ax[2].set_axis_off()
ax[2].contour(ls, [0.5], colors='r')
ax[2].set_title("Morphological GAC segmentation", fontsize=12)

ax[3].imshow(ls, cmap="gray")
ax[3].set_axis_off()
contour = ax[3].contour(evolution[0], [0.5], colors='g')
contour.collections[0].set_label("Iteration 0")
contour = ax[3].contour(evolution[100], [0.5], colors='y')
contour.collections[0].set_label("Iteration 100")
contour = ax[3].contour(evolution[-1], [0.5], colors='r')
contour.collections[0].set_label("Iteration 230")
ax[3].legend(loc="upper right")
title = "Morphological GAC evolution"
ax[3].set_title(title, fontsize=12)

fig.tight_layout()
plt.show()

脚本总运行时间: (0 分钟 3.663 秒)

由 Sphinx-Gallery 生成的图库