Skip to content

Reference for ultralytics/models/fastsam/utils.py

Note

This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/fastsam/utils.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!


ultralytics.models.fastsam.utils.adjust_bboxes_to_image_border

adjust_bboxes_to_image_border(boxes, image_shape, threshold=20)

Adjust bounding boxes to stick to image border if they are within a certain threshold.

Parameters:

Name Type Description Default
boxes Tensor

(n, 4)

required
image_shape tuple

(height, width)

required
threshold int

pixel threshold

20

Returns:

Name Type Description
adjusted_boxes Tensor

adjusted bounding boxes

Source code in ultralytics/models/fastsam/utils.py
def adjust_bboxes_to_image_border(boxes, image_shape, threshold=20):
    """
    Adjust bounding boxes to stick to image border if they are within a certain threshold.

    Args:
        boxes (torch.Tensor): (n, 4)
        image_shape (tuple): (height, width)
        threshold (int): pixel threshold

    Returns:
        adjusted_boxes (torch.Tensor): adjusted bounding boxes
    """

    # Image dimensions
    h, w = image_shape

    # Adjust boxes
    boxes[boxes[:, 0] < threshold, 0] = 0  # x1
    boxes[boxes[:, 1] < threshold, 1] = 0  # y1
    boxes[boxes[:, 2] > w - threshold, 2] = w  # x2
    boxes[boxes[:, 3] > h - threshold, 3] = h  # y2
    return boxes





Created 2023-11-12, Updated 2024-07-23
Authors: sometimesocrazy@gmail.com (1), glenn-jocher (6), Burhan-Q (1)