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

Distance transform

Syntax

Description

D = bwdist(BW) computes the Euclidean distance transform of the binary image BW. For each pixel in BW, the distance transform assigns a number that is the distance between that pixel and the nearest nonzero pixel of BW. bwdist uses the Euclidean distance metric by default. BW can have any dimension. D is the same size as BW.

[D,L] = bwdist(BW) also computes the nearest-neighbor transform and returns it as label matrix L, which has the same size as BW and D. Each element of L contains the linear index of the nearest nonzero pixel of BW.

[D,L] = bwdist(BW,METHOD) computes the distance transform, where METHOD specifies an alternate distance metric. METHOD can take any of these values:

Method
Description
'chessboard'
In 2-D, the chessboard distance between (x1,y1) and (x2,y2) is


'cityblock'
In 2-D, the cityblock distance between (x1,y1) and (x2,y2) is
'euclidean'
In 2-D, the Euclidean distance between (x1,y1) and (x2,y2) is
This is the default method.
'quasi-euclidean'
In 2-D, the quasi-Euclidean distance between (x1,y1) and (x2,y2) is

The METHOD string can be abbreviated.

Class Support

BW can be numeric or logical, and it must be nonsparse. D and L are double matrices with the same size as BW.

Example

Here is a simple example of the Euclidean distance transform.

In the nearest-neighbor matrix L the values 7 and 19 represent the position of the nonzero elements using linear matrix indexing. If a pixel contains a 7, its closest nonzero neighbor is at linear position 7.

This example compares the 2-D distance transforms for each of the supported distance methods. In the figure, note how the quasi-Euclidean distance transform best approximates the circular shape achieved by the Euclidean distance method.

This example compares isosurface plots for the distance transforms of a 3-D image containing a single nonzero pixel in the center.

Algorithm

For two-dimensional Euclidean distance transforms, bwdist uses the second algorithm described in

For higher dimensional Euclidean distance transforms, bwdist uses a nearest-neighbor search on an optimized kd-tree, as described in

For cityblock, chessboard, and quasi-Euclidean distance transforms, bwdist uses the two-pass, sequential scanning algorithm described in

The different distance measures are achieved by using different sets of weights in the scans, as described in

See Also

watershed


Previous page  bwboundaries bweuler Next page

© 1994-2005 The MathWorks, Inc.