Note: This page has been translated by MathWorks. Please click here

To view all translated materials including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materials including this page, select Japan from the country navigator on the bottom of this page.

Distance transform of binary image

`D = bwdist(BW)`

[D,IDX] = bwdist(BW)

[D,IDX] = bwdist(BW,* method*)

[gpuarrayD, gpuarrayIDX]= bwdist(gpuarrayBW)

`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,IDX] = bwdist(BW)`

also
computes the closest-pixel map in the form of an index array, `IDX`

.
(The closest-pixel map is also called the feature map, feature transform,
or nearest-neighbor transform.) `IDX`

has the same
size as `BW`

and `D`

. Each element
of `IDX`

contains the linear index of the nearest
nonzero pixel of `BW`

.

`[D,IDX] = bwdist(BW,`

computes
the distance transform, where * method*)

`method`

`method`

`[gpuarrayD, gpuarrayIDX]= bwdist(gpuarrayBW)`

computes
the Euclidean distance transform of the binary image `gpuarrayBW`

,
performing the operation on a GPU. The images must be 2-D and have
less than 2^{32-1} elements. In addition,
you can only compute the Euclidean distance metric. This syntax requires
the Parallel
Computing Toolbox™.

`BW`

can be numeric or logical, and it must
be nonsparse. `D`

is a single matrix with the same
size as `BW`

. The class of `IDX`

depends
on the number of elements in the input image, and is determined using
the following table.

Class | Range |
---|---|

`'uint32'` | `numel(BW)` <= 2^{32} −
1 |

`'uint64'` | `numel(BW)` >= 2^{32} |

`gpuarrayBW`

can be a 2-D gpuArray of type `uint8`

, `uint16`

, `uint32`

, `int8`

, `int16`

, `int32`

, `single`

, `double`

or `logical`

. `gpuarrayD`

is
a gpuArray with the same size as `gpuarrayBW`

and
underlying class `single`

. `gpuarrayIDX`

is
a gpuArray with the same size as `gpuarrayBW`

and
underlying class `uint32`

.

`bwdist`

uses fast algorithms to compute the true Euclidean distance transform, especially in the 2-D case. The other methods are provided primarily for pedagogical reasons. However, the alternative distance transforms are sometimes significantly faster for multidimensional input images, particularly those that have many nonzero elements.The function

`bwdist`

changed in version 6.4 (R2009b). Previous versions of the Image Processing Toolbox used different algorithms for computing the Euclidean distance transform and the associated label matrix. If you need the same results produced by the previous implementation, use the function`bwdist_old`

.

For Euclidean distance transforms, `bwdist`

uses
the fast algorithm described in

[1] Maurer, Calvin, Rensheng Qi, and Vijay Raghavan, "A Linear
Time Algorithm for Computing Exact Euclidean Distance Transforms of
Binary Images in Arbitrary Dimensions," *IEEE Transactions
on Pattern Analysis and Machine Intelligence*, Vol. 25,
No. 2, February 2003, pp. 265-270.

For cityblock, chessboard, and quasi-Euclidean distance transforms, `bwdist`

uses
the two-pass, sequential scanning algorithm described in

[2] Rosenfeld, Azriel and John Pfaltz, "Sequential operations
in digital picture processing," *Journal of the Association
for Computing Machinery*, Vol. 13, No. 4, 1966, pp. 471-494.

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

[3] Paglieroni, David, "Distance Transforms: Properties and
Machine Vision Applications," *Computer Vision, Graphics,
and Image Processing: Graphical Models and Image Processing*,
Vol. 54, No. 1, January 1992, pp. 57-58.

Was this topic helpful?