The Euclidean distance transform produces an array like its argument, but with element values representing the Euclidean distance to the nearest nonzero element. The input is treated as logically binary, with all nonzero values treated as "on", and all zeroes as "off".

distanceTransform(x, ...)

# S3 method for default
distanceTransform(x, pixdim = TRUE, signed = FALSE, ...)

Arguments

x

Any object. For the default method, this must be coercible to an array.

...

Additional arguments to methods.

pixdim

An optional numeric vector or logical value. In the former case it will be taken as giving the physical size of the array elements of x along each dimension, and these will be incorporated into the distance calculation. If TRUE, the default, the "pixdim" attribute of x will be used for this purpose, if it is present. If FALSE, any such attribute will be ignored, and distances will always be counted in array elements, with all dimensions treated equally.

signed

Logical value. If TRUE, the signed distance transform is returned, such that distances from the region boundary are negative within the region and positive outside. Otherwise, distances are zero within the region.

Value

An array of the same dimension as the original, whose elements give the Euclidean distance from that element to the nearest "on" element in the original.

References

This implementation is based on the "marching parabolas" algorithm described by Felzenszwalb and Huttenlocher in the paper below.

P.F. Felzenszwalb & D.P. Huttenlocher (2012). Distance transforms of sampled functions. Theory of Computing 8(19):415-428.

Author

Jon Clayden <code@clayden.org>

Examples

x <- c(0,0,1,0,0,0,1,1,1,0,0)
distanceTransform(x)
#>  [1] 2 1 0 1 2 1 0 0 0 1 2
distanceTransform(x, pixdim=2)
#>  [1] 4 2 0 2 4 2 0 0 0 2 4