watershed
Watershed transform
Description
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 watershed transform can be used to segment contiguous regions of interest into distinct objects.
Examples
Compute Watershed Transform and Display Resulting Label Matrix
Create a binary image containing two overlapping circular objects. Display the image.
center1 = 40;
center2 = center1;
dist = sqrt(2*(2*center1)^2);
radius = dist/2 * 1.4;
lims = [floor(center11.2*radius) ceil(center2+1.2*radius)];
[x,y] = meshgrid(lims(1):lims(2));
bw1 = sqrt((xcenter1).^2 + (ycenter1).^2) <= radius;
bw2 = sqrt((xcenter2).^2 + (ycenter2).^2) <= radius;
bw = bw1  bw2;
imshow(bw)
title('Binary Image with Overlapping Objects')
Calculate the distance transform of the complement of the binary image. The value of each pixel in the output image is the distance between that pixel and the nearest nonzero pixel of bw
.
D = bwdist(~bw);
imshow(D,[])
title('Distance Transform of Binary Image')
Take the complement of the distance transformed image so that light pixels represent high elevations and dark pixels represent low elevations for the watershed transform.
D = D;
imshow(D,[])
title('Complement of Distance Transform')
Calculate the watershed transform. Set pixels that are outside the ROI to 0
.
L = watershed(D); L(~bw) = 0;
Display the resulting label matrix as an RGB image.
rgb = label2rgb(L,'jet',[.5 .5 .5]); imshow(rgb) title('Watershed Transform')
Compute Watershed Transform of 3D Binary Image
Make a 3D binary image containing two overlapping spheres.
center1 = 10; center2 = center1; dist = sqrt(3*(2*center1)^2); radius = dist/2 * 1.4; lims = [floor(center11.2*radius) ceil(center2+1.2*radius)]; [x,y,z] = meshgrid(lims(1):lims(2)); bw1 = sqrt((xcenter1).^2 + (ycenter1).^2 + ... (zcenter1).^2) <= radius; bw2 = sqrt((xcenter2).^2 + (ycenter2).^2 + ... (zcenter2).^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
Input Arguments
A
— Input image
numeric array  logical array
Input image, specified as a numeric or logical array of any dimension.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
 logical
conn
— Pixel connectivity
4
 8
 6
 18
 26
 3by3by ... by3 matrix of 0
s and
1
s
Pixel connectivity, specified as one of the values in this table. The
default connectivity is 8
for 2D images, and
26
for 3D images.
Value  Meaning  

TwoDimensional Connectivities  
 Pixels are connected if their edges touch. The neighborhood of a pixel are the adjacent pixels in the horizontal or vertical direction. 
Current pixel is shown in gray. 
 Pixels are connected if their edges or corners touch. The neighborhood of a pixel are the adjacent pixels in the horizontal, vertical, or diagonal direction. 
Current pixel is shown in gray. 
ThreeDimensional Connectivities  
 Pixels are connected if their faces touch. The neighborhood of a pixel are the adjacent pixels in:

Current pixel is shown in gray. 
 Pixels are connected if their faces or edges touch. The neighborhood of a pixel are the adjacent pixels in:

Current pixel is center of cube. 
 Pixels are connected if their faces, edges, or corners touch. The neighborhood of a pixel are the adjacent pixels in:

Current pixel is center of cube. 
For higher dimensions, watershed
uses the default value
.conndef
(ndims(A),'maximal')
Connectivity can also be
defined in a more general way for any dimension by specifying a 3by3by ... by3 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. See Specifying Custom Connectivities for more information.
Note
If you specify a nondefault connectivity, pixels on the edge of the
image might not be considered to be border pixels. For example, if
conn = [0 0 0; 1 1 1; 0 0 0]
, elements on the
first and last row are not considered to be border pixels because,
according to that connectivity definition, they are not connected to the
region outside the image.
Data Types: double
 logical
Output Arguments
L
— Label matrix
numeric array of nonnegative integers
Label matrix, specified as a numeric array of nonnegative integers. The elements labeled
0
do not belong to a unique watershed region. The
elements labeled 1
belong to the first watershed region,
the elements labeled 2 belong to the second watershed region, and so
on.
Tips
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_old
.To prevent oversegmentation, remove shallow minima from the image by using the
imhmin
function before you use thewatershed
function.
Algorithms
watershed
uses the Fernand Meyer algorithm [1].
References
[1] Meyer, Fernand, "Topographic distance and watershed lines,” Signal Processing , Vol. 38, July 1994, pp. 113125.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
watershed
supports the generation of C code (requires MATLAB^{®} Coder™). Note that if you choose the genericMATLAB Host Computer
target platform,watershed
generates code that uses a precompiled, platformspecific 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 Types of Code Generation Support in Image Processing Toolbox.Supports only 2D images
Supports only
4
or8
connectivitySupports images containing up to 65,535 regions
Output type is always
uint16
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
Supports only 2D images
Supports only
4
or8
connectivitySupports images containing up to 65,535 regions
Output type is always
uint16
ThreadBased Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports threadbased environments. For more information, see Run MATLAB Functions in ThreadBased Environment.
Version History
Introduced before R2006aR2022b: Support for threadbased environments
watershed
now supports threadbased
environments.
See Also
bwlabel
 bwlabeln
 bwdist
 regionprops
 imhmin
Apri esempio
Si dispone di una versione modificata di questo esempio. Desideri aprire questo esempio con le tue modifiche?
Comando MATLAB
Hai fatto clic su un collegamento che corrisponde a questo comando MATLAB:
Esegui il comando inserendolo nella finestra di comando MATLAB. I browser web non supportano i comandi MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
 América Latina (Español)
 Canada (English)
 United States (English)
Europe
 Belgium (English)
 Denmark (English)
 Deutschland (Deutsch)
 España (Español)
 Finland (English)
 France (Français)
 Ireland (English)
 Italia (Italiano)
 Luxembourg (English)
 Netherlands (English)
 Norway (English)
 Österreich (Deutsch)
 Portugal (English)
 Sweden (English)
 Switzerland
 United Kingdom (English)