Skip to content

feat(cv2): add geometry fallbacks - #2432

Merged
Borda merged 5 commits into
developfrom
cv2/3
Jul 15, 2026
Merged

feat(cv2): add geometry fallbacks#2432
Borda merged 5 commits into
developfrom
cv2/3

Conversation

@Borda

@Borda Borda commented Jul 15, 2026

Copy link
Copy Markdown
Member

This pull request introduces several new fallback implementations for OpenCV-like image processing functions in the supervision._cv2 module. The main goal is to provide Python-based fallbacks for key connected components, contour, and polygon geometry operations when the native OpenCV backend is unavailable. The changes involve adding new modules, updating the main __init__.py to export these functions, and removing references to unavailable constants and methods.

The most important changes are:

New Fallback Implementations

  • Added _components.py with Python/scipy-based implementations for connectedComponents, connectedComponentsWithStats, and a utility _contains_holes for mask topology analysis. ([src/supervision/_cv2/_components.pyR1-R102](https://github.com/roboflow/supervision/pull/2432/files#diff-27518adcc08235320d81aa9ecdad8953f335345c8bd20615b93020b00224f61aR1-R102))
  • Added _contours.py with a fallback Suzuki-Abe contour tracing algorithm, including _find_contours for findContours and related helpers. ([src/supervision/_cv2/_contours.pyR1-R261](https://github.com/roboflow/supervision/pull/2432/files#diff-ddbb2f5922d838abeaa4293661ccd24c3eac3a99de45f8e7ac965e12ed04734aR1-R261))
  • Added _geometry.py providing polygon geometry methods: _approx_poly_dp (Douglas-Peucker simplification), _contour_area, _fill_poly, and _intersect_convex_convex. ([src/supervision/_cv2/_geometry.pyR1-R208](https://github.com/roboflow/supervision/pull/2432/files#diff-b5a2be7aec829d71675e502a4f6d79423f020c74f19dd4764dbcbc73d6547ba6R1-R208))

API and Constant Updates

  • Updated src/supervision/_cv2/__init__.py to export the new fallback implementations for approxPolyDP, connectedComponents, connectedComponentsWithStats, contourArea, fillPoly, findContours, and intersectConvexConvex, replacing previous _unavailable stubs. ([[1]](https://github.com/roboflow/supervision/pull/2432/files#diff-86e4f932e0a4f2028060b775cb747808c89741df6a612664a14ad5a523310876R7-R18), [[2]](https://github.com/roboflow/supervision/pull/2432/files#diff-86e4f932e0a4f2028060b775cb747808c89741df6a612664a14ad5a523310876L141-R176))
  • Removed the unused _RETR_CCOMP constant and all references to RETR_CCOMP in the API, consolidating support on RETR_TREE only. ([[1]](https://github.com/roboflow/supervision/pull/2432/files#diff-86e4f932e0a4f2028060b775cb747808c89741df6a612664a14ad5a523310876L45), [[2]](https://github.com/roboflow/supervision/pull/2432/files#diff-86e4f932e0a4f2028060b775cb747808c89741df6a612664a14ad5a523310876L79), [[3]](https://github.com/roboflow/supervision/pull/2432/files#diff-86e4f932e0a4f2028060b775cb747808c89741df6a612664a14ad5a523310876L202), [[4]](https://github.com/roboflow/supervision/pull/2432/files#diff-666cc43876a66ce9eb1a3308902462ecc588776d4aa07b185b60540c6a2534faL24))

Internal Usage

  • Updated masks.py to import and use the new _contains_holes utility from _components.py. ([src/supervision/detection/utils/masks.pyR7](https://github.com/roboflow/supervision/pull/2432/files#diff-76873461bd415c05ef936307b0c76ea0b62559253a2f7a02e16211f795be9642R7))

These changes significantly enhance the robustness and portability of the supervision._cv2 module by providing pure-Python/scipy fallbacks for essential image processing routines.

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

codecov Bot commented Jul 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.90909% with 34 lines in your changes missing coverage. Please review.
✅ Project coverage is 86%. Comparing base (3d669f1) to head (a412828).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop   #2432    +/-   ##
========================================
  Coverage       86%     86%            
========================================
  Files           78      81     +3     
  Lines        11020   11376   +356     
========================================
+ Hits          9507    9837   +330     
- Misses        1513    1539    +26     
🚀 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 expands supervision._cv2 with pure-Python (and SciPy-backed) fallbacks for several OpenCV geometry/connected-components operations, improving portability when native OpenCV is unavailable.

Changes:

  • Add fallback implementations for connected components, contour tracing, and polygon geometry in src/supervision/_cv2/.
  • Wire the new fallbacks into the _cv2 facade exports and remove the unsupported RETR_CCOMP constant usage.
  • Add tests comparing fallback behavior against OpenCV reference implementations and exercising key downstream consumers.

Assessment (n/5):

  • Code quality: 3/5
  • Testing: 3/5
  • Documentation: 4/5

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/cv2/test_geometry.py New tests validating fallback polygon geometry against OpenCV.
tests/cv2/test_contours.py New tests validating fallback contour tracing + consumer integration.
tests/cv2/test_constants.py Update facade-binding tests for the newly supported operations.
tests/cv2/test_components.py New tests validating connected-components fallbacks vs OpenCV.
src/supervision/detection/utils/masks.py Switch contains_holes to topology-based fallback + stabilize tie-breaking in segment filtering.
src/supervision/_cv2/constants.py Remove _RETR_CCOMP constant (now unsupported in fallback surface).
src/supervision/_cv2/_geometry.py New fallback implementations for area, polygon simplification, fill, and convex intersection.
src/supervision/_cv2/_contours.py New Suzuki–Abe-style contour fallback + hierarchy reconstruction.
src/supervision/_cv2/_components.py New SciPy-based connected-components + hole detection helper.
src/supervision/_cv2/init.py Export new fallbacks from the _cv2 compatibility facade.

Comment thread src/supervision/_cv2/_contours.py
Comment thread src/supervision/_cv2/_contours.py
Comment thread tests/cv2/test_geometry.py
Comment thread src/supervision/_cv2/__init__.py
Comment thread tests/cv2/test_contours.py
Borda and others added 4 commits July 15, 2026 23:31
[resolve group] PR #2432 — items 1 2

Item #1 (@Copilot): contour.mean(axis=0) can fall outside concave
contours, corrupting the parent/child hierarchy in _parents. Now
uses a scanline-midpoint interior point.
Item #2 (@Copilot): remapped_parents used ordered_indices.index()
in a comprehension (O(n^2)). Now precomputes an index_map for O(1)
lookups.
Challenge: both evidence=VALID suggestion=VALID resolution=as-suggested

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
…back

[resolve group] PR #2432 — items 3 5

Item #3 (@Copilot): np.sort(..., axis=0) sorted x/y columns
independently, letting mismatched point sets appear equal. Now
sorts points lexicographically (row-wise) via np.lexsort.
Item #5 (@Copilot): filter_segments_by_distance calls
connectedComponentsWithStats, which was not monkeypatched to the
fallback, so the test silently used native OpenCV. Added the
missing monkeypatch alongside the existing ones.
Challenge: both evidence=VALID suggestion=VALID resolution=as-suggested

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
[resolve group] PR #2432 — items 4

Item #4 (@Copilot): _contains_holes was imported into the fallback
facade but never used or re-exported there, which trips
unused-import lint checks. Removed from the import list.
Challenge: evidence=VALID suggestion=VALID resolution=codex-direct

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Co-authored-by: OpenAI Codex <codex@openai.com>
@Borda
Borda merged commit 7096ee9 into develop Jul 15, 2026
26 checks passed
@Borda
Borda deleted the cv2/3 branch July 15, 2026 22:55
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