# watershed

## 语法

``L = watershed(A)``
``L = watershed(A,conn)``

## 说明

``L = watershed(A)` 返回标识输入矩阵 `A` 的分水岭区域的标签矩阵 `L`。`
``L = watershed(A,conn)` 指定分水岭计算中要使用的连通性。`

## 示例

```center1 = -40; center2 = -center1; dist = sqrt(2*(2*center1)^2); radius = dist/2 * 1.4; lims = [floor(center1-1.2*radius) ceil(center2+1.2*radius)]; [x,y] = meshgrid(lims(1):lims(2)); bw1 = sqrt((x-center1).^2 + (y-center1).^2) <= radius; bw2 = sqrt((x-center2).^2 + (y-center2).^2) <= radius; bw = bw1 | bw2; imshow(bw) title('Binary Image with Overlapping Objects')```

```D = bwdist(~bw); imshow(D,[]) title('Distance Transform of Binary Image')```

```D = -D; imshow(D,[]) title('Complement of Distance Transform')```

```L = watershed(D); L(~bw) = 0;```

```rgb = label2rgb(L,'jet',[.5 .5 .5]); imshow(rgb) title('Watershed Transform')```

```center1 = -10; center2 = -center1; dist = sqrt(3*(2*center1)^2); radius = dist/2 * 1.4; lims = [floor(center1-1.2*radius) ceil(center2+1.2*radius)]; [x,y,z] = meshgrid(lims(1):lims(2)); bw1 = sqrt((x-center1).^2 + (y-center1).^2 + ... (z-center1).^2) <= radius; bw2 = sqrt((x-center2).^2 + (y-center2).^2 + ... (z-center2).^2) <= radius; bw = bw1 | bw2; figure, isosurface(x,y,z,bw,0.5), axis equal, title('BW') xlabel x, ylabel y, zlabel z xlim(lims), ylim(lims), zlim(lims) view(3), camlight, lighting gouraud```

```D = bwdist(~bw); figure, isosurface(x,y,z,D,radius/2), axis equal title('Isosurface of distance transform') xlabel x, ylabel y, zlabel z xlim(lims), ylim(lims), zlim(lims) view(3), camlight, lighting gouraud```

```D = -D; D(~bw) = Inf; L = watershed(D); L(~bw) = 0; figure isosurface(x,y,z,L==1,0.5) isosurface(x,y,z,L==2,0.5) axis equal title('Segmented objects') xlabel x, ylabel y, zlabel z xlim(lims), ylim(lims), zlim(lims) view(3), camlight, lighting gouraud```

## 输入参数

`4`

`8`

`6`

• 在所列方向之一上连通：内、外、左、右、上、下

`18`

• 在所列方向之一上连通：内、外、左、右、上、下

• 在两个方向的组合上连通，如右下或内上

`26`

• 在所列方向之一上连通：内、外、左、右、上、下

• 在两个方向的组合上连通，如右下或内上

• 在三个方向的组合上连通，如内右上或内左下

## 提示

• 此函数使用的分水岭变换算法在 Image Processing Toolbox™ 软件的 5.4 版 (R2007a) 中已更改。以前的算法偶尔会产生不连续的标注分水岭盆地。如果您需要获得与早期算法相同的结果，请使用函数 `watershed_old`

• 为了防止过分分割，请在使用 `watershed` 函数之前，使用 `imhmin` 函数去除图像中低浅的最小值。

## 算法

`watershed` 使用 Fernand Meyer 算法 [1]

## 参考

[1] Meyer, Fernand, "Topographic distance and watershed lines,” Signal Processing , Vol. 38, July 1994, pp. 113-125.