Skip to content

Crop functional transforms (augmentations.crops.functional)

def bbox_crop (bbox, x_min, y_min, x_max, y_max, rows, cols) [view source on GitHub]

Crop a bounding box.

Parameters:

Name Type Description
bbox Tuple[float, float, float, float]

A bounding box (x_min, y_min, x_max, y_max).

x_min int
y_min int
x_max int
y_max int
rows int

Image rows.

cols int

Image cols.

Returns:

Type Description
Tuple[float, float, float, float]

A cropped bounding box (x_min, y_min, x_max, y_max).

Source code in albumentations/augmentations/crops/functional.py
Python
def bbox_crop(
    bbox: BoxInternalType,
    x_min: int,
    y_min: int,
    x_max: int,
    y_max: int,
    rows: int,
    cols: int,
) -> BoxInternalType:
    """Crop a bounding box.

    Args:
        bbox: A bounding box `(x_min, y_min, x_max, y_max)`.
        x_min:
        y_min:
        x_max:
        y_max:
        rows: Image rows.
        cols: Image cols.

    Returns:
        A cropped bounding box `(x_min, y_min, x_max, y_max)`.

    """
    crop_coords = x_min, y_min, x_max, y_max
    crop_height = y_max - y_min
    crop_width = x_max - x_min
    return crop_bbox_by_coords(bbox, crop_coords, crop_height, crop_width, rows, cols)

def crop_bbox_by_coords (bbox, crop_coords, crop_height, crop_width, rows, cols) [view source on GitHub]

Crop a bounding box using the provided coordinates of bottom-left and top-right corners in pixels and the required height and width of the crop.

Parameters:

Name Type Description
bbox Tuple[float, float, float, float]

A cropped box (x_min, y_min, x_max, y_max).

crop_coords Tuple[int, int, int, int]

Crop coordinates (x1, y1, x2, y2).

crop_height int
crop_width int
rows int

Image rows.

cols int

Image cols.

Returns:

Type Description
Tuple[float, float, float, float]

A cropped bounding box (x_min, y_min, x_max, y_max).

Source code in albumentations/augmentations/crops/functional.py
Python
def crop_bbox_by_coords(
    bbox: BoxInternalType,
    crop_coords: Tuple[int, int, int, int],
    crop_height: int,
    crop_width: int,
    rows: int,
    cols: int,
) -> BoxInternalType:
    """Crop a bounding box using the provided coordinates of bottom-left and top-right corners in pixels and the
    required height and width of the crop.

    Args:
        bbox: A cropped box `(x_min, y_min, x_max, y_max)`.
        crop_coords: Crop coordinates `(x1, y1, x2, y2)`.
        crop_height:
        crop_width:
        rows: Image rows.
        cols: Image cols.

    Returns:
        A cropped bounding box `(x_min, y_min, x_max, y_max)`.

    """
    normalized_bbox = denormalize_bbox(bbox, rows, cols)
    x_min, y_min, x_max, y_max = normalized_bbox[:4]
    x1, y1 = crop_coords[:2]
    cropped_bbox = x_min - x1, y_min - y1, x_max - x1, y_max - y1
    return cast(BoxInternalType, normalize_bbox(cropped_bbox, crop_height, crop_width))

def crop_keypoint_by_coords (keypoint, crop_coords) [view source on GitHub]

Crop a keypoint using the provided coordinates of bottom-left and top-right corners in pixels and the required height and width of the crop.

Parameters:

Name Type Description
keypoint tuple

A keypoint (x, y, angle, scale).

crop_coords tuple

Crop box coords (x1, x2, y1, y2).

Returns:

Type Description
Tuple[float, float, float, float]

A keypoint (x, y, angle, scale).

Source code in albumentations/augmentations/crops/functional.py
Python
def crop_keypoint_by_coords(
    keypoint: KeypointInternalType,
    crop_coords: Tuple[int, int, int, int],
) -> KeypointInternalType:
    """Crop a keypoint using the provided coordinates of bottom-left and top-right corners in pixels and the
    required height and width of the crop.

    Args:
        keypoint (tuple): A keypoint `(x, y, angle, scale)`.
        crop_coords (tuple): Crop box coords `(x1, x2, y1, y2)`.

    Returns:
        A keypoint `(x, y, angle, scale)`.

    """
    x, y, angle, scale = keypoint[:4]
    x1, y1 = crop_coords[:2]
    return x - x1, y - y1, angle, scale

def keypoint_center_crop (keypoint, crop_height, crop_width, rows, cols) [view source on GitHub]

Keypoint center crop.

Parameters:

Name Type Description
keypoint Tuple[float, float, float, float]

A keypoint (x, y, angle, scale).

crop_height int

Crop height.

crop_width int

Crop width.

rows int

Image height.

cols int

Image width.

Returns:

Type Description
Tuple[float, float, float, float]

A keypoint (x, y, angle, scale).

Source code in albumentations/augmentations/crops/functional.py
Python
def keypoint_center_crop(
    keypoint: KeypointInternalType,
    crop_height: int,
    crop_width: int,
    rows: int,
    cols: int,
) -> KeypointInternalType:
    """Keypoint center crop.

    Args:
        keypoint: A keypoint `(x, y, angle, scale)`.
        crop_height: Crop height.
        crop_width: Crop width.
        rows: Image height.
        cols: Image width.

    Returns:
        A keypoint `(x, y, angle, scale)`.

    """
    crop_coords = get_center_crop_coords(rows, cols, crop_height, crop_width)
    return crop_keypoint_by_coords(keypoint, crop_coords)

def keypoint_random_crop (keypoint, crop_height, crop_width, h_start, w_start, rows, cols) [view source on GitHub]

Keypoint random crop.

Parameters:

Name Type Description
keypoint Tuple[float, float, float, float]

(tuple): A keypoint (x, y, angle, scale).

crop_height int

Crop height.

crop_width int

Crop width.

h_start int

Crop height start.

w_start int

Crop width start.

rows int

Image height.

cols int

Image width.

Returns:

Type Description
Tuple[float, float, float, float]

A keypoint (x, y, angle, scale).

Source code in albumentations/augmentations/crops/functional.py
Python
def keypoint_random_crop(
    keypoint: KeypointInternalType,
    crop_height: int,
    crop_width: int,
    h_start: float,
    w_start: float,
    rows: int,
    cols: int,
) -> KeypointInternalType:
    """Keypoint random crop.

    Args:
        keypoint: (tuple): A keypoint `(x, y, angle, scale)`.
        crop_height (int): Crop height.
        crop_width (int): Crop width.
        h_start (int): Crop height start.
        w_start (int): Crop width start.
        rows (int): Image height.
        cols (int): Image width.

    Returns:
        A keypoint `(x, y, angle, scale)`.

    """
    crop_coords = get_random_crop_coords(rows, cols, crop_height, crop_width, h_start, w_start)
    return crop_keypoint_by_coords(keypoint, crop_coords)