Repository navigation
fix(cv2): center thick rectangle borders on the edge - #2675
Conversation
Pillow's `ImageDraw.rectangle(outline=1, width=...)` grows the border inward only, so the NumPy fallback drew every `thickness >= 2` rectangle thinner and smaller than OpenCV does. OpenCV strokes each edge as a band centered on it and rounds the four joints with a filled disc, which is exactly the integer disc. Rasterize the stroke directly instead: four edge bands plus a corner disc, clipped to the canvas. `thickness=1` and filled rectangles are unchanged. Part of roboflow#2674
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The changelog must link this PR instead of issue #2674.
Review effort: Balanced
Findings: 1
Open (1)
What changed in this PR
Fixes NumPy fallback rectangle strokes to match OpenCV’s centered borders.
Changes:
- Adds edge-band and corner-disc rasterization.
- Adds parity and clipping tests.
- Documents the fix in the changelog.
Assessment: Code quality 5/5 · Testing 5/5 · Documentation 4/5
| File | Description |
|---|---|
src/supervision/_cv2/_drawing.py |
Implements centered rectangle strokes. |
tests/cv2/test_drawing.py |
Tests OpenCV parity and clipping. |
docs/changelog.md |
Records the user-visible fix. |
💡 Add a code-review agent skill or 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 #2675 +/- ##
=======================================
Coverage 93% 93%
=======================================
Files 78 78
Lines 11930 11962 +32
=======================================
+ Hits 11049 11081 +32
Misses 881 881 🚀 New features to boost your workflow:
|
…e-thick-stroke # Conflicts: # docs/changelog.md
|
Addressed the review note in 5c79df2: the changelog entry links this PR instead of #2674, per That commit also merges |
…e-thick-stroke # Conflicts: # docs/changelog.md
…thick-stroke # Conflicts: # docs/changelog.md
Reword the module and _point docstrings to Pillow/NumPy and pixel coordinates. [resolve group] PR roboflow#2675 — items 11 --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Evaluate each corner disc only over its clipped window, coerce rectangle thickness with operator.index, and raise ValueError above 32767. Document the centered border in the _rectangle docstring. [resolve group] PR roboflow#2675 — items 5 6 13 --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Add a cv2-free golden test for the centered border, NumPy integer thickness, huge and invalid thickness, and 18 parametrized OpenCV parity cases. [resolve group] PR roboflow#2675 — items 7 8 --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Name the box, state the zero-height exception, and document the thickness limits. [resolve group] PR roboflow#2675 — items 10 --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Pillow's `ImageDraw.rectangle(outline=1, width=...)` grows the border inward only, so the NumPy fallback drew every `thickness >= 2` rectangle thinner and smaller than OpenCV does. OpenCV strokes each edge as a band centered on it and rounds the four joints with a filled disc, which is exactly the integer disc. Rasterize the stroke directly instead: four edge bands plus a corner disc, clipped to the canvas. `thickness=1` and filled rectangles are unchanged. --------- Co-authored-by: jirka <[email protected]> Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>

Description
The NumPy fallback drew a stroked rectangle entirely inside the rectangle, while OpenCV centers the stroke on the edges. Any box with
thickness >= 2came out thinner and 1 pixel smaller per side withoutopencv-pythoninstalled.BoxAnnotator's defaultthicknessis2and OpenCV is no longer a dependency, so this was the default path for a plainpip install supervision:opencvfallbackRepro, run once with
opencv-pythonand once without:Motivation and Context
Pillow's
ImageDraw.rectangle(outline=1, width=...)only grows inward. OpenCV strokes each edge as a band centered on it and rounds the four joints with a filled disc, so the border spreads outside the rectangle as well as inside.The existing
test_fallback_line_draws_and_matches_opencv_extentcompares only the raster bounding box withatol=1, and the error atthickness=2is exactly 1 pixel per side, so it passed.Part of #2674
Changes Made
_rectanglenow rasterizes the stroke directly as four edge bands plus a corner disc, clipped to the canvas, instead of delegating to Pillow'soutline=/width=._rectangle_maskand_dischelpers insrc/supervision/_cv2/_drawing.py. The disc is built once at its own size and OR-ed into each corner window, so cost scales with the stroke, not the canvas.thickness=1and filled (thickness=-1) rectangles are unchanged, as is the public API._line,_circle,_polylinesand_ellipseshare the inward-stroke cause and are untouched here. See the two scoping questions in [Bug]: NumPy fallback draws thick rectangle borders inward instead of centering them on the edge #2674.Testing
Band radius is
0atthickness=1, else(thickness + 1) // 2, derived from OpenCV and checked against it. The patched_rectanglematchescv2.rectanglebyte for byte on 1310 randomized and hand-picked cases (degenerate, reversed, negative and out-of-bounds corners, float corners, thickness 1 to 25), on bothopencv-python4.10.0 and 5.0.0.New tests fail on
developand pass with this change.thickness=1passes either way:Full suite, both CI legs, on Windows 11 / Python 3.12:
docs/changelog.mdunderUnreleasedAdditional Notes
Not tested: macOS and Linux, and
opencv-python-headless. The change is pure NumPy and Pillow with no platform-specific code, and the OpenCV reference was checked on two major versions.I could not find a thickness at which OpenCV's own corner geometry is reproducible by simply expanding Pillow's bounds, which is why the corner disc is explicit. OpenCV's filled circle turns out to be exactly the integer disc
x^2 + y^2 <= r^2, verified for radius 0 to 14, so_discis exact rather than an approximation. That same fact would make_circlecheap to fix if you want it.