注
最後のに移動して、完全なサンプルコードをダウンロードするか、Binder経由でブラウザでこのサンプルを実行します。
ラベル画像の領域#
この例では、画像ラベリングで画像をセグメント化する方法を示します。次の手順が適用されます
- 自動大津法による閾値処理 
- バイナリクロージングによる小さな穴のクローズ 
- 画像境界に触れるアーティファクトの削除 
- 小さなオブジェクトをフィルタリングするための画像領域の測定 

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from skimage import data
from skimage.filters import threshold_otsu
from skimage.segmentation import clear_border
from skimage.measure import label, regionprops
from skimage.morphology import closing, footprint_rectangle
from skimage.color import label2rgb
image = data.coins()[50:-50, 50:-50]
# apply threshold
thresh = threshold_otsu(image)
bw = closing(image > thresh, footprint_rectangle((3, 3)))
# remove artifacts connected to image border
cleared = clear_border(bw)
# label image regions
label_image = label(cleared)
# to make the background transparent, pass the value of `bg_label`,
# and leave `bg_color` as `None` and `kind` as `overlay`
image_label_overlay = label2rgb(label_image, image=image, bg_label=0)
fig, ax = plt.subplots(figsize=(10, 6))
ax.imshow(image_label_overlay)
for region in regionprops(label_image):
    # take regions with large enough areas
    if region.area >= 100:
        # draw rectangle around segmented coins
        minr, minc, maxr, maxc = region.bbox
        rect = mpatches.Rectangle(
            (minc, minr),
            maxc - minc,
            maxr - minr,
            fill=False,
            edgecolor='red',
            linewidth=2,
        )
        ax.add_patch(rect)
ax.set_axis_off()
plt.tight_layout()
plt.show()
スクリプトの合計実行時間: (0分1.709秒)
