Image Processing Toolbox User's Guide Previous page   Next Page
bwboundaries

Trace region boundaries in a binary image

Syntax

Description

B = bwboundaries(BW) traces the exterior boundaries of objects, as well as boundaries of holes inside these objects, in the binary image BW. bwboundaries also descends into the outermost objects (parents) and traces their children (objects completely enclosed by the parents). BW must be a binary image where nonzero pixels belong to an object and 0 pixels constitute the background. The following figure illustrates these components.

bwboundaries returns B, a P-by-1 cell array, where P is the number of objects and holes. Each cell in the cell array contains a Q-by-2 matrix. Each row in the matrix contains the row and column coordinates of a boundary pixel. Q is the number of boundary pixels for the corresponding region.

B = bwboundaries(BW,CONN) specifies the connectivity to use when tracing parent and child boundaries. CONN can have either of the following scalar values.

Value
Meaning
4
4-connected neighborhood
8
8-connected neighborhood. This is the default.

B = bwboundaries(BW,CONN,options) specifies an optional argument, where options can have either of the following values:

Value
Meaning
'holes'
Search for both object and hole boundaries. This is the default.
'noholes'
Search only for object (parent and child) boundaries. This can provide better performance.

[B,L] = bwboundaries(...) returns the label matrix L as the second output argument. Objects and holes are labeled. L is a two-dimensional array of nonnegative integers that represent contiguous regions. The kth region includes all elements in L that have value k. The number of objects and holes represented by L is equal to max(L(:)). The zero-valued elements of L make up the background.

[B,L,N,A] = bwboundaries(...) returns N, the number of objects found, and A, an adjacency matrix. The first N cells in B are object boundaries. A represents the parent-child-hole dependencies. A is a square, sparse, logical matrix with side of length max(L(:)), whose rows and columns correspond to the positions of boundaries stored in B.

The boundaries enclosed by a B{m} as well as the boundary enclosing B{m} can both be found using A as follows:

Class Support

BW can be logical or numeric and it must be real, two-dimensional, and nonsparse. L and N are double. A is sparse logical.

Example

Example 1

Read in and threshold an intensity image. Display the labeled objects using the jet colormap, on a gray background, with region boundaries outlined in white.

Example 2

Read in and display a binary image. Overlay the region boundaries on the image. Display text showing the region number (based on the label matrix) next to every boundary. Additionally, display the adjacency matrix using the MATLAB spy function.

After the image is displayed, use the zoom tool to read individual labels.

Example 3

Display object boundaries in red and hole boundaries in green.

Example 4

Display parent boundaries in red (any empty row of the adjacency matrix belongs to a parent) and their holes in green.

See Also

bwtraceboundary, bwlabel, bwlabeln


Previous page  bwareaopen bwdist Next page

© 1994-2005 The MathWorks, Inc.