Function File: boundaries = bwboundaries(BW)
Function File: boundaries = bwboundaries(BW, conn)
Function File: boundaries = bwboundaries(BW, conn, holes)
Function File: [boundaries, labels] = bwboundaries(…)
Function File: [boundaries, labels, num_labels] = bwboundaries(…)

Trace the boundaries of the objects in a binary image.

boundaries is a cell array in which each element is the boundary of an object in the binary image BW. The clockwise boundary of each object is computed by the boundary function.

By default the boundaries are computed using 8-connectivity. This can be changed to 4-connectivity by setting conn to 4.

By default bwboundaries computes all boundaries in the image, i.e. both interior and exterior object boundaries. This behaviour can be changed through the holes input argument. If this is 'holes', both boundary types are considered. If it is instead 'noholes', only exterior boundaries will be traced.

If two or more output arguments are requested, the algorithm also returns the labelled image computed by bwlabel in labels. The number of labels in this image is optionally returned in num_labels.

See also: bwlabel, bwlabeln, bwperim, fchcode.

Demonstration 1

The following code

 ## Generate a simple image
 bw = false (100);
 bw (10:30, 40:80) = true;
 bw (40:45, 40:80) = true;

 ## Find boundaries
 bounds = bwboundaries (bw);

 ## Plot result
 imshow (bw);
 hold on
 for k = 1:numel (bounds)
   plot (bounds {k} (:, 2), bounds {k} (:, 1), 'r', 'linewidth', 2);
 endfor
 hold off

Produces the following figure

Figure 1

Package: image