[Bug]: NumPy `fillPoly` fallback leaves out boundary pixels that OpenCV paints, so `polygon_to_mask` differs by backend
Bug
Without opencv-python, supervision._cv2.fillPoly paints fewer pixels than OpenCV along polygon edges. The fallback (_drawing._fill_poly) delegates to Pillow's ImageDraw.polygon, whose fill rule excludes some edge pixels that OpenCV's scan-line fill (FillEdgeCollection, fixed-point edge rounding) includes. The fallback never paints a pixel OpenCV leaves empty; it only misses pixels OpenCV paints.
Smallest case I found, a triangle [[10, 9], [9, 9], [7, 11]] on a 12×12 canvas (# both backends, O OpenCV only):
.........##.
........#O..
.......#....
OpenCV paints 5 pixels, the fallback 4.
On 300 random simple polygons shaped like typical zones (3–11 vertices, radius 30–90 px on a 200×200 canvas), 293 differ. The median gap is 0.7% of OpenCV's painted area, the mean 1.3%, and thin shapes reach 40%. For seed 1000 of that sweep, OpenCV paints 4180 pixels and the fallback 4162 (18 missing, all on the boundary).
sv.polygon_to_mask is fillPoly on a zero mask, and PolygonZone.trigger tests detection anchors against that mask, so whether a detection whose anchor sits on the zone edge counts as inside depends on whether OpenCV happens to be installed. The same applies to sv.draw_polygon/draw_filled_polygon, PolygonAnnotator, and the OBB fill in OrientedBoxAnnotator.
Expected: the fallback paints the same pixels as cv2.fillPoly, as the fallback rectangle does since #2675.
Environment
- supervision
develop@ fdc0cd97f (0.31.0.dev0) - macOS 15 arm64, Python 3.12.13, NumPy 2.2.6, Pillow 12.x
- Reference: opencv-python 4.12.0
Minimal Reproducible Example
import numpy as np
import cv2
from supervision._cv2._drawing import _fill_poly
triangle = [np.array([[10, 9], [9, 9], [7, 11]], dtype=np.int32)]
expected = np.zeros((12, 12), dtype=np.uint8)
actual = np.zeros((12, 12), dtype=np.uint8)
cv2.fillPoly(expected, triangle, 1)
_fill_poly(actual, triangle, 1)
print(expected.sum(), actual.sum()) # 5 4
print(np.argwhere(expected != actual)) # [[10 9]]
Are you willing to submit a PR?
- [x] Yes I'd like to help by submitting a PR!
Before I start: this needs a port of OpenCV's polygon scan conversion (FillEdgeCollection with its XY_SHIFT fixed-point rounding) instead of Pillow, which also sets up drawContours(thickness=-1) to match. Is that the direction you want, or would you rather keep Pillow and document the difference?