A distance map can look plausible while answering the wrong geometric question. In an offline robot-vision inspection, an analyst may count rows and columns as equal steps even though their physical spacing differs. Adding a unit label afterward does not repair that choice. The nearest background sample itself can change when the two directions receive different weights.

This article builds a small synthetic mask with only two background samples. It compares ordinary index distance with a declared physical metric, checks both the distance and the selected sample identity, and records the assumptions needed to reproduce the result. The example concerns discrete image coordinates; it is not a robot clearance assessment or a measurement from a production camera.

What the function is asked to find

SciPy's distance_transform_edt replaces nonzero input locations with their shortest Euclidean distance to a zero location. Its sampling argument sets spacing along array axes. With return_indices=True, the function also returns the array coordinates of a selected nearest background sample. The default spacing is one along every axis. These details are documented in SciPy's primary reference [1].

For this example, call the axes row and column. Rows are axis zero, columns are axis one, and every coordinate below is zero-based. Define a 5-by-6 Boolean mask that is True everywhere except A=(1,2) and B=(3,5). Set those two locations to False. The query Q=(3,2) remains True. Nothing outside this finite mask is introduced as an additional background point.

The unweighted comparison is easy to inspect. Q and A share a column and differ by two rows, giving distance 2. Q and B share a row and differ by three columns, giving distance 3. A is the unique nearest background sample under unit grid spacing. No tie rule is needed to explain the result.

Change the metric, then compare again

Now declare a row spacing of 2 millimeters and a column spacing of 1 millimeter. This is an assumed orthogonal sampling grid for the example, not a calibration estimate. The physical difference from Q to A is four millimeters along the row direction. The difference from Q to B is three millimeters along the column direction. B becomes the unique nearest sample.

The array, query, and background labels did not move. Only the meaning of an index increment changed. A procedure that first chooses A under unit spacing and then measures the Q-to-A vector in millimeters produces 4. That vector length is correctly calculated, but A is no longer the minimizing candidate. The correct minimum in the newly declared metric is 3.

For an arbitrary candidate (r,c), the squared distance in this example is 4*(r-3)**2 + (c-2)**2. Minimization must use that expression across all eligible candidates. A single stored unweighted minimum does not preserve the directional differences needed to redo the comparison. Retaining only the old winner loses additional information.

Reproduce the original fixture

The following code constructs the complete mask, rather than relying on an unstated image threshold or crop:

import numpy as np

from scipy.ndimage import distance_transform_edt

mask = np.ones((5, 6), dtype=bool)

mask[1, 2] = False

mask[3, 5] = False

q = (3, 2)

unit_d, unit_i = distance_transform_edt(

mask, return_indices=True)

metric_d, metric_i = distance_transform_edt(

mask, sampling=(2.0, 1.0), return_indices=True)

assert unit_d[q] == 2.0

assert tuple(unit_i[:, 3, 2]) == (1, 2)

assert metric_d[q] == 3.0

assert tuple(metric_i[:, 3, 2]) == (3, 5)

Local execution with SciPy 1.18.1, NumPy 2.5.2, and Python 3.14.7 produced those values. The official pages consulted identify themselves as SciPy 1.18.0 documentation. Both distance and feature identity are checked because a distance-only report can hide an incorrect association with a background label.

Use a second implementation for a small check

SciPy also provides distance_transform_bf, a brute-force distance transform. Its Euclidean mode accepts axis spacing; other available metrics do not use that parameter in the same way. The reference describes this as a slow algorithm [2]. That makes a tiny fixture a useful setting for an implementation cross-check without making a runtime claim about large images.

from scipy.ndimage import distance_transform_bf

reference = distance_transform_bf(

mask, metric='euclidean', sampling=(2.0, 1.0))

np.testing.assert_allclose(metric_d, reference)

The two distance arrays agreed across all 30 cells in this fixture. The test does not require the two implementations to choose the same background identity in every possible tie elsewhere in the mask. At Q, the arithmetic above establishes a unique winner independently of either implementation.

Uniform scaling is a different case

As a separate control, give both axes spacing 3. Every candidate distance becomes three times its unit-spacing distance, so the minimizing comparison is preserved. The executed fixture confirmed that the resulting distance array equals three times unit_d. This control helps distinguish uniform changes of units from unequal axis weighting.

Do not use that control to justify arbitrary anisotropic rescaling. In the main example, multiplying 2 by the row spacing gives 4, while multiplying by the column spacing gives 2. Neither operation repeats the full weighted nearest-point search. An unexplained average spacing would introduce yet another metric.

Keep geometry assumptions beside the output

A useful review record contains the mask identity, zero/one meaning, axis names, spacing vector, units, query, selected background coordinate, and software versions. If the image is transposed, the spacing vector must follow the new axis order. If the data are resized or reprojected, the reviewer should establish the new coordinate interpretation before calculating another distance map.

For this fixture, millimeters describe a rectangular orthogonal grid. A perspective camera image does not acquire uniform millimeter spacing merely because a field is named sampling. Likewise, distance to a zero sample center does not automatically equal distance to a continuous object boundary. The distinction should remain visible when an inspection dashboard presents the result to someone who did not build the mask.

The practical acceptance check is therefore specific: under the declared spacing (2,1), Q selects B and reports 3 millimeters. Preserve that complete statement, including its coordinate model, instead of recording only a number and a unit.

Sources

[1] SciPy, distance_transform_edt: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.distance_transform_edt.html

[2] SciPy, distance_transform_bf: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.distance_transform_bf.html