Python bindings for the Julia DistanceTransforms.jl package, providing efficient Euclidean distance transforms for arrays.
pip install distance_transforms- Python 3.11+
- Julia 1.10+ or 1.11
- Required Julia packages will be automatically installed on first use
import numpy as np
import distance_transforms as dts
# Create a binary array (1's for foreground, 0's for background)
arr = np.zeros((5, 5), dtype=np.uint8)
arr[2, 2] = 1 # Set center point as foreground
# Compute distance transform
# Returns squared distances from each point to the nearest foreground point
distances = dts.transform(arr)
print(distances)If you have CUDA available and PyTorch installed:
import torch
import distance_transforms as dts
# Create a binary tensor on GPU
tensor = torch.zeros((5, 5), dtype=torch.bool, device="cuda")
tensor[2, 2] = True
# Compute distance transform on GPU
distances = dts.transform_cuda(tensor)
print(distances)This package is a Python wrapper around DistanceTransforms.jl, which provides efficient implementations of Euclidean distance transforms. The distance transform computes, for each pixel in an image, the squared Euclidean distance to the nearest non-zero pixel.
On first use, the package will set up the required Julia environment automatically.
- Fast CPU implementation using the Felzenszwalb algorithm
- Optional GPU acceleration with CUDA support
- Simple, NumPy-compatible API
- Support for multidimensional arrays
For comprehensive documentation on the algorithms and detailed usage, please refer to the DistanceTransforms.jl documentation.
MIT