备注
单击 here 下载完整的示例代码或通过活页夹在浏览器中运行此示例
展开无重叠的分段标签¶
给定由标签图像表示的几个连通分量,这些连通分量可以使用扩展到背景区域 skimage.segmentation.expand_labels()
。与之形成鲜明对比的是 skimage.morphology.dilation()
这种方法不会让连通部件扩展到标签编号较低的相邻连通部件。

import numpy as np
import matplotlib.pyplot as plt
from skimage.filters import sobel
from skimage.measure import label
from skimage.segmentation import watershed, expand_labels
from skimage.color import label2rgb
from skimage import data
coins = data.coins()
# Make segmentation using edge-detection and watershed.
edges = sobel(coins)
# Identify some background and foreground pixels from the intensity values.
# These pixels are used as seeds for watershed.
markers = np.zeros_like(coins)
foreground, background = 1, 2
markers[coins < 30.0] = background
markers[coins > 150.0] = foreground
ws = watershed(edges, markers)
seg1 = label(ws == foreground)
expanded = expand_labels(seg1, distance=10)
# Show the segmentations.
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(9, 5),
sharex=True, sharey=True)
color1 = label2rgb(seg1, image=coins, bg_label=0)
axes[0].imshow(color1)
axes[0].set_title('Sobel+Watershed')
color2 = label2rgb(expanded, image=coins, bg_label=0)
axes[1].imshow(color2)
axes[1].set_title('Expanded labels')
for a in axes:
a.axis('off')
fig.tight_layout()
plt.show()
脚本的总运行时间: (0分0.211秒)