You are here : matlabImage Processingwatershed

watershed() - Image Processing

L = watershed(A) returns
a label matrix 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 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. 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.Code Generation support:
Yes.MATLABĀ® Function Block support: No.


Syntax

L = watershed(A) exampleL = watershed(A,conn)


Example

Compute Watershed Transform and Display Resulting Label MatrixOpen This Example
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 -Inf .D = -D;
D(~bw) = -Inf;
Compute the watershed transform and display the resulting label matrix as an RGB image.L = watershed(D);
rgb = label2rgb(L,'jet',[.5 .5 .5]);
figure
imshow(rgb,'InitialMagnification','fit')
title('Watershed transform of D')

Compute Watershed Transform of 3-D Binary ImageOpen This Example
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);
figure
isosurface(x,y,z,L==2,0.5)
isosurface(x,y,z,L==3,0.5)
axis equal
title('Segmented objects')
xlabel x, ylabel y, zlabel z
xlim(lims), ylim(lims), zlim(lims)
view(3), camlight, lighting gouraud


Output / Return Value


Limitations


Alternatives / See Also


Reference