www.gusucode.com > images 案例代码 matlab源码程序 > images/ComputeWatershedTransformAndDisplayResultingLabelMatrixExample.m

    %% Compute Watershed Transform and Display Resulting Label Matrix
% 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.

% Copyright 2015 The MathWorks, Inc.

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')