An inspection report can point to the wrong reference mark when it measures proximity in pixels on an image whose rows and columns have different physical spacing. The pixel closest in array coordinates can be farther away on the object plane than a point two pixels away along the other axis. Multiplying an already computed distance map by a constant cannot change which point was selected.
This issue can arise in an offline inspection of a flat part when the image has been rectified or resampled onto a rectangular grid. The spacing to use belongs to the current image in the object plane. It can differ from the original sensor pitch and may change after a resize.
Put physical spacing into the calculation
SciPy's exact Euclidean distance transform measures each nonzero element's distance to the closest zero element. With no sampling argument, steps in all array dimensions have unit length. A sampling sequence specifies the spacing for each array dimension. In a conventional two-dimensional grayscale array, the sequence follows row and column order. Requesting indices also returns the coordinates of the selected zero element, so a test can verify identity as well as distance.
Build a five-by-five mask with two zero-valued reference marks. The query is at row two, column two, with coordinates counted from zero. One mark is a single row away, at row three and column two. The other is two columns to the right, at row two and column four. The row spacing is 0.6 millimeter and the column spacing is 0.2 millimeter.
import numpy as np
from scipy.ndimage import distance_transform_edt
from skimage.morphology import isotropic_dilation
mask = np.ones((5, 5), dtype=bool)
mask[3, 2] = False
mask[2, 4] = False
query = (2, 2)
spacing_mm = (0.6, 0.2)
pixels, pixel_target = distance_transform_edt(
mask, return_indices=True
)
millimeters, metric_target = distance_transform_edt(
mask, sampling=spacing_mm, return_indices=True
)
assert pixels[query] == 1
assert tuple(pixel_target[:, 2, 2]) == (3, 2)
assert np.isclose(millimeters[query], 0.4)
assert tuple(metric_target[:, 2, 2]) == (2, 4)
covered = isotropic_dilation(~mask, radius=0.5, spacing=spacing_mm)
assert covered[query]
In the pixel calculation the one-row move wins. On the sampled plane it costs 0.6 millimeter, whereas the two-column move costs 0.4 millimeter. The physical calculation therefore points to a different mark. At a 0.5-millimeter review radius, the query belongs to the neighborhood around the right-hand mark. The scikit-image dilation example inverts the mask so that the reference marks are true. Its spacing argument uses the same row and column order, and the radius is expressed in the same units as that spacing.
Check the dataset before measuring
A spacing pair describes a regular array with constant spacing along each dimension. If the image is perspective-distorted and the physical step changes across the part, a single pair of numbers cannot recover the actual plane distances. Work on a rectified plane with known grid geometry or convert relevant points to calibrated plane coordinates and calculate distances there. State the plane, units, spacing, and transform in exported results so a reviewer can reproduce the calculation.
Keep mask semantics explicit as well. In the distance transform, zeros are the candidate reference elements. If invalid pixels were encoded as zeros during a separate image operation, they can become nearer candidates than the real reference marks. Use a distinct validity mask and construct the binary input from the intended reference set. If there are no valid targets for a frame, record that condition explicitly instead of using a distance result as evidence of a match.
Check distance and identity together
Use unequal row and column spacing in a small fixture with a known nearest coordinate. A square grid or a target that wins in both metrics may fail to reveal an axis swap. Include a case like this one, where physical spacing changes the winner, and check a point inside the review radius and one outside it. Also check what category the target belongs to if the reference mask is assembled from more than one class.
These distances are between sampled element locations, which can represent pixel centers in the rectified plane. A report that needs the distance to a fitted contour or the edge of a feature should document and use that geometry explicitly. An overlay should show the query and selected reference mark beside the distance, allowing reviewers to spot a plausible number tied to the wrong point.
Sources
SciPy API reference, exact Euclidean distance transform with sampling and indices
https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.distance_transform_edt.html
scikit-image API reference, binary isotropic dilation with spacing
https://scikit-image.org/docs/stable/api/skimage.morphology.html#skimage.morphology.isotropic_dilation