L = watershed(A)
L = watershed(A,conn)
a label matrix
L = watershed(
L that identifies the watershed
regions of the input matrix
A, which can have
any dimension. The watershed transform finds "catchment basins" or
"watershed ridge lines" in an image by treating it as a surface where
light pixels represent high elevations and dark pixels represent low
elevations. The elements of
L are integer values
greater than or equal to
0. The elements labeled
not belong to a unique watershed region. The elements labeled
to the first watershed region, the elements labeled 2 belong to the
second watershed region, and so on. By default,
8-connected neighborhoods for 2-D inputs and 26-connected neighborhoods
for 3-D inputs. For higher dimensions,
the connectivity given by
Compute the watershed transform and display the resulting label matrix as an RGB image. This example works with 2-D images.
Create a binary image containing two overlapping circular objects and display it.
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')
Compute the distance transform of the complement of the binary image.
D = bwdist(~bw); figure imshow(D,,'InitialMagnification','fit') title('Distance transform of ~bw')
Complement the distance transform, and force pixels that don't belong to the objects to be at
D = -D; D(~bw) = Inf;
Compute the watershed transform and display the resulting label matrix as an RGB image.
L = watershed(D); L(~bw) = 0; rgb = label2rgb(L,'jet',[.5 .5 .5]); figure imshow(rgb,'InitialMagnification','fit') title('Watershed transform of D')
Make a 3-D binary image containing two overlapping spheres.
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
Compute the distance transform.
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
Complement the distance transform, force nonobject pixels to be
Inf, and then compute the watershed transform.
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
A— Input image
Input image, specified as a real, nonsparse, numeric, or logical array of any dimension.
RGB = imread('pears.png'); I = rgb2gray(RGB);
Connectivity, specified as one of the values in this table.
Connectivity can also be defined in a more general way for any
dimension by using for
conn a 3-by-3-by- ... -by-3
elements define neighborhood locations relative to the center element
conn, which must be symmetric around its center
If you specify a nondefault connectivity, pixels on the edge
of the image might not be considered to be border pixels. For example,
L = watershed(I,4);
L— Label matrix
Label matrix, specified as a numeric array of unsigned integers.
The watershed transform algorithm used by this function
changed in version 5.4 (R2007a) of the Image Processing Toolbox™ software.
The previous algorithm occasionally produced labeled watershed basins
that were not contiguous. If you need to obtain the same results as
the previous algorithm, use the function
watershed uses the Fernand Meyer algorithm .
 Meyer, Fernand, "Topographic distance and watershed lines," Signal Processing , Vol. 38, July 1994, pp. 113-125.
Usage notes and limitations:
This function supports the generation of C code using MATLAB® Coder™.
Note that if you choose the generic
MATLAB Host Computer target
platform, the function generates code that uses a precompiled, platform-specific
shared library. Use of a shared library preserves performance optimizations
but limits the target platforms for which code can be generated. For
more information, see Understanding Code Generation with Image Processing Toolbox.
Supports only 2-D images
Supports images containing up to 65,535 regions
Output type is always