diff --git a/docs/detection/utils.md b/docs/detection/utils.md
index abacdc21..02fb813e 100644
--- a/docs/detection/utils.md
+++ b/docs/detection/utils.md
@@ -65,6 +65,12 @@ status: new
:::supervision.detection.utils.move_boxes
+
+
+:::supervision.detection.utils.move_masks
+
diff --git a/supervision/__init__.py b/supervision/__init__.py
index bb526514..71fba5fe 100644
--- a/supervision/__init__.py
+++ b/supervision/__init__.py
@@ -52,6 +52,7 @@ from supervision.detection.utils import (
mask_to_polygons,
mask_to_xyxy,
move_boxes,
+ move_masks,
polygon_to_mask,
polygon_to_xyxy,
scale_boxes,
diff --git a/supervision/detection/tools/inference_slicer.py b/supervision/detection/tools/inference_slicer.py
index bd4a2425..a951664c 100644
--- a/supervision/detection/tools/inference_slicer.py
+++ b/supervision/detection/tools/inference_slicer.py
@@ -1,5 +1,5 @@
from concurrent.futures import ThreadPoolExecutor, as_completed
-from typing import Callable, Optional, Tuple
+from typing import Callable, Optional, Tuple, Union
import numpy as np
@@ -9,15 +9,19 @@ from supervision.utils.image import crop_image
def move_detections(
- detections: Detections, offset: np.ndarray, image_shape: np.ndarray
+ detections: Detections,
+ offset: np.ndarray,
+ image_shape: Optional[Union[Tuple[int, int, int], Tuple[int, int]]] = None,
) -> Detections:
"""
Args:
detections (sv.Detections): Detections object to be moved.
offset (np.ndarray): An array of shape `(2,)` containing offset values in format
is `[dx, dy]`.
- image_size (np.ndarray): An array of shape `(2,)` or `(3,)`, size of the image
- is in format `[width, height]`.
+ image_size (Tuple, optional): A tuple of image shape. Can be `(2,)` or `(3,)`.
+ Important when moving for segmentation detections, as it defines mask array
+ size.
+
Returns:
(sv.Detections) repositioned Detections object.
"""
diff --git a/supervision/detection/utils.py b/supervision/detection/utils.py
index d5b908b0..d20cadea 100644
--- a/supervision/detection/utils.py
+++ b/supervision/detection/utils.py
@@ -593,7 +593,9 @@ def move_boxes(xyxy: np.ndarray, offset: np.ndarray) -> np.ndarray:
def move_masks(
- masks: np.ndarray, offset: np.ndarray, desired_shape: Optional[np.ndarray] = None
+ masks: np.ndarray,
+ offset: np.ndarray,
+ desired_shape: Optional[Union[Tuple[int, int, int], Tuple[int, int]]] = None,
) -> np.ndarray:
"""
Offset the masks in an array by the specified (x, y) amount.
@@ -602,34 +604,35 @@ def move_masks(
- `masks`: array of shape `(n, y, x)`
- `offset`: array of ints: `(x, y)`
- - `desired_shape`: array of ints, shaped `(y, x, ...)`
+ - `desired_shape`: tuple of ints, shaped `(y, x)` or `(y, x, ...)`
Args:
masks (np.ndarray): array of bools
offset (np.ndarray): An array of shape `(2,)` containing non-negative int values
`[dx, dy]`.
- desired_shape (np.ndarray, optional): Final shape of the mask in the format
- `(height, width, ...)`. If provided, the masks will be padded to match this
- shape. Note the axis order (y,x)!
+ desired_shape (Tuple, optional): Final shape of the mask in the format
+ `(height, width)`, `(height, width, ...)`. The masks will be padded to match
+ the first 2 shape dimensions. Note the axis order (y,x)!
Returns:
(np.ndarray) repositioned masks, optionally padded to the specified shape.
"""
+
if offset[0] < 0 or offset[1] < 0:
raise ValueError(f"Offset values must be non-negative integers. Got: {offset}")
- size_x, size_y = masks.shape[1:] + offset[::-1]
+ size_y, size_x = masks.shape[1:] + offset[::-1]
if desired_shape is not None:
size_y, size_x = desired_shape[:2]
- mask_arr = np.full((masks.shape[0], size_y, size_x), False)
- mask_arr[
+ mask_array = np.full((masks.shape[0], size_y, size_x), False)
+ mask_array[
:,
offset[1] : masks.shape[1] + offset[1],
offset[0] : masks.shape[2] + offset[0],
] = masks
- return mask_arr
+ return mask_array
def scale_boxes(xyxy: np.ndarray, factor: float) -> np.ndarray: