注意
跳到結尾下載完整的範例程式碼。 或透過 Binder 在您的瀏覽器中執行此範例
形態學蛇形#
形態學蛇形 [1] 是一系列用於影像分割的方法。它們的行為類似於活動輪廓(例如,測地活動輪廓 [2] 或無邊緣活動輪廓 [3])。然而,形態學蛇形 在二元陣列上使用形態學運算子(例如,擴張或侵蝕),而不是在浮點數陣列上求解偏微分方程式,這是活動輪廓的標準方法。這使得形態學蛇形 比傳統的對應物更快且數值更穩定。
在此實作中有兩種形態學蛇形 方法可用:形態學測地活動輪廓(MorphGAC,在 morphological_geodesic_active_contour
函式中實作)和無邊緣形態學活動輪廓(MorphACWE,在 morphological_chan_vese
函式中實作)。
MorphGAC 適用於具有可見輪廓的影像,即使這些輪廓可能嘈雜、混亂或部分不明確。然而,它需要預處理影像以突顯輪廓。這可以使用 inverse_gaussian_gradient
函式完成,儘管使用者可能想要定義自己的版本。MorphGAC 分割的品質很大程度上取決於此預處理步驟。
相反地,當要分割的物件的內部和外部區域的像素值具有不同的平均值時,MorphACWE 運作良好。與 MorphGAC 不同,MorphACWE 不需要物件的輪廓明確定義,並且它可以對原始影像執行,而無需任何先前的處理。這使得 MorphACWE 比 MorphGAC 更容易使用和調整。
參考文獻#

/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 分鐘 9.697 秒)