用法:
skimage.segmentation.watershed(image, markers=None, connectivity=1, offset=None, mask=None, compactness=0, watershed_line=False)
在從給定標記淹沒的圖像中查找流域盆地。
- image:ndarray (2-D, 3-D, ...)
首先標記最低值點的數據數組。
- markers:int 或 int 的 ndarray 形狀與圖片, 可選的
所需數量的標記,或用要在標簽矩陣中分配的值標記盆地的數組。零表示不是標記。如果
None
(未給出標記),則使用圖像的局部最小值作為標記。- connectivity:ndarray,可選
與圖像具有相同維數的數組,其非零元素表示連接的鄰居。按照 scipy 約定,默認是圖像尺寸的 one-connected 數組。
- offset:數組 形狀 image.ndim 可選
連接的偏移量(每個維度一個偏移量)
- mask:布爾值或 0 和 1 的 ndarray,可選
與圖像形狀相同的數組。隻有 mask == True 的點才會被標記。
- compactness:浮點數,可選
使用具有給定緊湊度參數的緊湊流域[3]。較高的值會導致更多的regularly-shaped 流域盆地。
- watershed_line:布爾型,可選
如果watershed_line 為True,則one-pixel 寬線將分水嶺算法獲得的區域分開。該行的標簽為 0。
- out:ndarray
與標記具有相同類型和形狀的標記矩陣
參數:
返回:
注意:
這個函數實現了一個分水嶺算法[1][2],它將像素分配到標記的盆地中。該算法使用優先級隊列來保存像素,優先級隊列的度量是像素值,然後是進入隊列的時間——這會解決有利於最近標記的關係。
一些想法來自 Soille,“使用數學形態學從數字高程模型中自動劃分盆地”,信號處理 20 (1990) 171-182
論文中最重要的見解是,進入隊列的時間解決了兩個問題:一個像素應該分配給具有最大梯度的鄰居,或者,如果沒有梯度,高原上的像素應該在相對兩側的標記之間分割.
此實現將所有參數轉換為特定的最低公分母類型,然後將它們傳遞給 C 算法。
標記可以手動確定,也可以使用例如圖像梯度的局部最小值或與背景的距離函數的局部最大值自動確定,以分離重疊對象(參見示例)。
參考:
- 1
https://en.wikipedia.org/wiki/Watershed_%28image_processing%29
- 2
- 3
Peer Neubert & Peter Protzel (2014). Compact Watershed and Preemptive SLIC: On Improving Trade-offs of Superpixel Segmentation Algorithms. ICPR 2014, pp 996-1001. DOI:10.1109/ICPR.2014.181 https://www.tu-chemnitz.de/etit/proaut/publications/cws_pSLIC_ICPR.pdf
例子:
分水嶺算法對於分離重疊對象很有用。
我們首先生成具有兩個重疊圓圈的初始圖像:
>>> x, y = np.indices((80, 80)) >>> x1, y1, x2, y2 = 28, 28, 44, 52 >>> r1, r2 = 16, 20 >>> mask_circle1 = (x - x1)**2 + (y - y1)**2 < r1**2 >>> mask_circle2 = (x - x2)**2 + (y - y2)**2 < r2**2 >>> image = np.logical_or(mask_circle1, mask_circle2)
接下來,我們要將兩個圓圈分開。我們在到背景距離的最大值處生成標記:
>>> from scipy import ndimage as ndi >>> distance = ndi.distance_transform_edt(image) >>> from skimage.feature import peak_local_max >>> max_coords = peak_local_max(distance, labels=image, ... footprint=np.ones((3, 3))) >>> local_maxima = np.zeros_like(image, dtype=bool) >>> local_maxima[tuple(max_coords.T)] = True >>> markers = ndi.label(local_maxima)[0]
最後,我們在圖像和標記上運行分水嶺:
>>> labels = watershed(-distance, markers, mask=image)
該算法也適用於 3-D 圖像,例如可以用於分離重疊的球體。
相關用法
- Python skimage.segmentation.active_contour用法及代碼示例
- Python skimage.segmentation.relabel_sequential用法及代碼示例
- Python skimage.segmentation.flood_fill用法及代碼示例
- Python skimage.segmentation.clear_border用法及代碼示例
- Python skimage.segmentation.felzenszwalb用法及代碼示例
- Python skimage.segmentation.expand_labels用法及代碼示例
- Python skimage.segmentation.find_boundaries用法及代碼示例
- Python skimage.segmentation.random_walker用法及代碼示例
- Python skimage.segmentation.slic用法及代碼示例
- Python skimage.segmentation.flood用法及代碼示例
- Python skimage.segmentation.join_segmentations用法及代碼示例
- Python skimage.feature.graycomatrix用法及代碼示例
- Python skimage.color.lab2lch用法及代碼示例
- Python skimage.draw.random_shapes用法及代碼示例
- Python skimage.feature.blob_doh用法及代碼示例
- Python skimage.feature.blob_dog用法及代碼示例
- Python skimage.filters.unsharp_mask用法及代碼示例
- Python skimage.registration.optical_flow_tvl1用法及代碼示例
- Python skimage.filters.rank.noise_filter用法及代碼示例
- Python skimage.exposure.histogram用法及代碼示例
注:本文由純淨天空篩選整理自scikit-image.org大神的英文原創作品 skimage.segmentation.watershed。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。