[Bug]: A detection with an empty mask is anchored at (0, 0) for Position.CENTER_OF_MASS, giving phantom LineZone crossings and zone hits
Search before asking
- [x] I have searched the issues and discussions and found no similar bug report.
Bug
What's the bug?
Detections.get_anchors_coordinates(Position.CENTER_OF_MASS) returns (0, 0) for a detection whose mask has no pixels, because calculate_masks_centroids guards its division by zero and reports (0, 0). Every consumer of the CENTER_OF_MASS anchor then acts on the frame's top-left corner, although the detection still has a valid xyxy. A segmentation detection can easily end up with an empty mask (a small object lost to thresholding or resizing; see #2631 and #2741 for other empty-mask handling).
Expected behavior: A stationary object whose mask is empty in one frame should not produce a crossing or leave its zone. Anchoring such a detection at the center of its box would give the same location as the neighbouring frames.
Actual behavior:
anchor, non-empty mask: [[100, 90]]
anchor, EMPTY mask : [[0, 0]] (box centre is [[100.0, 90.0]] )
LineZone counts for an object that never moves: in=1 out=1 (expected 0/0)
PolygonZone in-zone (object at (100,90), zone top-left): non-empty mask -> [False] | empty mask -> [True] (expected False)
DotAnnotator pixels drawn around: (1, 1) (expected around (100, 90))
LabelAnnotator and TraceAnnotator also read the anchor through get_anchors_coordinates, so they draw at the corner too.
Environment
- Supervision: 0.32.0.dev (
developat 5210466) - Python: 3.12.14
- OS: macOS (Apple Silicon)
Minimal Reproducible Example
import numpy as np
import supervision as sv
H, W = 120, 160
def make(empty):
mask = np.zeros((1, H, W), dtype=bool)
if not empty:
mask[0, 80:100, 90:110] = True
return sv.Detections(
xyxy=np.array([[90.0, 80.0, 110.0, 100.0]]),
mask=mask,
class_id=np.array([0]),
tracker_id=np.array([7]),
)
line = sv.LineZone(
start=sv.Point(0, 50),
end=sv.Point(W, 50),
triggering_anchors=[sv.Position.CENTER_OF_MASS],
)
for empty in [False, False, True, False, False]: # the object never moves
line.trigger(make(empty))
print(line.in_count, line.out_count) # 1 1, expected 0 0
print(make(True).get_anchors_coordinates(sv.Position.CENTER_OF_MASS)) # [[0 0]]
Are you willing to submit a PR?
- [x] Yes I'd like to help by submitting a PR!