注
末尾へ移動して、完全なサンプルコードをダウンロードするか、Binder経由でブラウザでこのサンプルを実行します。
輪郭検出#
画像内の一定値の輪郭を検出するために、マーチングスクエア法を使用します。skimage.measure.find_contours
では、出力輪郭の精度を高めるために配列値が線形補間されます。画像エッジと交差する輪郭は開いています。その他はすべて閉じています。
マーチングスクエアアルゴリズムは、マーチングキューブアルゴリズムの特殊なケースです(Lorensen、WilliamおよびHarvey E. Cline。マーチングキューブ:高解像度3D表面構築アルゴリズム。Computer Graphics SIGGRAPH 87 Proceedings)21(4) 1987年7月、p. 163-170)。

import numpy as np
import matplotlib.pyplot as plt
from skimage import measure
# Construct some test data
x, y = np.ogrid[-np.pi : np.pi : 100j, -np.pi : np.pi : 100j]
r = np.sin(np.exp(np.sin(x) ** 3 + np.cos(y) ** 2))
# Find contours at a constant value of 0.8
contours = measure.find_contours(r, 0.8)
# Display the image and plot all contours found
fig, ax = plt.subplots()
ax.imshow(r, cmap=plt.cm.gray)
for contour in contours:
ax.plot(contour[:, 1], contour[:, 0], linewidth=2)
ax.axis('image')
ax.set_xticks([])
ax.set_yticks([])
plt.show()
スクリプトの総実行時間: (0分 0.205秒)