Skip to content

perf(cv2): simplify fallback operations - #2441

Merged
Borda merged 5 commits into
developfrom
cv2/simple
Jul 17, 2026
Merged

perf(cv2): simplify fallback operations#2441
Borda merged 5 commits into
developfrom
cv2/simple

Conversation

@Borda

@Borda Borda commented Jul 17, 2026

Copy link
Copy Markdown
Member

This pull request focuses on removing unused cv2 compatibility symbols and streamlining the cv2-free fallback implementation, while also improving performance and precision in key image processing operations. The main changes include removing unused API surface from the _cv2 module, adding a minimal find_contours helper, vectorizing connected-component statistics, and updating color conversion to exactly match OpenCV for uint8 images.

API surface reduction and fallback simplification:

  • Removed unused raw compatibility symbols from src/supervision/_cv2/__init__.py, including distanceTransform, getRotationMatrix2D, and warpAffine, as well as related constants and their exports. These are no longer needed by production consumers, which now use more targeted operations. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]

  • Added a new find_contours function to src/supervision/_cv2/__init__.py that provides only the mask-to-polygon conversion geometry required by production, further narrowing the fallback API. [1] [2]

Performance and precision improvements:

  • Updated the cv2-free fallback for connected-component statistics in src/supervision/_cv2/_components.py to use vectorized NumPy operations and scipy.ndimage.find_objects, significantly reducing overhead and improving performance.

  • Changed the BGR-to-gray conversion in src/supervision/_cv2/_color.py to use OpenCV's fixed-point uint8 arithmetic, ensuring exact matches for all possible input colors.

Changelog and documentation:

  • Updated the changelog to reflect the removal of unused compatibility symbols, the new precise BGR-to-gray conversion, and the vectorized connected-component statistics. [1] [2] [3]

Minor refactoring:

  • Cleaned up imports and type usage in src/supervision/_cv2/_contours.py.

- Remove unused compatibility operations and use focused Pillow and NumPy paths to reduce maintained fallback code.
- Preserve numerical decisions and hot-path performance with exact regression coverage and bounded algorithms.

---

Co-authored-by: Codex <codex@openai.com>
@codecov

codecov Bot commented Jul 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.30516% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 87%. Comparing base (a4b9c4e) to head (0ef6fb1).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2441   +/-   ##
=======================================
  Coverage       86%     87%           
=======================================
  Files           84      84           
  Lines        11786   11785    -1     
=======================================
+ Hits         10170   10196   +26     
+ Misses        1616    1589   -27     
🚀 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.

Pull request overview

This PR streamlines Supervision’s private _cv2 compatibility layer by removing unused fallback surface area and tightening the cv2-free implementations, while improving fidelity/performance for several core image-processing primitives (contours, connected components, color conversion, resize/text rendering). It also updates library consumers and tests to validate the new, narrower contracts.

Changes:

  • Reduced _cv2 API surface (removed unused transform/distance APIs) and introduced a minimal find_contours() geometry helper used by mask-to-polygon conversion.
  • Improved numeric fidelity/performance in cv2-free paths: fixed-point BGR2GRAY for uint8, vectorized connected-component stats, refined approxPolyDP, and optimized Pillow-based text/resize behavior.
  • Updated docs/changelog and expanded regression tests around contours, resize, text clipping, and edge-distance behavior.

Reviewed changes

Copilot reviewed 24 out of 24 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/detection/test_line_counter.py Adds regression test ensuring rotated line-zone labels use the Pillow rotation path and preserve alpha behavior.
tests/cv2/test_transform.py Removes tests for fallback APIs that were deleted from the _cv2 surface (affine + distance transform), keeping blur coverage.
tests/cv2/test_text.py Adds coverage for clipping text when the baseline origin is partially off-canvas.
tests/cv2/test_image.py Adds tighter resize regression tests for uint8 linear interpolation and float interpolation threshold behavior.
tests/cv2/test_geometry.py Adds randomized regression tests for approxPolyDP and float32 drift bounds for polygon intersection.
tests/cv2/test_cv2.py Updates expected _cv2 facade symbol set (replaces findContours with find_contours, removes unused symbols).
tests/cv2/test_contours.py Updates contour expectations to geometry-only contract, adds facade test for find_contours, and validates chamfer distances/edge-distance behavior.
tests/cv2/test_constants.py Updates constants expectations to reflect removed compatibility exports.
tests/cv2/test_components.py Adds high-component-image regression test for vectorized connected-component stats matching OpenCV.
tests/cv2/test_color.py Adds regression test for uint8 fixed-point BGR2GRAY rounding matching OpenCV.
src/supervision/detection/utils/masks.py Introduces fixed-point 3×3 L2 chamfer transform and updates edge-distance filtering to avoid distanceTransform.
src/supervision/detection/utils/converters.py Switches mask→polygon extraction to use the new cv2.find_contours() helper.
src/supervision/detection/line_zone.py Replaces cv2 affine rotation with Pillow-based rotation for label images.
src/supervision/_cv2/constants.py Removes unused _DIST_L2 constant from the private constants set.
src/supervision/_cv2/_video.py Uses PyAV’s stream-template API to replace the removed local stream-copy helper.
src/supervision/_cv2/_transform.py Removes unused affine and distance-transform fallbacks, leaving _blur.
src/supervision/_cv2/_text.py Reworks Pillow rasterization to build a glyph-sized mask and clip safely at image boundaries.
src/supervision/_cv2/_image.py Delegates uint8 bilinear resize to Pillow and keeps numeric half-pixel sampling for non-uint8 paths.
src/supervision/_cv2/_geometry.py Replaces approxPolyDP simplification with an OpenCV-matching stack traversal + cleanup step.
src/supervision/_cv2/_contours.py Simplifies contour extraction to return geometry only (no hierarchy construction).
src/supervision/_cv2/_components.py Vectorizes centroid/statistics computation and uses ndimage.find_objects for component bounds.
src/supervision/_cv2/_color.py Implements exact OpenCV-style fixed-point BGR2GRAY for uint8 images.
src/supervision/_cv2/init.py Removes unused cv2-compat symbols and adds find_contours() as the minimal geometry facade function.
docs/changelog.md Documents the fallback API reduction and the new precision/performance behaviors.

Comment thread src/supervision/_cv2/_components.py Outdated
Comment thread tests/cv2/test_image.py Outdated
Borda and others added 4 commits July 17, 2026 12:48
- Preserve INTER_LINEAR uint8 reductions within one LSB while retaining
  the resize performance budget and numeric RGBA handling.
- Restore repeated-endpoint contour anchors and bound cross-platform
  chamfer coefficient drift in regression tests.

---

Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@Borda
Borda merged commit f7b63f1 into develop Jul 17, 2026
26 checks passed
@Borda
Borda deleted the cv2/simple branch July 17, 2026 15:32
@Borda Borda mentioned this pull request Jul 29, 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.

2 participants