Skip to content

fix(cv2): map resize pixels with fx and fy when dsize is not given - #2690

Merged
Borda merged 3 commits into
roboflow:developfrom
raashish1601:fix/fallback-resize-fx-fy
Oct 8, 2026
Merged

Borda merged 3 commits into
roboflow:developfrom
raashish1601:fix/fallback-resize-fx-fy

Conversation

@raashish1601

@raashish1601 raashish1601 commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Description

When dsize is not given, OpenCV computes the output size from fx/fy and also maps destination pixels with 1 / fx and 1 / fy. The NumPy fallback for cv2.resize computed 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 exactly 1 / fx:

import numpy as np, cv2
from supervision._cv2._image import _resize

src = np.random.default_rng(20260717).integers(0, 256, (13, 10, 3), dtype=np.uint8)
_resize(src, None, fx=1 / 7, fy=1 / 7).tolist()   # [[[195, 119, 86]], [[50, 115, 57]]]
cv2.resize(src, None, fx=1 / 7, fy=1 / 7).tolist()  # [[[221, 37, 117]], [[31, 74, 38]]]

sv.PixelateAnnotator resizes with dsize=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 _cv2 fixes. No issue.

Changes Made

  • _resize uses 1 / fx and 1 / fy as the sampling scale when the size comes from the factors, and the size ratio otherwise (unchanged).
  • Nearest uses floor(dst * scale), as OpenCV does; the existing exact integer formula is kept for the dsize case.
  • Linear with factors goes through the existing NumPy path, which clamps at the border. The Pillow fast path is kept for dsize only, because Pillow's affine transform fills outside pixels with 0 when upscaling.

Testing

  • Added test_fallback_resize_with_scale_factors_matches_opencv in tests/cv2/test_image.py (nearest and linear, BGR and gray, several fx/fy including mixed and 1 / 7). 18 of its 20 cases fail on develop and 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 the dsize path.

  • uv run pytest tests/cv2 tests/utils/test_image.py tests/annotators: 772 passed. The one failure is a collection check that needs scikit-learn, which isn't installed here.

  • ruff check and ruff format --check on 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.md under Unreleased (skip for lint/type/format-only or pure doc changes)

Additional Notes

cv2.resize with both dsize and fx/fy still uses dsize, as OpenCV does.

@raashish1601
raashish1601 requested a review from SkalskiP as a code owner October 7, 2026 14:18
@Borda
Borda requested a balanced review from Copilot October 7, 2026 15:24
@Borda Borda added the bug Something isn't working label Oct 7, 2026
@codecov

codecov Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93%. Comparing base (4491c92) to head (49da90e).
⚠️ Report is 2 commits behind head on develop.

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread src/supervision/_cv2/_image.py Resolved
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
raashish1601 force-pushed the fix/fallback-resize-fx-fy branch from 1feab6b to 49da90e Compare October 7, 2026 23:26
@Borda Borda changed the title fix(cv2): map resize pixels with fx and fy when dsize is not given fix(cv2): map resize pixels with fx and fy when dsize is not given Oct 8, 2026
@Borda
Borda merged commit 3ab21e2 into roboflow:develop Oct 8, 2026
36 checks passed
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.
@Borda Borda mentioned this pull request 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants