主要内容

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

Figure contains an axes object. The hidden axes object with title Binary Image with Overlapping Objects contains an object of type image.

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

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

Figure contains an axes object. The hidden axes object with title Distance Transform of Binary Image contains an object of type image.

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

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

Figure contains an axes object. The hidden axes object with title Complement of Distance Transform contains an object of type image.

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

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

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

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

Figure contains an axes object. The hidden axes object with title Watershed Transform contains an object of type image.

生成一个包含两个重叠球体的三维二值图像。

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

Figure contains an axes object. The axes object with title BW, xlabel x, ylabel y contains an object of type patch.

计算距离变换。

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

Figure contains an axes object. The axes object with title Isosurface of distance transform, xlabel x, ylabel y contains an object of type patch.

对距离变换取反色,强制非目标像素为 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

Figure contains an axes object. The axes object with title Segmented objects, xlabel x, ylabel y contains 2 objects of type patch.

您可以隐藏低浅的区域最小值以避免在分水岭分割期间出现过分割。

加载一个要分割的 RGB 图像。将该图像转换为灰度并显示。每个梨的中心是明亮的,对应于区域最大值。

RGB = imread("pears.png");
I = im2gray(RGB);
imshow(I)

Figure contains an axes object. The hidden axes object contains an object of type image.

在分水岭分割中,图像类似于由分水岭线和汇水盆地组成的曲面。当水流入该曲面时,它会汇集在汇水盆地中。在灰度图像中,局部最小值是汇水盆地。为了分割梨,请反转图像以使梨的中心成为区域最小值。

Icomp = imcomplement(I);
imshow(Icomp)

Figure contains an axes object. The hidden axes object contains an object of type image.

将反转后的图像显示为三维曲面,其中每个像素的第三个维度是其强度值。每个梨的较深区域具有尖底,表明有许多低浅的区域最小值,就像水可以汇入的汇水盆地。

surf(Icomp,EdgeColor="none")
colormap(gray)

Figure contains an axes object. The axes object contains an object of type surface.

分割未滤波的图像并将结果显示为标签叠加。图像出现过分割,意味着有许多小掩膜而不是每个梨对应一个掩膜。

L = watershed(Icomp);
overlay = labeloverlay(I,L);
imshow(overlay)

Figure contains an axes object. The hidden axes object contains an object of type image.

通过应用 H 最小值变换隐藏低浅的最小值。h 的值已通过试错法确定。更改该值以查看 h 值如何影响分割结果。

h = 30;
Ifilt = imhmin(Icomp,h);

将滤波后的图像显示为三维曲面。

surf(Ifilt,EdgeColor="none")
colormap(gray)

Figure contains an axes object. The axes object contains an object of type surface.

分割滤波后的图像并显示结果。图像前景中的每个梨大约都对应一个掩膜。

Lfilt = watershed(Ifilt);
overlayfilt = labeloverlay(I,Lfilt);
imshow(overlayfilt)

Figure contains an axes object. The hidden axes object contains an object of type image.

输入参数

全部折叠

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

数据类型: 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 的元素属于第二个分水岭区域,依此类推。

提示

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

算法

watershed 使用 Fernand Meyer 算法 [1]

参考

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

扩展功能

全部展开

版本历史记录

在 R2006a 之前推出

全部展开