From aab7e238e41f6872400f1d35ec14fc2dde0b18ce Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Mon, 10 Apr 2023 18:46:13 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=98=B7=20to=20=F0=9F=93=A6=20initial=20ve?= =?UTF-8?q?rsion=20of=20mask=5Fto=5Fxyxy=20util?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/detection/utils.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/supervision/detection/utils.py b/supervision/detection/utils.py index ec50200d..e10e7489 100644 --- a/supervision/detection/utils.py +++ b/supervision/detection/utils.py @@ -122,3 +122,18 @@ def xywh_to_xyxy(boxes_xywh: np.ndarray) -> np.ndarray: xyxy[:, 2] = boxes_xywh[:, 0] + boxes_xywh[:, 2] xyxy[:, 3] = boxes_xywh[:, 1] + boxes_xywh[:, 3] return xyxy + + +def mask_to_xyxy(masks: np.ndarray) -> np.ndarray: + n = masks.shape[0] + bboxes = np.zeros((n, 4), dtype=int) + + for i, mask in enumerate(masks): + rows, cols = np.where(mask) + + if len(rows) > 0 and len(cols) > 0: + x_min, x_max = np.min(cols), np.max(cols) + y_min, y_max = np.min(rows), np.max(rows) + bboxes[i, :] = [x_min, y_min, x_max, y_max] + + return bboxes