albumentations.augmentations.crops.functional
Functional implementations of image cropping operations. This module provides utility functions for performing various cropping operations on images, bounding boxes, and keypoints. It includes functions to calculate crop coordinates, crop images, and handle the corresponding transformations for bounding boxes and keypoints to maintain consistency between different data types during cropping operations.
Members
- functioncrop
- functioncrop_and_pad
- functioncrop_and_pad_bboxes
- functioncrop_and_pad_keypoints
- functioncrop_bboxes_by_coords
- functioncrop_keypoints_by_coords
- functionget_center_crop_coords
- functionget_crop_coords
- functionpad_along_axes
- functionvolume_crop_yx
- functionvolumes_crop_yx
cropfunction
Crop an image. This function crops an image.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| img | One of:
| - | Input image. |
| x_min | int | - | Minimum x coordinate. |
| y_min | int | - | Minimum y coordinate. |
| x_max | int | - | Maximum x coordinate. |
| y_max | int | - | Maximum y coordinate. |
Returns
- np.ndarray: Cropped image.
crop_and_padfunction
Crop and pad an image. This function crops and pads an image.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| img | One of:
| - | Input image. |
| crop_params | One of:
| - | Crop parameters. |
| pad_params | One of:
| - | Pad parameters. |
| pad_value | One of:
| - | Pad value. |
| image_shape | tuple | - | Original image shape. |
| interpolation | int | - | Interpolation method. |
| pad_mode | int | - | Pad mode. |
| keep_size | bool | - | Whether to keep the original size. |
Returns
- np.ndarray: Cropped and padded image.
crop_and_pad_bboxesfunction
Crop and pad bounding boxes. This function crops and pads bounding boxes. Supports both HBB and OBB. Simply shifts coordinates - clipping/filtering happens in Compose via clamping_mode.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| bboxes | ndarray | - | Array of bounding boxes (HBB or OBB) with any number of columns. First 4 columns are [x_min, y_min, x_max, y_max], rest are preserved. |
| crop_params | One of:
| - | Crop parameters. |
| pad_params | One of:
| - | Pad parameters. |
| image_shape | tuple | - | Original image shape. |
| result_shape | tuple | - | Result image shape. |
Returns
- np.ndarray: Array of cropped and padded bounding boxes.
crop_and_pad_keypointsfunction
Crop and pad multiple keypoints simultaneously.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| keypoints | ndarray | - | Array of keypoints with shape (N, 4+) where each row is (x, y, angle, scale, ...). |
| crop_params | One of:
| None | Crop parameters [crop_x1, crop_y1, ...]. |
| pad_params | One of:
| None | Pad parameters [top, bottom, left, right]. |
| image_shape | tuple | (0, 0) | Original image shape (rows, cols). |
| result_shape | tuple | (0, 0) | Result image shape (rows, cols). |
| keep_size | bool | False | Whether to keep the original size. |
Returns
- np.ndarray: Array of transformed keypoints with the same shape as input.
crop_bboxes_by_coordsfunction
Crop bounding boxes based on given crop coordinates. This function adjusts bounding boxes to fit within a cropped image. Supports both HBB (axis-aligned) and OBB (oriented) bounding boxes. Simply shifts the bounding rectangle coordinates - clipping/filtering happens later in Compose via clamping_mode.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| bboxes | ndarray | - | Array of normalized bounding boxes (Albumentations format) with shape (N, 4+) where each row is [x_min, y_min, x_max, y_max, ...] for HBB or [x_min, y_min, x_max, y_max, angle, ...] for OBB. |
| crop_coords | tuple | - | Crop coordinates (x_min, y_min, x_max, y_max) in absolute pixel values. |
| image_shape | tuple | - | Original image shape (height, width). |
Returns
- np.ndarray: Array of cropped bounding boxes in normalized coordinates (Albumentations format).
crop_keypoints_by_coordsfunction
Crop keypoints using the provided coordinates of bottom-left and top-right corners in pixels.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| keypoints | ndarray | - | An array of keypoints with shape (N, 4+) where each row is (x, y, angle, scale, ...). |
| crop_coords | tuple | - | Crop box coords (x1, y1, x2, y2). |
Returns
- np.ndarray: An array of cropped keypoints with the same shape as the input.
get_center_crop_coordsfunction
Get center crop coordinates. This function gets the center crop coordinates.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| image_shape | tuple | - | Original image shape. |
| crop_shape | tuple | - | Crop shape. |
Returns
- tuple[int, int, int, int]: Center crop coordinates.
get_crop_coordsfunction
Get crop coordinates. This function gets the crop coordinates.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| image_shape | tuple | - | Original image shape. |
| crop_shape | tuple | - | Crop shape. |
| h_start | float | - | Start height. |
| w_start | float | - | Start width. |
Returns
- tuple[int, int, int, int]: Crop coordinates.
pad_along_axesfunction
Pad an array along specified height (H) and width (W) axes using np.pad.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| arr | ndarray | - | Input array. |
| pad_top | int | - | Padding added to the top (start of H axis). |
| pad_bottom | int | - | Padding added to the bottom (end of H axis). |
| pad_left | int | - | Padding added to the left (start of W axis). |
| pad_right | int | - | Padding added to the right (end of W axis). |
| h_axis | int | - | Index of the height axis (Y). |
| w_axis | int | - | Index of the width axis (X). |
| border_mode | int | - | OpenCV border mode. |
| pad_value | One of:
| 0 | Value for constant padding. |
Returns
- np.ndarray: Padded array.
volume_crop_yxfunction
Crop a single volume along Y (height) and X (width) axes only.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| volume | One of:
| - | Input volume with shape (D, H, W) or (D, H, W, C). |
| x_min | int | - | Minimum width coordinate. |
| y_min | int | - | Minimum height coordinate. |
| x_max | int | - | Maximum width coordinate. |
| y_max | int | - | Maximum height coordinate. |
Returns
- np.ndarray: Cropped volume (D, H_new, W_new, [C]).
volumes_crop_yxfunction
Crop a batch of volumes along Y (height) and X (width) axes only.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| volumes | ndarray | - | Input batch of volumes with shape (B, D, H, W) or (B, D, H, W, C). |
| x_min | int | - | Minimum width coordinate. |
| y_min | int | - | Minimum height coordinate. |
| x_max | int | - | Maximum width coordinate. |
| y_max | int | - | Maximum height coordinate. |
Returns
- np.ndarray: Cropped batch of volumes (B, D, H_new, W_new, [C]).