[Bug]: NumPy fallback draws thick rectangle borders inward instead of centering them on the edge
Bug
What's the bug?
The NumPy fallback draws a stroked rectangle entirely inside the rectangle, while OpenCV centers the stroke on the rectangle's edges. Any box drawn with thickness >= 2 therefore comes out thinner and smaller when opencv-python is not installed.
Since OpenCV is no longer a dependency, the fallback is what a plain pip install supervision uses, and BoxAnnotator's default thickness is 2, so this is the default path. CropAnnotator (default border_thickness=2), PercentageBarAnnotator's border and sv.draw_rectangle (default thickness=2) reach the same call. The filled calls in ColorAnnotator and LabelAnnotator are unaffected.
Expected behavior: the fallback matches OpenCV, per the intent stated in #2433 and #2439.
Actual behavior: the border is drawn inward only. thickness=1 and filled (thickness=-1) rectangles are unaffected.
| backend | painted px | painted bbox |
|---|---|---|
opencv |
596 | x 19-81, y 19-61 |
fallback |
392 | x 20-80, y 20-60 |
For a (3,3) to (11,8) rectangle at thickness=2, OpenCV paints rows 2 to 9 and columns 2 to 12, its left band covering columns 2 to 4. The fallback paints rows 3 to 8 and columns 3 to 11, its left band covering only columns 3 and 4. The stroke is one pixel narrower and sits one pixel inside OpenCV's on every side.
Cause: _rectangle in src/supervision/_cv2/_drawing.py uses Pillow's ImageDraw.rectangle(outline=1, width=thickness), and Pillow's 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.
This was not caught because test_fallback_line_draws_and_matches_opencv_extent compares only the raster bounding box with atol=1, and the error at thickness=2 is exactly 1 pixel per side.
Environment
- Supervision: 0.31.0.dev0 (
develop, 09e0b53) - Python: 3.12
- OS: Windows 11
Reproduced identically against opencv-python 4.10.0 and 5.0.0.
Minimal Reproducible Example
Run once with opencv-python installed and once without.
import numpy as np
import supervision as sv
from supervision._cv2 import BACKEND_NAME
detections = sv.Detections(
xyxy=np.array([[20.0, 20.0, 80.0, 60.0]]),
class_id=np.array([0]),
confidence=np.array([0.9]),
)
scene = np.zeros((80, 100, 3), dtype=np.uint8)
painted = sv.BoxAnnotator().annotate(scene, detections).any(axis=2)
ys, xs = np.where(painted)
print(f"backend : {BACKEND_NAME}")
print(f"painted pixels : {int(painted.sum())}")
print(f"painted bbox : x {xs.min()}-{xs.max()}, y {ys.min()}-{ys.max()}")
Are you willing to submit a PR?
Yes. I have a fix for rectangle that matches OpenCV exactly on 1310 randomized and edge cases (degenerate, reversed, negative and out-of-bounds corners, thickness 1 to 25) on both OpenCV 4.10 and 5.0, with a test that fails on develop and passes with it.
Two scoping questions before I open it:
_line,_circle,_polylinesand_ellipsehave the same inward-stroke cause. Do you want one PR for all five, orrectanglefirst and the rest separately?_circleand_ellipsealso differ slightly when filled or atthickness=1, because Pillow's ellipse rasterizer is not OpenCV's. OpenCV's filled circle is exactly the integer discx^2 + y^2 <= r^2, so that is cheap to match. Worth doing in the same pass, or is a separate issue better?
Hi! I'm interested in contributing to this issue as my first contribution to Supervision. I saw that you already have a fix for _rectangle. Would it be helpful if I worked on one of the related primitives, such as _line, _circle, _polylines, or _ellipse, in a separate PR? I'd be happy to focus on a clearly scoped piece of work to avoid duplicating your changes.
Following up on the other primitives mentioned above. I compared the NumPy fallback against OpenCV on current develop (fdc0cd9) for _line, _circle and _polylines on a 100x90 canvas. At thickness 1 the line and polyline pixel counts match exactly. At thickness >= 2 the fallback paints noticeably fewer pixels:
| primitive | thickness | fallback px | opencv px |
|---|---|---|---|
| line | 2 | 172 | 297 |
| line | 5 | 433 | 639 |
| circle outline | 2 | 348 | 562 |
| circle outline | 5 | 848 | 1320 |
| closed polylines | 2 | 330 | 616 |
| closed polylines | 5 | 989 | 1500 |
This is the same class of difference as the rectangle fix in #2675. I'd like to take _line, _circle and _polylines in one PR, with tests that compare against OpenCV. @samarth-git27, if you have already started on any of these, tell me which and I will leave them to you and take the rest. Happy to adjust scope if the maintainers prefer one primitive per PR.
Update: I'm not going to take _line, _circle and _polylines after all. A thick line in OpenCV is a filled polygon with fixed-point edge rounding plus round caps, and I could not get a pixel-exact match quickly enough to be useful. @samarth-git27, these are free for you. The pixel counts in my earlier comment are still valid as a starting point, and an OpenCV pixel-equality test like the one added in #2675 is a good oracle.
I reproduced the remaining circle mismatch on current develop: the fallback paints 348 pixels versus OpenCV's 562 at thickness 2. Would maintainers welcome a separate circle-fallback PR with regression tests? @samarth-git27, have you started circle work? I can take it if it is still available.
Thanks for the update! I haven't completed the "_circle" implementation yet, so it's still available. I'd be interested in taking it on and working on the OpenCV pixel-parity implementation with regression tests.
I'll first review the current fallback implementation and the pixel-equality tests to make sure the fix is consistent with OpenCV's behavior and doesn't duplicate existing work. Please feel free to let me know if there are any specific expectations.
Hi everyone — following the scope left open in #2727, I'd like to take _ellipse.
Plan: port OpenCV's Ellipse2Poly / EllipseEx path and route it through the fixed-point rasterization that already landed for thick lines and polylines, then add pixel-equality tests against cv2.ellipse for thickness 1/2/large, filled, rotated, partial arcs, and off-canvas cases.
I'll leave _circle to @samarth-git27 since he expressed interest; the shared ellipse helper should be reusable there if it helps. I'll open a PR against develop shortly.
Thank you @px12811 ...will work on it shortly!