supervision icon indicating copy to clipboard operation
supervision copied to clipboard

Polygon Zone Superposition - Clipping bug

Open zaknye opened this issue 3 months ago • 2 comments

Search before asking

  • [x] I have searched the Supervision issues and found no similar bug report.

Bug

I've identified a bug in the PolygonZone class that allows a single object to occupy two ROIs even if they have no overlap. It seems the bug was introduced here: https://github.com/roboflow/supervision/issues/1101#issuecomment-2051863063

By moving the clipping calculation from using the frame height/width to the ROI height/width you're creating two separate bounding boxes for each separate ROI. This clips the boxes to exist within the bounds of the boolean numpy array (good) but then calculates the anchor point AFTER modifying the box (bad). This allows a single object to have two different anchor points for different ROIs causing the object to exists within two zones at once.

I propose that instead of clipping the incoming BBox to within the ROI then calculating anchor points do the following:

  1. Calculate the center of the box
  2. Check if the center point exceeds the min/max of the shape of the self.mask array. If so return False as the BBox can not possible occupy the ROI
  3. Finally check the point against the self.mask array and return the result.

Environment

  • Supervision: 0.26.1
  • Python 3.12.3

Minimal Reproducible Example

rois: roi1 = np.array([[0,167],[422,79],[457,427],[102,547]], np.int32) roi2 = np.array([[458,67],[1011,75],[997,309],[515,609]], np.int32) roi3 = np.array([[0,683],[457,474],[474,639],[219,720]], np.int32)

detection: Detections(xyxy=array([[441.50900269, 258.48202515, 718.4284668 , 712.74450684]]), mask=None, confidence=array([0.89746094]), class_id=array([0]), tracker_id=array([3]), data={'class_name': array(['person'], dtype='<U6')}, metadata={})

Additional

No response

Are you willing to submit a PR?

  • [x] Yes I'd like to help by submitting a PR!

zaknye avatar Oct 09 '25 20:10 zaknye

I've been looking into this issue and I think I have a potential fix. Would it be okay if I worked on this and submitted a PR?

Adithi-Sreenath avatar Oct 14 '25 05:10 Adithi-Sreenath

Hmm seems like you beat me to it. I've created my own PR here: https://github.com/roboflow/supervision/pull/1992

zaknye avatar Oct 14 '25 15:10 zaknye