Skip to content

Distance Transforms

torchmorph.euclidean_distance_transform

euclidean_distance_transform(
    input,
    sampling=None,
    return_distances=True,
    return_indices=False,
    distances=None,
    indices=None,
)

Compute the exact Euclidean distance to the nearest background point

Distances are computed independently for each batch and channel. Nonzero input values are foreground and zero values are background.

Parameters:

Name Type Description Default
input Tensor

CUDA tensor with shape (B, C, Spatial...).

required
sampling float or sequence[float]

Positive spacing along each spatial axis. A scalar or one-element sequence is broadcast.

None
return_distances bool

Return a newly allocated distance tensor unless distances is supplied.

True
return_indices bool

Return a newly allocated nearest-background index tensor unless indices is supplied.

False
distances Tensor

Preallocated distance output with the same shape and device as input. Supplying it enables distance computation regardless of return_distances.

None
indices Tensor

Preallocated index output with shape (spatial_ndim, *input.shape) on the input device. Supplying it enables index computation regardless of return_indices.

None

Returns:

Type Description
Tensor | tuple[Tensor, Tensor] | None

torch.Tensor, tuple[torch.Tensor, torch.Tensor], or None: Requested

Tensor | tuple[Tensor, Tensor] | None

newly allocated outputs. Distances have float32 dtype and input

Tensor | tuple[Tensor, Tensor] | None

shape; indices have int32 dtype and shape

Tensor | tuple[Tensor, Tensor] | None

(spatial_ndim, *input.shape). Preallocated outputs are filled but

Tensor | tuple[Tensor, Tensor] | None

omitted from the return value, so the function returns None when

Tensor | tuple[Tensor, Tensor] | None

every requested output is preallocated.

Example
>>> import torch
>>> import torchmorph as tm
>>> x = torch.tensor([[[0, 1, 1, 0]]], device="cuda")
>>> tm.euclidean_distance_transform(x)
tensor([[[0., 1., 1., 0.]]], device='cuda:0')

torchmorph.chamfer_distance_transform

chamfer_distance_transform(
    input,
    metric="chessboard",
    return_distances=True,
    return_indices=False,
    distances=None,
    indices=None,
)

Compute a chamfer distance to the nearest background point

Distances are computed independently for each batch and channel. Nonzero input values are foreground and zero values are background.

Parameters:

Name Type Description Default
input Tensor

CUDA tensor with shape (B, C, Spatial...).

required
metric str

"chessboard" or "taxicab". "cityblock" and "manhattan" are accepted aliases for "taxicab".

'chessboard'
return_distances bool

Return a newly allocated distance tensor unless distances is supplied.

True
return_indices bool

Return a newly allocated nearest-background index tensor unless indices is supplied.

False
distances Tensor

Preallocated distance output with the same shape and device as input.

None
indices Tensor

Preallocated index output with shape (spatial_ndim, *input.shape) on the input device.

None

Returns:

Type Description
Tensor | tuple[Tensor, Tensor] | None

torch.Tensor, tuple[torch.Tensor, torch.Tensor], or None: Requested

Tensor | tuple[Tensor, Tensor] | None

newly allocated outputs. Distances are float32 and indices are

Tensor | tuple[Tensor, Tensor] | None

int32. Preallocated outputs are filled but omitted from the return

Tensor | tuple[Tensor, Tensor] | None

value.

Example
>>> import torch
>>> import torchmorph as tm
>>> x = torch.tensor([[[0, 1, 1, 0]]], device="cuda")
>>> tm.chamfer_distance_transform(x, metric="taxicab")
tensor([[[0., 1., 1., 0.]]], device='cuda:0')

torchmorph.brute_force_distance_transform

brute_force_distance_transform(
    input,
    metric="euclidean",
    sampling=None,
    return_distances=True,
    return_indices=False,
    distances=None,
    indices=None,
)

Compute distances to background points by exhaustive search

This reference-style transform supports Euclidean, taxicab, and chessboard metrics and computes each batch and channel independently.

Parameters:

Name Type Description Default
input Tensor

CUDA tensor with shape (B, C, Spatial...); nonzero values are foreground and zeros are background.

required
metric str

One of "euclidean", "taxicab", or "chessboard".

'euclidean'
sampling float or sequence[float]

Positive spatial spacing. A scalar or one-element sequence is broadcast to every axis.

None
return_distances bool

Return a newly allocated distance tensor unless distances is supplied.

True
return_indices bool

Return a newly allocated nearest-background index tensor unless indices is supplied.

False
distances Tensor

Preallocated distance output with the same shape and device as input.

None
indices Tensor

Preallocated index output with shape (spatial_ndim, *input.shape) on the input device.

None

Returns:

Type Description
Tensor | tuple[Tensor, Tensor] | None

torch.Tensor, tuple[torch.Tensor, torch.Tensor], or None: Requested

Tensor | tuple[Tensor, Tensor] | None

newly allocated outputs. Distances are float32 and indices are

Tensor | tuple[Tensor, Tensor] | None

int32. Preallocated outputs are filled but omitted from the return

Tensor | tuple[Tensor, Tensor] | None

value.

Example
>>> import torch
>>> import torchmorph as tm
>>> x = torch.tensor([[[0, 1, 1, 0]]], device="cuda")
>>> tm.brute_force_distance_transform(x, metric="euclidean")
tensor([[[0., 1., 1., 0.]]], device='cuda:0')