API Reference
This page documents all the exported functions in py-distance-transforms.
Core Functions
transform
transform(arr)Computes the squared Euclidean distance transform of a binary array.
Parameters: - arr : numpy.ndarray - A binary array (0s and 1s) of any dimension
Returns: - numpy.ndarray - The squared Euclidean distance transform
Example:
import numpy as np
from py_distance_transforms import transform
arr = np.array([
[0, 1, 1, 0, 1],
[0, 0, 0, 1, 0],
[1, 1, 0, 0, 0]
], dtype=np.float32)
result = transform(arr)
print(result)transform_cuda
transform_cuda(tensor)GPU-accelerated version of the distance transform function.
Parameters: - tensor : torch.Tensor - A binary tensor (0s and 1s) of any dimension, located on a CUDA device
Returns: - torch.Tensor - The squared Euclidean distance transform, located on the same CUDA device
Example:
import torch
from py_distance_transforms import transform_cuda
# Create a tensor on GPU
tensor = torch.zeros((10, 10), device='cuda')
tensor[2, 2] = 1
tensor[7, 7] = 1
result = transform_cuda(tensor)
print(result)Utility Functions
numpy_to_torch
numpy_to_torch(arr, device='cuda')Converts a NumPy array to a PyTorch tensor on the specified device.
Parameters: - arr : numpy.ndarray - Input NumPy array - device : str, optional - Device to place the tensor on. Default is ‘cuda’
Returns: - torch.Tensor - PyTorch tensor on the specified device
torch_to_numpy
torch_to_numpy(tensor)Converts a PyTorch tensor to a NumPy array.
Parameters: - tensor : torch.Tensor - Input PyTorch tensor
Returns: - numpy.ndarray - NumPy array
Full Function List
| Function | Description |
|---|---|
transform |
Computes squared Euclidean distance transform on CPU |
transform_cuda |
Computes squared Euclidean distance transform on GPU |
numpy_to_torch |
Converts NumPy array to PyTorch tensor |
torch_to_numpy |
Converts PyTorch tensor to NumPy array |
Integration with Other Libraries
NumPy
py-distance-transforms works seamlessly with NumPy arrays:
import numpy as np
from py_distance_transforms import transform
# Create a NumPy array
arr = np.zeros((100, 100), dtype=np.float32)
arr[25:75, 25:75] = 1
# Apply transform
result = transform(arr)PyTorch
For PyTorch integration, use the CUDA-specific functions:
import torch
from py_distance_transforms import transform_cuda
# Create a PyTorch tensor
tensor = torch.zeros((100, 100), device='cuda')
tensor[25:75, 25:75] = 1
# Apply transform
result = transform_cuda(tensor)scikit-image
Works alongside scikit-image for more advanced image processing:
import numpy as np
from skimage import io, color, morphology
from py_distance_transforms import transform
# Load and process image
img = io.imread('image.png', as_gray=True)
binary = img > 0.5
# Apply morphological operations
cleaned = morphology.remove_small_objects(binary)
# Apply distance transform
dist = transform(cleaned.astype(np.float32))