使用緊湊分水嶺尋找規則區段#

分水嶺轉換通常用作許多分割演算法的起點。然而,如果沒有明智地選擇種子,它會產生非常不均勻的片段大小,這在下游分析中可能難以處理。

緊湊分水嶺轉換透過偏好接近所考慮像素的種子來解決此問題。

這兩種演算法都在 skimage.segmentation.watershed() 函式中實作。若要使用緊湊形式,只需傳遞大於 0 的 compactness 值。

Classical watershed, Compact watershed
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.451 秒)

由 Sphinx-Gallery 產生的圖庫