注記
完全なサンプルコードをダウンロードするには、最後まで進むか、Binder経由でブラウザでこのサンプルを実行してください。
モルフォロジカルスネーク#
_モルフォロジカルスネーク_ [1] は、画像セグメンテーションのための手法群です。その挙動は、アクティブコンター(例えば、_測地線アクティブコンター_ [2] や _エッジのないアクティブコンター_ [3])と似ています。しかし、_モルフォロジカルスネーク_ は、アクティブコンターの標準的なアプローチである浮動小数点配列に対する偏微分方程式を解く代わりに、バイナリ配列に対するモルフォロジー演算子(膨張や収縮など)を使用します。これにより、_モルフォロジカルスネーク_ は、従来の手法よりも高速で数値的に安定します。
この実装では、2つの_モルフォロジカルスネーク_ メソッドが利用可能です:_モルフォロジー測地線アクティブコンター_(**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秒)