形态学蛇形算法#

形态学蛇形算法 [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
/home/runner/work/scikit-image/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.

/home/runner/work/scikit-image/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.

/home/runner/work/scikit-image/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.

/home/runner/work/scikit-image/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.

/home/runner/work/scikit-image/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.

/home/runner/work/scikit-image/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 分钟 4.687 秒)

由 Sphinx-Gallery 生成的图库