用法:
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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。