Main Content

bwdist

Distance transform of binary image

Description

example

D= bwdist(BW)computes the Euclidean distance transform of the binary imageBW. For each pixel inBW, the distance transform assigns a number that is the distance between that pixel and the nearest nonzero pixel ofBW.

[D,idx] = bwdist(BW)also computes the closest-pixel map in the form of an index array,idx. Each element ofidxcontains the linear index of the nearest nonzero pixel ofBW. The closest-pixel map is also called the feature map, feature transform, or nearest-neighbor transform.

[D,idx] = bwdist(BW,method)computes the distance transform using an alternate distance metric, specified bymethod.

Examples

collapse all

This example shows how to compute the Euclidean distance transform of a binary image, and the closest-pixel map of the image.

Create a binary image.

bw = zeros(5,5); bw(2,2) = 1; bw(4,4) = 1
bw =5×50 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 =5x5 single matrix1.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
IDX =5x5 uint32 matrix7 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 matrixIDXthe 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 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(rescale(D1), [1 1 3]); RGB2 = repmat(rescale(D2), [1 1 3]); RGB3 = repmat(rescale(D3), [1 1 3]); RGB4 = repmat(rescale(D4), [1 1 3]); figure subplot(2,2,1), imshow(RGB1), title('Euclidean') holdon, imcontour(D1) subplot(2,2,2), imshow(RGB2), title('Cityblock') holdon, imcontour(D2) subplot(2,2,3), imshow(RGB3), title('Chessboard') holdon, imcontour(D3) subplot(2,2,4), imshow(RGB4), title('Quasi-Euclidean') holdon, imcontour(D4)

Figure contains 4 axes objects. Axes object 1 with title Euclidean contains 2 objects of type image, contour. Axes object 2 with title Cityblock contains 2 objects of type image, contour. Axes object 3 with title Chessboard contains 2 objects of type image, contour. Axes object 4 with title Quasi-Euclidean contains 2 objects of type image, contour.

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'); figure subplot(2,2,1), isosurface(D1,15), axisequal, view(3) camlight, lightinggouraud, title('Euclidean') subplot(2,2,2), isosurface(D2,15), axisequal, view(3) camlight, lightinggouraud, title('City block') subplot(2,2,3), isosurface(D3,15), axisequal, view(3) camlight, lightinggouraud, title('Chessboard') subplot(2,2,4), isosurface(D4,15), axisequal, view(3) camlight, lightinggouraud, title('Quasi-Euclidean')

Figure contains 4 axes objects. Axes object 1 with title Euclidean contains an object of type patch. Axes object 2 with title City block contains an object of type patch. Axes object 3 with title Chessboard contains an object of type patch. Axes object 4 with title Quasi-Euclidean contains an object of type patch.

Input Arguments

collapse all

Binary image, specified as a numeric or logical array of any dimension.For numeric input, any nonzero pixels are considered to be1(true).

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical

Distance metric, specified as one of the these values.

Method

Description

'chessboard'

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

max(│x1x2│,│y1y2│).

'cityblock'

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

x1x2│ + │y1y2

'euclidean'

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

( x 1 x 2 ) 2 + ( y 1 y 2 ) 2 .

'quasi-euclidean'

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

| x 1 x 2 | + ( 2 1 ) | y 1 y 2 | , | x 1 x 2 | > | y 1 y 2 |

( 2 1 ) | x 1 x 2 | + | y 1 y 2 | , otherwise .

For more information, seeDistance Transform of a Binary Image.

Data Types:char|string

Output Arguments

collapse all

Distance, returned as a numeric array of the same size asBW. The value of each element is the distance between that pixel and the nearest nonzero pixel inBW, as defined by the distance metric,method.

Data Types:single

Index array, returned as a numeric array of the same size asBW. Each element ofidxcontains the linear index of the nearest nonzero pixel ofBW. The class ofidxdepends on the number of elements in the input image, and is determined as follows.

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

Data Types:uint32|uint64

Tips

  • bwdistuses 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 functionbwdist更改为6.4版(R2009b)。图像处理工具箱的先前版本使用不同的算法来计算欧几里得距离变换和相关的标签矩阵。如果您需要上一个实现产生相同的结果,请使用该功能bwdist_old.

Algorithms

  • For Euclidean distance transforms,bwdistuses the fast algorithm.[1]

  • For cityblock, chessboard, and quasi-Euclidean distance transforms,bwdistuses the two-pass, sequential scanning algorithm.[2]

  • 不同的措施是通过我们的距离ing different sets of weights in the scans, as described in[3].

参考

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

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

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

Extended Capabilities

Introduced before R2006a