备注
单击 here 下载完整的示例代码或通过活页夹在浏览器中运行此示例
使用紧凑分水岭查找规则线段¶
分水岭变换通常被用作许多分割算法的起点。然而,如果不明智地选择种子,它可能会产生非常不均匀的碎片大小,这在后续分析中可能很难处理。
这个 紧凑型 分水岭变换通过偏爱接近所考虑的像素的种子来解决这一问题。
这两种算法都是在 skimage.morphology.watershed()
功能。若要使用紧凑形式,只需传递一个 compactness
值大于0。

import numpy as np
from skimage import data, util, filters, color
from skimage.segmentation import watershed
import matplotlib.pyplot as plt
coins = data.coins()
edges = filters.sobel(coins)
grid = util.regular_grid(coins.shape, n_points=468)
seeds = np.zeros(coins.shape, dtype=int)
seeds[grid] = np.arange(seeds[grid].size).reshape(seeds[grid].shape) + 1
w0 = watershed(edges, seeds)
w1 = watershed(edges, seeds, compactness=0.01)
fig, (ax0, ax1) = plt.subplots(1, 2)
ax0.imshow(color.label2rgb(w0, coins, bg_label=-1))
ax0.set_title('Classical watershed')
ax1.imshow(color.label2rgb(w1, coins, bg_label=-1))
ax1.set_title('Compact watershed')
plt.show()
脚本的总运行时间: (0分0.235秒)