Image Processing Toolbox User's Guide |
Syntax
Description
L = watershed(A)
computes a label matrix identifying the watershed regions of the input matrix A
, which can have any dimension. The elements of L
are integer values greater than or equal to 0. The elements labeled 0 do not belong to a unique watershed region. These are called watershed pixels. The elements labeled 1 belong to the first watershed region, the elements labeled 2 belong to the second watershed region, and so on.
By default, watershed
uses 8-connected neighborhoods for 2-D inputs and 26-connected neighborhoods for 3-D inputs. For higher dimensions, watershed
uses the connectivity given by conndef(ndims(A),'maximal')
.
L = watershed(A,CONN)
specifies the connectivity to be used in the watershed computation. CONN
can have any of the following scalar values.
Connectivity can be defined in a more general way for any dimension by using for CONN
a 3-by-3-by- ...-by-3 matrix of 0
's and 1
's. The 1
-valued elements define neighborhood locations relative to the center element of CONN
. Note that CONN
must be symmetric about its center element.
Class Support
A
can be a numeric or logical array of any dimension, and it must be nonsparse. The output array L
is of class double
.
2-D Example
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')
D = bwdist(~bw); figure, imshow(D,[],'InitialMagnification','fit') title('Distance transform of ~bw')
-Inf
.
3-D Example
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
, and then compute the watershed transform.
D = -D; D(~bw) = -Inf; L = watershed(D); figure, isosurface(x,y,z,L==2,0.5), axis equal title('Segmented object') xlabel x, ylabel y, zlabel z xlim(lims), ylim(lims), zlim(lims) view(3), camlight, lighting gouraud figure, isosurface(x,y,z,L==3,0.5), axis equal title('Segmented object') xlabel x, ylabel y, zlabel z xlim(lims), ylim(lims), zlim(lims) view(3), camlight, lighting gouraud
Algorithm
watershed
uses a variation of the Vincent and Soille algorithm [1]. For details of the variation, see toolbox/images/images/private/watershed_vs.h
.
See Also
bwlabel
, bwlabeln
, bwdist
, regionprops
Reference
[1] Vincent, Luc, and Pierre Soille, "Watersheds in Digital Spaces: An Efficient Algorithm Based on Immersion Simulations," IEEE Transactions of Pattern Analysis and Machine Intelligence, Vol. 13, No. 6, June 1991, pp. 583-598.
warp | whitepoint |
© 1994-2005 The MathWorks, Inc.