Repository navigation
fix: treat any non-zero pixel as foreground in distance_transform - #242
Merged
Merged
Conversation
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>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
| Duplication | 0 |
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.
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Image.distance_transform()inverted the image as255 - xbefore 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(orx != 0forinvert=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.0note.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.pyandtests/test_image_spatial.pypass 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