bundles / scipy 1.17.1 / scipy / ndimage / _morphology / distance_transform_edt
function
scipy.ndimage._morphology:distance_transform_edt
Signature
def distance_transform_edt ( input , sampling = None , return_distances = True , return_indices = False , distances = None , indices = None ) Summary
Exact Euclidean distance transform.
Extended Summary
This function calculates the distance transform of the input, by replacing each foreground (non-zero) element, with its shortest distance to the background (any zero-valued element).
In addition to the distance transform, the feature transform can be calculated. In this case the index of the closest background element to each foreground element is returned in a separate array.
Parameters
input: array_likeInput data to transform. Can be any type but will be converted into binary: 1 wherever input equates to True, 0 elsewhere.
sampling: float, or sequence of float, optionalSpacing of elements along each dimension. If a sequence, must be of length equal to the input rank; if a single number, this is used for all axes. If not specified, a grid spacing of unity is implied.
return_distances: bool, optionalWhether to calculate the distance transform. Default is True.
return_indices: bool, optionalWhether to calculate the feature transform. Default is False.
distances: float64 ndarray, optionalAn output array to store the calculated distance transform, instead of returning it.
return_distancesmust be True. It must be the same shape asinput.indices: int32 ndarray, optionalAn output array to store the calculated feature transform, instead of returning it.
return_indiciesmust be True. Its shape must be(input.ndim,) + input.shape.
Returns
distances: float64 ndarray, optionalThe calculated distance transform. Returned only when
return_distancesis True anddistancesis not supplied. It will have the same shape as the input array.indices: int32 ndarray, optionalThe calculated feature transform. It has an input-shaped array for each dimension of the input. See example below. Returned only when
return_indicesis True andindicesis not supplied.
Notes
The Euclidean distance transform gives values of the Euclidean distance
n y_i = sqrt(sum (x[i]-b[i])**2) i
where b[i] is the background point (value 0) with the smallest Euclidean distance to input points x[i], and n is the number of dimensions.
Array API Standard Support
distance_transform_edt has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ✅ PyTorch ✅ ⛔ JAX ⚠️ no JIT ⛔ Dask ⚠️ computes graph n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
from scipy import ndimage import numpy as np a = np.array(([0,1,1,1,1], [0,0,1,1,1], [0,1,1,1,1], [0,1,1,1,0], [0,1,1,0,0]))✓
ndimage.distance_transform_edt(a)
✗ndimage.distance_transform_edt(a, sampling=[2,1])
✗edt, inds = ndimage.distance_transform_edt(a, return_indices=True)
✓inds
✗indices = np.zeros(((np.ndim(a),) + a.shape), dtype=np.int32)
✓ndimage.distance_transform_edt(a, return_indices=True, indices=indices) indices✗
Aliases
-
scipy.ndimage.distance_transform_edt