Repository navigation
fix(cv2): map resize pixels with fx and fy when dsize is not given - #2690
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #2690 +/- ##
=======================================
Coverage 93% 93%
=======================================
Files 78 78
Lines 11964 11974 +10
=======================================
+ Hits 11085 11095 +10
Misses 879 879 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Factor-based resizing still changes pixels when rounded output dimensions match the source, unlike OpenCV’s unchanged copy.
1 open finding
What changed in this PR
Aligns the NumPy resize fallback with OpenCV’s factor-based sampling, improving PixelateAnnotator consistency without OpenCV.
Changes:
- Uses inverse scale factors when output dimensions derive from
fx/fy. - Keeps Pillow acceleration for explicit-size resizing.
- Adds backend-comparison tests and a changelog entry.
Quality: 3/5 · Testing: 4/5 · Documentation: 5/5
| File | Description |
|---|---|
| tests/cv2/test_image.py | Adds factor-based resize comparisons. |
| src/supervision/_cv2/_image.py | Corrects sampling coordinates and fast-path selection. |
| docs/changelog.md | Documents the fix and pixelation impact. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
OpenCV maps destination pixels with 1 / fx and 1 / fy when the output size is computed from the scale factors. The NumPy fallback used the ratio of the rounded sizes instead, so PixelateAnnotator, which resizes with fx=1 / pixel_size, sampled different source pixels than OpenCV. Use the scale factors for nearest and linear sampling in that case.
raashish1601
force-pushed
the
fix/fallback-resize-fx-fy
branch
from
October 7, 2026 23:26
1feab6b to
49da90e
Compare
dsize is not given
Borda
approved these changes
Oct 8, 2026
Borda
pushed a commit
that referenced
this pull request
Oct 8, 2026
…2690) OpenCV maps destination pixels with 1 / fx and 1 / fy when the output size is computed from the scale factors. The NumPy fallback used the ratio of the rounded sizes instead, so PixelateAnnotator, which resizes with fx=1 / pixel_size, sampled different source pixels than OpenCV. Use the scale factors for nearest and linear sampling in that case.
Merged
Borda
pushed a commit
that referenced
this pull request
Oct 8, 2026
…2690) OpenCV maps destination pixels with 1 / fx and 1 / fy when the output size is computed from the scale factors. The NumPy fallback used the ratio of the rounded sizes instead, so PixelateAnnotator, which resizes with fx=1 / pixel_size, sampled different source pixels than OpenCV. Use the scale factors for nearest and linear sampling in that case.
This was referenced Oct 26, 2026
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.

Description
When
dsizeis not given, OpenCV computes the output size fromfx/fyand also maps destination pixels with1 / fxand1 / fy. The NumPy fallback forcv2.resizecomputed the same output size but mapped pixels with the ratio of the rounded sizes (source_width / width), so it samples different source pixels whenever that ratio isn't exactly1 / fx:sv.PixelateAnnotatorresizes withdsize=None, fx=1 / pixel_size, so without OpenCV installed its output differs from the OpenCV backend.Motivation and Context
Keeps the fallback in line with OpenCV, like the other recent
_cv2fixes. No issue.Changes Made
_resizeuses1 / fxand1 / fyas the sampling scale when the size comes from the factors, and the size ratio otherwise (unchanged).floor(dst * scale), as OpenCV does; the existing exact integer formula is kept for thedsizecase.dsizeonly, because Pillow's affine transform fills outside pixels with 0 when upscaling.Testing
Added
test_fallback_resize_with_scale_factors_matches_opencvintests/cv2/test_image.py(nearest and linear, BGR and gray, severalfx/fyincluding mixed and1 / 7). 18 of its 20 cases fail ondevelopand all pass with this change. A wider local comparison against OpenCV over more shapes, dtypes and factors gives the same 1-LSB linear budget as thedsizepath.uv run pytest tests/cv2 tests/utils/test_image.py tests/annotators: 772 passed. The one failure is a collection check that needsscikit-learn, which isn't installed here.ruff checkandruff format --checkon the changed files.Added/updated tests (I ran the test files listed above, not the full suite)
Updated docs (docstrings / mkdocs entry) for new or changed public API
Added a changelog entry in
docs/changelog.mdunderUnreleased(skip for lint/type/format-only or pure doc changes)Additional Notes
cv2.resizewith bothdsizeandfx/fystill usesdsize, as OpenCV does.