Main Content


Gray-weighted distance transform of grayscale image


T = graydist(I,mask) computes the gray-weighted distance transform of the grayscale image I. Locations where mask is true are seed locations.


T = graydist(I,C,R) specifies the column and row coordinates of seed locations in vectors C and R.

T = graydist(I,ind) specifies the linear indices of seed locations, ind.

T = graydist(___,method) specifies an alternate distance metric, method.


collapse all

Create a magic square. Matrices generated by the magic function have equal row, column, and diagonal sums. The minimum path between the upper-left and lower-right corner is along the diagonal.

A  = magic(3)
A = 3×3

     8     1     6
     3     5     7
     4     9     2

Calculate the gray-weighted distance transform, specifying the upper left corner and the lower right corner of the square as seed locations.

T1 = graydist(A,1,1);
T2 = graydist(A,3,3);

Sum the two transforms to find the minimum path between the seed locations. As expected, there is a constant-value minimum path along the diagonal.

T  = T1 + T2
T = 3×3

    10    11    17
    13    10    13
    17    17    10

Input Arguments

collapse all

Grayscale image, specified as a numeric or logical array.

Binary mask that specifies seed locations, specified as a logical array the same size as I.

Column and row coordinates of seed locations, specified as a vector of positive integers. Coordinate values are valid C,R subscripts in I.

Indices of seed locations, specified as a vector of positive integers.

Distance metric, specified as one of these values.




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



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

x1x2│ + │y1y2


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

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

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

For more information, see Distance Transform of a Binary Image.

Output Arguments

collapse all

Gray-weighted distance transform, returned as a numeric array of the same size as I. If the input numeric type of I is double, then the output numeric type of T is double. If the input is any other numeric type, then the output T is single.

Data Types: single | double


graydist uses the geodesic time algorithm [1]. The basic equation for geodesic time along a path is:


method determines the chamfer weights that are assigned to the local neighborhood during outward propagation. Each pixel's contribution to the geodesic time is based on the chamfer weight in a particular direction multiplied by the pixel intensity.


[1] Soille, P. "Generalized geodesy via geodesic time." Pattern Recognition Letters. Vol.15, December 1994, pp. 1235–1240.

Version History

Introduced in R2011b