Skip to main content

torch-distmap

Euclidean distance transform in PyTorch.

This is an implementation of the algorithm from the paper

"Distance Transforms of Sampled Functions"
Pedro F. Felzenszwalb & Daniel P. Huttenlocher
Theory of Computing (2012)

Although it is in PyTorch, our implementation performs loops across voxels and hence quite slow. Moreover, it takes masks as an input and therefore does not allow backpropagation.

Installation

Dependency

  • torch >= 1.3

Conda

conda install torch-distmap -c balbasty -c pytorch

Pip

pip install torch-distmap

Example

See our demo notebook

API

euclidean_distance_transform(x, ndim=None, vx=1)
"""Compute the Euclidean distance transform of a binary image

Parameters
----------
x : (..., *spatial) tensor
    Input tensor. Zeros will stay zero, and the distance will
    be propagated into nonzero voxels.
ndim : int, default=`x.dim()`
    Number of spatial dimensions
vx : [sequence of] float, default=1
    Voxel size

Returns
-------
d : (..., *spatial) tensor
    Distance map
"""
euclidean_signed_transform(x, ndim=None, vx=1)
"""Compute the signed Euclidean distance transform of a binary image

Parameters
----------
x : (..., *spatial) tensor
    Input tensor.
    A negative distance will propagate into zero voxels and
    a positive distance will propagate into nonzero voxels.
ndim : int, default=`x.dim()`
    Number of spatial dimensions
vx : [sequence of] float, default=1
    Voxel size

Returns
-------
d : (..., *spatial) tensor
    Signed distance map
"""
l1_distance_transform(x, ndim=None, vx=1)
"""Compute the L1 distance transform of a binary image

Parameters
----------
x : (..., *spatial) tensor
    Input tensor. Zeros will stay zero, and the distance will
    be propagated into nonzero voxels.
ndim : int, default=`x.dim()`
    Number of spatial dimensions
vx : [sequence of] float, default=1
    Voxel size

Returns
-------
d : (..., *spatial) tensor
    Distance map
"""
l1_signed_transform(x, ndim=None, vx=1)
"""Compute the signed L1 distance transform of a binary image

Parameters
----------
x : (..., *spatial) tensor
    Input tensor.
    A negative distance will propagate into zero voxels and
    a positive distance will propagate into nonzero voxels.
ndim : int, default=`x.dim()`
    Number of spatial dimensions
vx : [sequence of] float, default=1
    Voxel size

Returns
-------
d : (..., *spatial) tensor
    Signed distance map
"""

Related packages

"A linear time algorithm for computing exact euclidean distance transforms of binary images in arbitrary dimensions"
C. R. Maurer, Jr., R. Qi, V. Raghavan
IEEE Trans. PAMI 25, 265-270, (2003)

Release files for torch-distmap 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torch-distmap 0.3.0
File Size Uploaded
torch_distmap-0.3.0.tar.gz 25.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for torch-distmap 0.3.0
File Interpreter ABI Platform
torch_distmap-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 37.5 kB

Release files / torch_distmap-0.3.0.tar.gz

Download URL torch_distmap-0.3.0.tar.gz
Size 25.9 kB
Tags Source
SHA-256 checksum
How to use checksums
38f02f9891aae7c2262aba4f85be88ea3acc3c3eddb66c50423ea09a865b6de2
BLAKE2b-256 checksum
How to use checksums
9cfcf36cddcb73abc4b2edfa3010921b84c009b4bce2126684abc88c1325c541
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release files / torch_distmap-0.3.0-py3-none-any.whl

Download URL torch_distmap-0.3.0-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f55dc660901664b46bbd1f519f2b04af560ea674588aa38ae151aabafa2a11ff
BLAKE2b-256 checksum
How to use checksums
6c3c11fad2497d424cc27ab7591a35cd7311b77beee59ee618f4f4804a4434bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page