Geoscience Reference
In-Depth Information
by
bwdist
, which assigns to each pixel a number that is the distance between
a pixel and the nearest non-zero pixel in
I7
. In an image in which objects
are identii ed by pixel values of zero and the background by pixel values of
one, the distance transform has zeros in the background areas and non-zero
values that increase progressively with increasing distances from the edges
of the objects. In our example however, the objects have pixel values of one
and the background has pixels with with values of zero. We therefore have
to apply
bwdist
to the complement of the binary image
I7
instead of to the
image itself.
D = bwdist(~I7,'cityblock');
h e function
bwdist
provides several methods for computing the nearest-
neighbor distances, including
Euclidean distances
,
cityblock distances
,
chessboard distances
and
quasi-Euclidean distances
. We choose the
cityblock
option in this particular example, but other methods might be more
appropriate for separating objects in other images. h e distance matrix now
contains positive non-zero values in the object pixels and zeros elsewhere.
We then complement the distance transform, and ascribe a value of
-Inf
to
each pixel that does not belong to an object.
D = -D;
D(~I7) = -Inf;
We compute the watershed transform for the distance matrix, and display
the resulting label matrix.
L2 = watershed(D);
imshow(label2rgb(L2,@jet,'w','shuffle'),...
'XData',[0 ix],'YData',[0 iy])
title('Watershed Segmentation')
At er having displayed the results from watershed segmentation, we
determine the number of pixels for each object using the recipe from above,
except for index
i
running from
2
to
max(objects)
since the value
1
denotes
the background and
0
denotes the boundaries of the objects. h e i rst true
object is therefore marked by the value of
2
.
objects = sortrows(L2(:),1);
max(objects)
clear objectsizes
for i = 2 : max(objects)
clear individualobject
individualobject = objects(objects == i);
objectsizes(i) = length(individualobject);
end
objectsizes = objectsizes';