util.clip_boxes

lucid.models.objdet.util.clip_boxes(boxes: Tensor, image_shape: tuple[int, int]) Tensor

The clip_boxes function constrains bounding boxes to lie within the spatial bounds of the image.

Function Signature

def clip_boxes(boxes: Tensor, image_shape: tuple[int, int]) -> Tensor

Parameters

  • boxes (Tensor): Tensor of shape \((N, 4)\) representing bounding boxes in the format \((x_1, y_1, x_2, y_2)\).

  • image_shape (tuple[int, int]): Tuple representing the image shape as \((H, W)\).

Returns

  • Tensor: A new tensor of shape \((N, 4)\) where each coordinate is clipped to remain within the image boundaries.

Note

Coordinates are clipped between \([0, W - 1]\) for x and \([0, H - 1]\) for y.

Example

>>> from lucid.models.objdet.util import clip_boxes
>>> boxes = lucid.Tensor([[10, 10, 120, 130], [-5, -5, 50, 60]])
>>> clipped = clip_boxes(boxes, image_shape=(100, 100))
>>> print(clipped)
Tensor([[10, 10, 99, 99],
        [ 0,  0, 50, 60]], ...)