function L =watershed(varargin)
%WATERSHED Watershedtransform.
% L = WATERSHED(A) computes a label matrixidentifying the watershed
% regions of the input matrix A. A can have any dimension. The elements
% of L are integer values greater than orequal to 0. The elements
% labeled 0 do not belong to a uniquewatershed region. These are called
% "watershed pixels." The elements labeled 1 belong to the first
% watershed region, the elements labeled 2belong to the second watershed
% region, and so on.
L =WATERSHED(A) 计算一个标签矩阵, 用于识别输入矩阵A的汇水区域. A可以是任意维. L的元素均为大于等于0的整数值. 标签值为0的元素不属于任何一个汇水区域, 称它们为"分水岭". 标签值为1的元素属于第1个汇水区域, 标签值为2的元素属于第2个汇水区域, 以此类推.
% By default, WATERSHED uses 8-connectedneighborhoods for 2-D inputs
% and 26-connected neighborhoods for 3-Dinputs. For higher
% dimensions, WATERSHED uses the connectivitygiven by
% CONNDEF(NDIMS(A),'maximal').
WATERSHED默认对2维输入数据采用8邻接, 对3维输入数据采用26邻接. 对于更高维数的输入, WATERSHED根据CONNDEF(NDIMS(A), 'maximal')的值来判定连通性.
% L = WATERSHED(A,CONN) computes the watershedtransform using the
% specified connectivity. CONN may have the following scalar values:
%
% 4 two-dimensional four-connected neighborhood
% 8 two-dimensional eight-connected neighborhood
% 6 three-dimensional six-connected neighborhood
% 18 three-dimensional 18-connected neighborhood
% 26 three-dimensional 26-connected neighborhood
L =WATERSHED(A, CONN)根据指定的连通性判定规则计算分水岭变换. CONN可以有如下取值(标量)
4: 针对2维矩阵, 4邻接;
8: 针对2维矩阵, 8邻接;
6: 针对3维矩阵, 6邻接
18: 针对3维矩阵, 18邻接
26: 针对3维矩阵, 26邻接
% Connectivity may be defined in a moregeneral way for any dimension by
% using for CONN a 3-by-3-by- ... -by-3 matrixof 0s and 1s. The 1-valued
% elements define neighborhood locationsrelative to the center element of
% CONN. If specified this way, CONN must be symmetric about its center.
连通性判定可以用一种更通用的方式指定, 即为CONN使用一个3X3X…X3矩阵, 每个元素的值只能为0或1. 值为1的元素定义相对于CONN的中心元素相邻位置(即值为1的元素视为与中心元素是邻接的). 如果采用这种方法, CONN必须满足中心对称.
% Note
% ----
% The watershed transform algorithm used bythis function changed in
% version 5.4 (R2007a) of the Image ProcessingToolbox. The previous
% algorithm occasionally produced labeledwatershed basins that were not
% contiguous. If you need to obtain the same results as the previous
% algorithm, use the function WATERSHED_OLD.
注:
自5.4版(R2007)图像处理工具箱以来, 该函数(R2015a)采用的分水岭变换算法已经改变了. 之前采用的算法偶尔会生成非毗邻的汇水盆地(?不一定翻译到位). 如果需要获取与之前算法同样的结果, 请调用WATERSHED_OLD.
% Class Support
% -------------
% A can be a numeric or logical array of anydimension, and it must be
% nonsparse. The output array L is an unsigned integer type.
类支持
A可以是任意维的数值或逻辑数组, 且必须是非稀疏的. 输出数组L的类型为无符号整型.
% Example (2-D)
% -------------
% 1. Make a binary image containing twooverlapping circular objects.
%
% center1 = -10;
% 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;
% figure,imshow(bw,'InitialMagnification','fit'), title('bw')
示例 (2维)
1. 制作一幅包含两个重叠圆形对象的二值图像
center1 = -10; %第一个圆的中心(-10, -10)
center2 =-center1;%第二个圆的中心(10, 10)
dist = sqrt(2*(2*center1)^2);%两圆心距离
radius = dist/2 * 1.4;%两圆心距离的0.7倍, 使得两个圆重叠
lims= [floor(center1-1.2*radius) ceil(center2+1.2*radius)];%[-3434]
[x,y] = meshgrid(lims(1):lims(2));%生成一个矩形网格, X: [-34, -34], Y: [-34, -34], x,y均为2维矩阵, 这两个矩阵可用于指定任何一个69X69矩阵的任何位置对应的直角坐标
bw1 = sqrt((x-center1).^2 + (y-center1).^2) <= radius;%产生第一个圆的二值图像
bw2 = sqrt((x-center2).^2 + (y-center2).^2) <= radius;%产生第二个圆的二值图像
bw = bw1 | bw2;%逻辑或, 两幅图像综合
figure,imshow(bw,'InitialMagnification','fit'), title('bw')