Main Content

watershed

分水岭变换

说明

分水岭变换将图像视为一个曲面,其中亮像素表示较高处,暗像素表示较低处,从而找出图像中的“汇水盆地”或“分水岭脊线”。分水岭变换可用于将感兴趣的连续区域分割成不同对象。

示例

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')

计算二值图像反色的距离变换。输出图像中每个像素的值是该像素与 bw 中最近的非零像素之间的距离。

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

对距离变换图像求补码,以便在经过分水岭变换后, 亮像素表示较高处,暗像素表示较低处。

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

计算分水岭变换。将 ROI 外的像素设置为 0

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

将生成的标签矩阵显示为 RGB 图像。

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

对距离变换求补码,强制非目标像素为 Inf,然后计算分水岭变换。

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

输入参数

全部折叠

输入图像,指定为任意维度的数值或逻辑数组。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

像素连通性,指定为下表中的值之一。对于二维图像,默认连通性是 8,对于三维图像,默认连通性是 26

意义

二维连通

4

如果像素的边缘相互接触,则这些像素具有连通性。像素的邻域是水平或垂直方向上的相邻像素。

3-by-3 pixel neighborhood with four pixels connected to the center pixel

当前像素以灰色显示。

8

如果像素的边缘或角相互接触,则这些像素具有连通性。像素的邻域是水平、垂直或对角线方向上的相邻像素。

3-by-3 pixel neighborhood with 8 pixels connected to the center pixel

当前像素以灰色显示。

三维连通

6

如果像素的面接触,则这些像素具有连通性。像素的邻域是符合以下条件的相邻像素:

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

3-by-3-by-3 pixel neighborhood with 6 pixels connected to the faces of the center pixel

当前像素以灰色显示。

18

如果像素的面或边缘接触,则这些像素具有连通性。像素的邻域是符合以下条件的相邻像素:

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

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

3-by-3-by-3 pixel neighborhood with 6 pixels connected to the faces and 12 pixels connected to the edges of the center pixel

当前像素是立方体的中心。

26

如果像素的面、边缘或角接触,则这些像素具有连通性。像素的邻域是符合以下条件的相邻像素:

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

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

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

3-by-3-by-3 pixel neighborhood with 6 pixels connected to the faces, 12 pixels connected to the edges, and 8 pixels connected to the corners of the center pixel

当前像素是立方体的中心。

对于更高的维度,watershed 使用默认值 conndef(ndims(A),'maximal')

也可以通过指定由 01 组成的 3×3×...×3 矩阵,以更通用的方式来定义任意维度的连通性。值为 1 的元素定义相对于 conn 的中心元素的邻域位置。请注意,conn 必须关于其中心元素对称。有关详细信息,请参阅指定自定义连通性

注意

如果指定非默认连通性,图像边缘上的像素可能不被视为边框像素。例如,如果 conn = [0 0 0; 1 1 1; 0 0 0],则第一行和最后一行的元素不被视为边框像素,因为根据连通性定义,它们未连接到图像外部的区域。

数据类型: double | logical

输出参量

全部折叠

标签矩阵,指定为由非负整数组成的数值数组。标注为 0 的元素不属于一个唯一的分水岭区域。标注为 1 的元素属于第一个分水岭区域,标注为 2 的元素属于第二个分水岭区域,依此类推。

提示

  • 此函数使用的分水岭变换算法在 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.

扩展功能

版本历史记录

在 R2006a 之前推出

全部展开