This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals 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,method) computes the distance transform, where method specifies an alternate distance metric. method can take any of the following values.




In 2-D, the chessboard distance between (x1,y1) and (x2,y2) is max(│x1x2│,│y1y2│).


In 2-D, the cityblock distance between (x1,y1) and (x2,y2) is │x1x2│ + │y1y2│.


In 2-D, the Euclidean distance between (x1,y1) and (x2,y2) is


This is the default method.


In 2-D, the quasi-Euclidean distance between (x1,y1) and (x2,y2) is

|x1x2|+(21)|y1y2|, |x1x2|>|y1y2|

(21)|x1x2|+|y1y2|, otherwise.

[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 232-1 elements. In addition, you can only compute the Euclidean distance metric. This syntax requires the Parallel Computing Toolbox™.

Code Generation support: Yes.

MATLAB Function Block support: Yes.

Class Support

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.

'uint32'numel(BW) <= 232 − 1
'uint64'numel(BW) >= 232

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.


collapse all

Create an image.

bw = zeros(5,5); 
bw(2,2) = 1; 
bw(4,4) = 1
bw =
     0     0     0     0     0
     0     1     0     0     0
     0     0     0     0     0
     0     0     0     1     0
     0     0     0     0     0

Calculate the distance transform.

[D,IDX] = bwdist(bw)
D =
    1.4142    1.0000    1.4142    2.2361    3.1623
    1.0000         0    1.0000    2.0000    2.2361
    1.4142    1.0000    1.4142    1.0000    1.4142
    2.2361    2.0000    1.0000         0    1.0000
    3.1623    2.2361    1.4142    1.0000    1.4142


     7     7     7     7     7
     7     7     7     7    19
     7     7     7    19    19
     7     7    19    19    19
     7    19    19    19    19

In the nearest-neighbor matrix IDX 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.

Create an image.

bw = gpuArray.zeros(5,5); 
bw(2,2) = 1; 
bw(4,4) = 1;

Calculate the distance transform.

[D,IDX] = bwdist(bw)

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

bw = zeros(200,200);
bw(50,50) = 1; bw(50,150) = 1; bw(150,100) = 1;
D1 = bwdist(bw,'euclidean');
D2 = bwdist(bw,'cityblock');
D3 = bwdist(bw,'chessboard');
D4 = bwdist(bw,'quasi-euclidean');
RGB1 = repmat(mat2gray(D1), [1 1 3]);
RGB2 = repmat(mat2gray(D2), [1 1 3]);
RGB3 = repmat(mat2gray(D3), [1 1 3]);
RGB4 = repmat(mat2gray(D4), [1 1 3]);
subplot(2,2,1), imshow(RGB1), title('Euclidean')
hold on, imcontour(D1)
subplot(2,2,2), imshow(RGB2), title('City block')
hold on, imcontour(D2)
subplot(2,2,3), imshow(RGB3), title('Chessboard')
hold on, imcontour(D3)
subplot(2,2,4), imshow(RGB4), title('Quasi-Euclidean')
hold on, imcontour(D4)

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

bw = zeros(50,50,50); bw(25,25,25) = 1;
D1 = bwdist(bw);
D2 = bwdist(bw,'cityblock');
D3 = bwdist(bw,'chessboard');
D4 = bwdist(bw,'quasi-euclidean');
subplot(2,2,1), isosurface(D1,15), axis equal, view(3)
camlight, lighting gouraud, title('Euclidean')
subplot(2,2,2), isosurface(D2,15), axis equal, view(3)
camlight, lighting gouraud, title('City block')
subplot(2,2,3), isosurface(D3,15), axis equal, view(3)
camlight, lighting gouraud, title('Chessboard')
subplot(2,2,4), isosurface(D4,15), axis equal, view(3)
camlight, lighting gouraud, title('Quasi-Euclidean')

More About

collapse all

Code Generation

This function supports the generation of C code using MATLAB® Coder™. The code generated for this function uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Understanding Code Generation with Image Processing Toolbox.

When generating code, the optional second input argument, method, must be a compile-time constant. Input images must have fewer than 232 pixels.

MATLAB Function Block

You can use this function in the MATLAB Function Block in Simulink.


  • 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.

Introduced before R2006a

Was this topic helpful?