Main Content

bwdistgeodesic

Geodesic distance transform of binary image

Description

D = bwdistgeodesic(BW,mask) computes the geodesic distance transform, given the binary image BW and the seed locations specified by mask. Regions where BW is true represent valid regions that can be traversed in the computation of the distance transform. Regions where BW is false represent constrained regions that cannot be traversed in the distance computation. For each true pixel in BW, the geodesic distance transform assigns a number that is the constrained distance between that pixel and the nearest true pixel in mask. Output matrix D contains geodesic distances.

example

D = bwdistgeodesic(BW,C,R) computes the geodesic distance transform of the binary image BW. Vectors C and R contain the column and row coordinates of the seed locations.

D = bwdistgeodesic(BW,idx) computes the geodesic distance transform of the binary image BW. idx is a vector of linear indices of seed locations.

D = bwdistgeodesic(___,method) computes the geodesic distance transform using an alternate distance metric specified by method.

Examples

collapse all

Create a sample binary image for this example.

BW = [1 1 1 1 1 1 1 1 1 1;...
     1 1 1 1 1 1 0 0 1 1;...
     1 1 1 1 1 1 0 0 1 1;...
     1 1 1 1 1 1 0 0 1 1;...
     0 0 0 0 0 1 0 0 1 0;...
     0 0 0 0 1 1 0 1 1 0;...
     0 1 0 0 1 1 0 0 0 0;...
     0 1 1 1 1 1 1 0 1 0;...
     0 1 1 0 0 0 1 1 1 0;...
     0 0 0 0 1 0 0 0 0 0];
 BW = logical(BW);

Create two vectors of seed locations.

C = [1 2 3 3 3];
R = [3 3 3 1 2];

Calculate the geodesic distance transform. Output pixels for which BW is false have undefined geodesic distance and contain NaN values. Because there is no connected path from the seed locations to element BW(10,5), the output D(10,5) has a value of Inf.

D = bwdistgeodesic(BW,C,R)
D = 10x10 single matrix

     2     1     0     1     2     3     4     5     6     7
     1     1     0     1     2     3   NaN   NaN     6     7
     0     0     0     1     2     3   NaN   NaN     7     7
     1     1     1     1     2     3   NaN   NaN     8     8
   NaN   NaN   NaN   NaN   NaN     3   NaN   NaN     9   NaN
   NaN   NaN   NaN   NaN     4     4   NaN    10    10   NaN
   NaN     8   NaN   NaN     5     5   NaN   NaN   NaN   NaN
   NaN     8     7     6     6     6     6   NaN     8   NaN
   NaN     8     7   NaN   NaN   NaN     7     7     8   NaN
   NaN   NaN   NaN   NaN   Inf   NaN   NaN   NaN   NaN   NaN

Input Arguments

collapse all

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

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

Seed locations, specified as a logical array of the same size as BW.

Column coordinates of seed locations, specified as a vector of positive integers of the same length as R.

Row coordinates of seed locations, specified as a vector of positive integers of the same length as C.

Linear indices of seed locations, specified as a vector of positive integers.

Distance metric, specified as one of the following.

Method

Description

'chessboard'

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

max(abs(x1-x2),abs(y1-y2))

'cityblock'

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

abs(x1-x2) + abs(y1-y2)

'quasi-euclidean'

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

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

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

Data Types: char | string

Output Arguments

collapse all

Geodesic distances, returned as a numeric array of the same size as BW.

Data Types: single

Algorithms

bwdistgeodesic uses the geodesic distance algorithm described in Soille, P., Morphological Image Analysis: Principles and Applications, 2nd Edition, Secaucus, NJ, Springer-Verlag, 2003, pp. 219–221.

Version History

Introduced in R2011b

See Also

|