ITADN

Conversion from YOLO polygons to masks is inaccurate

#2023OpenStijnPruijssers 创建于 2025-12-05
bug
S
StijnPruijsserscommented
### Search before asking - [x] I have searched the Supervision [issues](https://github.com/roboflow/supervision/issues) and found no similar bug report. ### Bug When loading in YOLO annotations with polygons from file the polygons are not parsed correctly to a mask due to integer casting. When calling loading in YOLO polygon annotations to detections using `yolo.py` in `supervision.datasets.formats` the mask is not the same as the original mask used to create the polygon points. I tested this by converting a binary mask to polygons and then back to a mask. Theoretically this should be the same. **Original mask** <img width="626" height="510" alt="Image" src="https://github.com/user-attachments/assets/9668d65f-1b25-4da7-907f-a56012166770" /> **Reconstructed mask** <img width="626" height="510" alt="Image" src="https://github.com/user-attachments/assets/d9687767-c573-4949-be6f-b0d580bba210" /> **Difference image** Green = Pixels in original mask but not reconstructed mask, i.e. missing pixels Red = Pixels in reconstructed mask but not original mask, i.e. extra pixels <img width="937" height="763" alt="Image" src="https://github.com/user-attachments/assets/7f509020-7d9a-47b6-a811-3afe53ce51dd" /> The error arises because in `yolo.py` on line 119 in the `yolo_annotations_to_detections` method. The ``` polygons = [ (polygon * np.array(resolution_wh)).astype(int) for polygon in relative_polygon ] ``` This causes all float values in polygon to be floored due to the multiplication with an int typed array. Now all values with the first decimal > 5 are wrong. This causes a shift of the mask to the top left as in the example. A simple np.round can solve this. ### Environment - Supervision 0.27.0 - Python 3.12 - Windows 11 ### Minimal Reproducible Example _No response_ ### Additional _No response_ ### Are you willing to submit a PR? - [x] Yes I'd like to help by submitting a PR! When I get some time :)
0 条评论