Repository navigation
fix(cv2): match OpenCV's nearest-neighbour sampling grid in the resize fallback - #2722
Merged
Borda merged 5 commits intoOct 15, 2026
Merged
Conversation
Contributor
Author
|
recheck |
Changes: - Merge develop at e2c958f and preserve both sides of the changelog insertion conflict. - Retain the PR's reciprocal nearest-neighbor sampling and regression tests alongside upstream docstring corrections. - Incorporate upstream label separation, oriented-box precision, configurable mAP limits, integer-area overflow, keypoint indexing, CompactMask copy handling, and CSV array serialization fixes. - Incorporate upstream keypoint parity tests and dependency, API documentation links and examples, review guidance, and documentation publishing improvements. Impact: - Bring the PR branch onto the fetched target implementation without discarding either changelog history or the resize fix. - Preserve the upstream behavior and tooling updates in the local integration. Verification: - Focused CompactMask and image tests: 318 passed, 2 skipped, 1 warning. - Configured Markdown formatter and git diff --cached --check passed. - Entire staged tree checked against fetched target; the three PR files match their independently constructed expected contents, with no unresolved index entries. Residual limits: - OpenCV is absent; native parity tests remain unverified. - Review findings await scope selection; independent review and the canonical full gates remain pending. --- Co-authored-by: Codex <[email protected]>
Changes: - Limit the PR roboflow#2722 changelog claim to some whole-number positions where reciprocal multiplication rounds below the integer. - Preserve the 6-to-34 example and its CompactMask impact. Impact: - Explain the backend mismatch without implying every integer boundary differs. Verification: - Full configured pre-commit and targeted mdformat passed. - Full suite: 5566 passed, 79 skipped, 39 warnings; 94% coverage. - Native OpenCV resize checks: 35 passed; four before/after oracle cases verified the correction. - Four independent role assessments and the snapshot evidence gate passed. Residual limits: - Native parity exercised OpenCV 5.0.0.93 on macOS ARM64; other versions and platforms remain unverified. - Full-suite skips and warnings remain recorded; historical review provenance is unchanged. --- Co-authored-by: Codex <[email protected]>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The localized arithmetic correction addresses the reported mismatch with focused regression coverage and no identified blocking issues.
0 open findings
What changed in this PR
Aligns the NumPy resize fallback with OpenCV’s nearest-neighbour sampling, keeping resized masks consistent across backends.
Changes:
- Uses reciprocal scale arithmetic for explicit destination sizes.
- Adds four ramp regression cases and a two-axis color-image test.
- Documents the fix in the changelog.
Scores: Code quality 5/5 · Testing 5/5 · Documentation 5/5
| File | Description |
|---|---|
| tests/cv2/test_image.py | Adds exact OpenCV parity regression tests. |
| src/supervision/_cv2/_image.py | Corrects nearest-neighbour sampling arithmetic. |
| docs/changelog.md | Records the fix and its effect on compact masks. |
🧠 Review effort: Balanced
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #2722 +/- ##
=======================================
- Coverage 94% 94% -0%
=======================================
Files 83 83
Lines 13129 13127 -2
=======================================
- Hits 12322 12320 -2
Misses 807 807 🚀 New features to boost your workflow:
|
Borda
approved these changes
Oct 15, 2026
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
The NumPy
resizefallback picked a different source pixel than OpenCV forINTER_NEARESTat some sizes. The fallback used the exact integer ratiox * src_w // dst_w; OpenCV steps through the source with the reciprocal of the size ratio,ifx = 1. / (dst_w / src_w), and takescvFloor(x * ifx)(resize.cpp,resizeNN). Wherex * src_w / dst_wis a whole number, that reciprocal can land just below it and OpenCV picks the previous pixel.Smallest case: a 6-pixel ramp resized to 34 wide. At
x = 17,17 * 6 / 34 = 3.0, but17 * (1 / (34 / 6)) = 2.9999999999999996, so OpenCV samples pixel 2 and the fallback sampled pixel 3.62 (source width ≤ 40, destination width ≤ 80) pairs were affected; on random color images at random sizes in that range, 24 of 600 resizes differed.
Motivation and Context
CompactMask.resize(#2697, #2705) derives its sampling grid fromcv2.resize(ramp, …, INTER_NEAREST)through the facade and documents that the map "equalsINTER_NEARESTexactly, including its floating-point rounding", so resized masks differed between environments with and without OpenCV. Thefx/fypath already used OpenCV'sfloor(x / fx)since #2690; this makes thedsizepath consistent with it.Closes #2721
Changes Made
_resize: compute the sampling step for thedsizepath as1 / (dst / src)like OpenCV, and use the samefloor(x * step)indexing for both nearest paths. The linear path is unaffected in practice (the uint8 branch goes through Pillow; the generic branch changes only in the last floating-point bit).Unreleased.Testing
New tests on
develop(withopencv-python):With this change:
A randomized sweep of 600 nearest resizes against OpenCV 4.12 (random 1–40 px sources, 1–80 px destinations) goes from 24 mismatches to 0.
docs/changelog.mdunderUnreleasedAdditional Notes
The equivalent rounding exists for
INTER_LINEARin OpenCV (scale_x = 1. / inv_scale_x), but the fallback's uint8 linear path is Pillow-based and already tested atatol=1, so this PR leaves it alone.