From f0d3b167fb8e07d14f8d655e37010d9f9e3fec0e Mon Sep 17 00:00:00 2001 From: Laughing <61612323+Laughing-q@users.noreply.github.com> Date: Wed, 18 Dec 2024 21:02:55 +0800 Subject: [PATCH] Revert `segment2box` and clip segments (#18294) --- ultralytics/utils/ops.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ultralytics/utils/ops.py b/ultralytics/utils/ops.py index 40de949c..b2c453a2 100644 --- a/ultralytics/utils/ops.py +++ b/ultralytics/utils/ops.py @@ -75,8 +75,13 @@ def segment2box(segment, width=640, height=640): (np.ndarray): the minimum and maximum x and y values of the segment. """ x, y = segment.T # segment xy - x = x.clip(0, width) - y = y.clip(0, height) + # any 3 out of 4 sides are outside the image, clip coordinates first, https://github.com/ultralytics/ultralytics/pull/18294 + if np.array([x.min() < 0, y.min() < 0, x.max() > width, y.max() > height]).sum() >= 3: + x = x.clip(0, width) + y = y.clip(0, height) + inside = (x >= 0) & (y >= 0) & (x <= width) & (y <= height) + x = x[inside] + y = y[inside] return ( np.array([x.min(), y.min(), x.max(), y.max()], dtype=segment.dtype) if any(x)