Skip to content

fix: treat any non-zero pixel as foreground in distance_transform - #242

Merged
petercorke merged 4 commits into
mainfrom
bug/distance-transform-foreground
Nov 22, 2026
Merged

petercorke merged 4 commits into
mainfrom
bug/distance-transform-foreground

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Summary

Image.distance_transform() inverted the image as 255 - x before calling OpenCV, so it only worked on images whose pixels were 0 or 255. For any other values there was no zero pixel left for OpenCV to measure from, and the result was about 65000 everywhere. That includes the example in its own docstring, which marks pixels with the value 1.

Any non-zero pixel is now foreground. The function builds a 0/1 image from x == 0 (or x != 0 for invert=True) instead of inverting. For images that are 0 and 255, results are unchanged. The docstring says that any non-zero value counts as foreground, and has a .. versionchanged:: 2.5.0 note.

Tests

New tests: a single foreground pixel with the value 1, 7, 128, 254 or 255 gives the same result as 255, with and without invert, and an image with two foreground pixels of different values (3 and 200) measures the distance to each. tests/test_image_spatial_extra.py and tests/test_image_spatial.py pass and mypy reports no errors. I have not run the full suite locally. CI will.

This change was written with AI assistance (Claude Code) and I have reviewed the diff.

🤖 Generated with Claude Code

distance_transform() inverted the image as 255 - x, so it only worked on
images whose pixels were 0 or 255. For any other values there was no zero
pixel left and the result was about 65000 everywhere, including for the
example in its own docstring, which uses the value 1.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

petercorke and others added 3 commits November 23, 2026 08:31
The test used an image of ones with one zero pixel and expected a non-zero
corner pixel to have a positive distance, which only held because the old
code mishandled values other than 0 and 255. A non-zero pixel is foreground,
so its distance is 0, and the zero pixel is one pixel from the foreground.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@petercorke
petercorke merged commit 5c51cf2 into main Nov 22, 2026
32 checks passed
@petercorke
petercorke deleted the bug/distance-transform-foreground branch November 22, 2026 23:46
@github-actions github-actions Bot mentioned this pull request Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant