Skip to content

pixano.utils.boxes

denormalize_coords(coord, height, width, rounded_int=True)

Denormalize coordinates

Parameters:

Name Type Description Default
coord list[float]

Normalized coordinates

required
height int

Height

required
width int

Width

required
rounded_int bool

True to round denormalized float to nearest integer. Default to True

True

Returns:

Type Description
list[float]

Unnormalized coordinates,

Source code in pixano/utils/boxes.py
def denormalize_coords(
    coord: list[float], height: int, width: int, rounded_int=True
) -> list[float]:
    """Denormalize coordinates

    Args:
        coord (list[float]): Normalized coordinates
        height (int): Height
        width (int): Width
        rounded_int (bool): True to round denormalized float to nearest integer. Default to True

    Returns:
        list[float]: Unnormalized coordinates,
    """

    denorm = []

    for i, c in enumerate(coord):
        if i % 2 == 0:
            denorm.append(round(c * width) if rounded_int else c * width)
        else:
            denorm.append(round(c * height) if rounded_int else c * height)

    return denorm

mask_to_bbox(mask)

Return the smallest bounding box containing all the mask pixels

Parameters:

Name Type Description Default
mask ndarray

Mask as NumPy Array

required

Returns:

Type Description
list[float]

Normalized xywh bounding box

Source code in pixano/utils/boxes.py
def mask_to_bbox(mask: np.ndarray) -> list[float]:
    """Return the smallest bounding box containing all the mask pixels

    Args:
        mask (np.ndarray): Mask as NumPy Array

    Returns:
        list[float]: Normalized xywh bounding box
    """

    height, width = mask.shape
    bool_mask = np.array(mask).astype(bool)

    # Find all columns and rows that contain ones
    rows = np.any(bool_mask, axis=1)
    cols = np.any(bool_mask, axis=0)

    # Find the min and max col/row index that contain ones
    rmin, rmax = np.where(rows)[0][[0, -1]]
    cmin, cmax = np.where(cols)[0][[0, -1]]

    # Calculate bbox height and width
    w = (cmax - cmin + 1) / width
    h = (rmax - rmin + 1) / height

    return [cmin / width, rmin / height, w, h]

normalize_coords(coord, height, width)

Normalize coordinates

Parameters:

Name Type Description Default
coord list[float]

Unnormalized coordinates

required
height int

Height

required
width int

Width

required

Returns:

Type Description
list[float]

Normalized coordinates

Source code in pixano/utils/boxes.py
def normalize_coords(coord: list[float], height: int, width: int) -> list[float]:
    """Normalize coordinates

    Args:
        coord (list[float]): Unnormalized coordinates
        height (int): Height
        width (int): Width

    Returns:
        list[float]: Normalized coordinates
    """

    norm = []

    for i, c in enumerate(coord):
        if i % 2 == 0:
            norm.append(c / width)
        else:
            norm.append(c / height)

    return norm

urle_to_bbox(urle)

Return the smallest bounding box containing all the mask pixels

Parameters:

Name Type Description Default
urle dict

Mask as uncompressed RLE

required

Returns:

Type Description
list[float]

Normalized xywh bounding box

Source code in pixano/utils/boxes.py
def urle_to_bbox(urle: dict) -> list[float]:
    """Return the smallest bounding box containing all the mask pixels

    Args:
        urle (dict): Mask as uncompressed RLE

    Returns:
        list[float]: Normalized xywh bounding box
    """

    return mask_to_bbox(rle_to_mask(urle_to_rle(urle)))

xywh_to_xyxy(xywh)

Convert bounding box coordinates from xywh (using top left point as reference) to xyxy

Parameters:

Name Type Description Default
xywh list[float]

xywh coordinates

required

Returns:

Type Description
list[float]

xyxy coordinates

Source code in pixano/utils/boxes.py
def xywh_to_xyxy(xywh: list[float]) -> list[float]:
    """Convert bounding box coordinates from xywh (using top left point as reference) to xyxy

    Args:
        xywh (list[float]): xywh coordinates

    Returns:
        list[float]: xyxy coordinates
    """

    return [
        float(xywh[0]),
        float(xywh[1]),
        float(xywh[0] + xywh[2]),
        float(xywh[1] + xywh[3]),
    ]

xyxy_to_xywh(xyxy)

Convert bounding box coordinates from xyxy to xywh (using top left point as reference)

Parameters:

Name Type Description Default
xyxy list[float]

xyxy coordinates

required

Returns:

Type Description
list[float]

xywh coordinates

Source code in pixano/utils/boxes.py
def xyxy_to_xywh(xyxy: list[float]) -> list[float]:
    """Convert bounding box coordinates from xyxy to xywh (using top left point as reference)

    Args:
        xyxy (list[float]): xyxy coordinates

    Returns:
        list[float]: xywh coordinates
    """

    return [
        float(xyxy[0]),
        float(xyxy[1]),
        float(xyxy[2] - xyxy[0]),
        float(xyxy[3] - xyxy[1]),
    ]