Repository navigation
fix(cv2): draw thick lines and polylines like OpenCV without OpenCV - #2727
Merged
Merged
Conversation
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #2727 +/- ##
========================================
Coverage 94% 94%
========================================
Files 83 83
Lines 13100 13316 +216
========================================
+ Hits 12293 12506 +213
- Misses 807 810 +3 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Oversized thickness values can stall rasterization, and existing rectangle regression tests were removed.
2 open findings
What changed in this PR
🟠 Request Changes — Adds OpenCV-compatible thick-line rasterization to the NumPy fallback.
Changes:
- Implements fixed-point thick lines, caps, clipping, and polygon filling.
- Applies rasterization to lines, polylines, and contour outlines.
- Adds comparison tests and a changelog entry.
Quality: Code 4/5 · Tests 3/5 · Documentation 5/5 · Risk 3/5
| File | Description |
|---|---|
src/supervision/_cv2/_drawing.py |
Implements thick-line rasterization. |
tests/cv2/test_drawing.py |
Adds pixel-equality tests but removes rectangle regressions. |
docs/changelog.md |
Documents corrected fallback behavior. |
🧠 Review effort: Balanced
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Signed-off-by: kevin9327 <[email protected]>
Signed-off-by: kevin9327 <[email protected]>
Reject a line, polyline or contour thickness above OpenCV's MAX_THICKNESS (32767) before rasterizing, sharing one validator with the rectangle path, so an oversized value can no longer stall _fill_convex_fixed_point. Restore the rectangle regression tests (OpenCV raster matrix, centered border, NumPy-integer thickness, huge thickness) and add rejection tests for the stroke paths.
kevin9327
force-pushed
the
fix/thick-line-fallback
branch
from
October 15, 2026 09:18
36aa4be to
3d17af3
Compare
Changes: - Add four regression cases for absent or empty thick polylines and zero-sized clipping bounds. - Qualify the changelog pixel-parity claim to LINE_8 and document the existing LINE_AA limitation. Impact: - Protect the three previously uncovered drawing guard returns. - Describe which fallback drawing calls match OpenCV without promising antialiasing support. Verification: - Full suite: 5582 passed, 97 skipped. - OpenCV 5.0.0.93 drawing oracle: 69 passed, zero skipped. - Repository-wide pre-commit and all five canonical gates passed. - Independent source review completed 6500 reference comparisons. Residual limits: - Oracle evidence covers one OpenCV version and macOS arm64; finite probes are not exhaustive. - Parent verified the documentation correction and added tests after the independent source review. --- Co-authored-by: Codex <[email protected]>
Borda
approved these changes
Oct 15, 2026
This was referenced Oct 16, 2026
2 of 3 tasks
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
Without
opencv-python,_cv2.line,_cv2.polylinesand the outline form of_cv2.drawContours(thickness >= 0) atthickness >= 2paint different pixels from OpenCV. They used Pillow'sImageDraw.line(width=...), which has square ends and its own edge rounding. OpenCV draws each segment as a fixed-point quadrilateral and puts a filled disc on its ends. This is the part of #2674 that #2675 left open, after the rectangle fix.On 2,000 random lines on a 200×200 canvas with
thickness2–12, 2000/2000 differed fromcv2.lineondevelop. The median gap was 24.2% of OpenCV's painted pixels and the largest 66.4%. With this PR: 0/2000.Consumers on this path include
OrientedBoxAnnotator,BoxCornerAnnotator,TraceAnnotator,TriangleAnnotator's outline,RoundBoxAnnotator,LineZoneAnnotator, the keypointEdgeAnnotator,sv.draw_lineandsv.draw_polygon(and soPolygonZoneAnnotator), whenever theirthicknessis 2 or more.Motivation and Context
Since OpenCV is optional, the fallback is what a plain
pip install supervisiondraws with. #2433 and #2439 say the fallback should match OpenCV. A comment on #2674 freed_lineand_polylines, and I said there that I'm taking them.Refs #2674
Changes Made
src/supervision/_cv2/_drawing.py. All of this is private, and the public API does not change._thick_line_mask: a port of OpenCV'sThickLinefor 8-connected lines. It does the following:thickness, as OpenCV does.XY_SHIFT=16fixed-point quadrilateral withcvRound(round half to even) offsets.(thickness + 1) // 2on the requested ends. It reuses_paint_discfrom fix(cv2): center thick rectangle borders on the edge #2675._fill_convex_fixed_point: a port ofFillConvexPoly. It uses OpenCV's two walking edges, the rounded per-row slope, and C truncating division._fixed_point_line_pixels: a port ofLine2, the fixed-point outline, withclipLineon the scaled canvas. It is vectorized per edge._clip_line: a port ofclipLine, using the same double-precision product truncated toward zero._polylinesand_draw_contourswiththickness > 1follow OpenCV'sPolyLineanddrawContourscap rules. Each segment gets an end cap, and an open polyline also gets a start cap. An open one-point polyline draws nothing, as in OpenCV._paint_aroundpaints only the window around the points. Thin shapes on large frames then do not pay for a full-frame boolean index.thickness <= 1keeps the existing Pillow path, which already matched OpenCV._circleand_ellipsewiththickness >= 2, andlineType=LINE_AA, which the fallback ignores as before.tests/cv2/test_drawing.pytest_fallback_line_draws_and_matches_opencv_extentnow asserts pixel equality instead of a bounding box withatol=1. Thatatol=1is the reason this was not caught before.TestFallbackThickLineclass with these tests:polylines: open and closed at thickness 2, 3 and 7, plus one-point and repeated-point inputs.drawContoursoutline.Testing
develop:20 failed, 11 passed. With this PR:31 passed.opencv-python-headless5.0.0:5571 passed, 1 skipped. That includestest_ordinary_suite_passes_when_cv2_is_blocked.pre-commit runon the changed files: clean (ruff, ruff-format, docformatter, mypy).cv2.line/cv2.polylines/cv2.drawContourswith 20,000 random cases each. Canvases were 5–120 px, points from -40 to 160 (so many are partly or fully off-canvas),thickness2–24, and 1–7 points, open or closed. Result: 0 differing pixels for all three.Timing (1920×1080×3 image, best of 3×20, ms per call):
developlinecorner to corner, t=2linecorner to corner, t=8polylines4-point zone, t=4polylines200-point contour, t=2docs/changelog.mdunderUnreleasedAdditional Notes