Euclidean Distance Transform 3D

raw JSON →
3.1.1 verified Fri May 01 auth: no python

Computes the Euclidean distance transform of 3D and 2D label fields. Supports anisotropic voxels (non-cubic) and multi-label segmentation. v3.1.1, updated June 2024, active development with irregular releases.

pip install edt
error ImportError: cannot import name 'edt' from 'edt'
cause Trying to import edt as from edt import edt, but edt is the module itself.
fix
Use just 'import edt' then call 'edt.edt()'.
error ValueError: ndim is 2 but expected 3
cause Passed a 2D array to edt.edt without specifying ndim=2 or using the 2D variant.
fix
For 2D arrays, use edt.edt(..., ndim=2) or call edt.edt2d() if available (not in current version).
error TypeError: anisotropic must be a tuple of floats
cause Anisotropic parameter passed as list or wrong type.
fix
Pass anisotropic as a tuple of three floats, e.g., (0.5, 0.5, 1.0).
gotcha edt.edt expects uint32 or uint64 labels by default. Passing int32 or float labels will silently produce incorrect results.
fix Convert labels to np.uint32 or np.uint64 before calling edt.edt.
gotcha Anisotropic parameter must be a tuple of three floats (for 3D) or two floats (for 2D). Omitting or using wrong shape leads to undefined behavior.
fix Always provide anisotropic as (sx, sy, sz) tuple for 3D, or (sx, sy) for 2D.
gotcha edt.edt modifies the input array in-place if the 'in_place' parameter is True (default False). Unexpected side effects may occur.
fix If you need to preserve the original array, set in_place=False (the default) or pass a copy.
deprecated The 'parallel' parameter (int) was deprecated in 3.0.0 and removed in 3.1.0. Use 'num_threads' instead.
fix Use num_threads=N instead of parallel=N.

Compute Euclidean distance transform for a 3D label array. Use anisotropic parameter to set voxel size.

import edt
import numpy as np

labels = np.array([[[0, 1, 0], [1, 0, 0], [0, 0, 2]]], dtype=np.uint32)
dist = edt.edt(labels, anisotropic=(1.0, 1.0, 1.0))
print(dist)
# Output: 3D distance transform (x, y, z axes)