Crop

Targets:
image
mask
bboxes
keypoints
volume
mask3d
Image Types:uint8, float32

Crop a specific region from the input image.

This transform crops a rectangular region from the input image, mask, bounding boxes, and keypoints based on specified coordinates. It's useful when you want to extract a specific area of interest from your inputs.

Arguments
x_min
int
0

Minimum x-coordinate of the crop region (left edge). Must be >= 0. Default: 0.

y_min
int
0

Minimum y-coordinate of the crop region (top edge). Must be >= 0. Default: 0.

x_max
int
1024

Maximum x-coordinate of the crop region (right edge). Must be > x_min. Default: 1024.

y_max
int
1024

Maximum y-coordinate of the crop region (bottom edge). Must be > y_min. Default: 1024.

pad_if_needed
bool
false

Whether to pad if crop coordinates exceed image dimensions. Default: False.

border_mode
0 | 1 | 2 | 3 | 4
0

OpenCV border mode used for padding. Default: cv2.BORDER_CONSTANT.

fill
tuple[float, ...] | float
0

Padding value if border_mode is cv2.BORDER_CONSTANT. Default: 0.

fill_mask
tuple[float, ...] | float
0

Padding value for masks. Default: 0.

pad_position
center | top_left | top_right | bottom_left | bottom_right | random
center

Position of padding. Default: 'center'.

p
float
1

Probability of applying the transform. Default: 1.0.

Examples
>>> import numpy as np
>>> import albumentations as A
>>> import cv2
>>>
>>> # Prepare sample data
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> mask = np.random.randint(0, 2, (100, 100), dtype=np.uint8)
>>> bboxes = np.array([[10, 10, 50, 50], [40, 40, 80, 80]], dtype=np.float32)
>>> bbox_labels = [1, 2]
>>> keypoints = np.array([[20, 30], [60, 70]], dtype=np.float32)
>>> keypoint_labels = [0, 1]
>>>
>>> # Example 1: Basic crop with fixed coordinates
>>> transform = A.Compose([
...     A.Crop(x_min=20, y_min=20, x_max=80, y_max=80),
... ], bbox_params=A.BboxParams(coord_format='pascal_voc', label_fields=['bbox_labels']),
...    keypoint_params=A.KeypointParams(coord_format='xy', label_fields=['keypoint_labels']))
>>>
>>> # Apply the transform
>>> transformed = transform(
...     image=image,
...     mask=mask,
...     bboxes=bboxes,
...     bbox_labels=bbox_labels,
...     keypoints=keypoints,
...     keypoint_labels=keypoint_labels
... )
>>>
>>> # Get the transformed data
>>> transformed_image = transformed['image']  # Will be 60x60 - cropped from (20,20) to (80,80)
>>> transformed_mask = transformed['mask']    # Will be 60x60
>>> transformed_bboxes = transformed['bboxes']  # Bounding boxes adjusted to the cropped area
>>> transformed_bbox_labels = transformed['bbox_labels']  # Labels for boxes that remain after cropping
>>>
>>> # Example 2: Crop with padding when the crop region extends beyond image dimensions
>>> transform_padded = A.Compose([
...     A.Crop(
...         x_min=50, y_min=50, x_max=150, y_max=150,  # Extends beyond the 100x100 image
...         pad_if_needed=True,
...         border_mode=cv2.BORDER_CONSTANT,
...         fill=0,      # Black padding for image
...         fill_mask=0  # Zero padding for mask
...     ),
... ], bbox_params=A.BboxParams(coord_format='pascal_voc', label_fields=['bbox_labels']),
...    keypoint_params=A.KeypointParams(coord_format='xy', label_fields=['keypoint_labels']))
>>>
>>> # Apply the padded transform
>>> padded_transformed = transform_padded(
...     image=image,
...     mask=mask,
...     bboxes=bboxes,
...     bbox_labels=bbox_labels,
...     keypoints=keypoints,
...     keypoint_labels=keypoint_labels
... )
>>>
>>> # The result will be 100x100 (50:150, 50:150) with padding on right and bottom
>>> padded_image = padded_transformed['image']  # 100x100 with 50 pixels of original + 50 pixels of padding
>>> padded_mask = padded_transformed['mask']
>>> padded_bboxes = padded_transformed['bboxes']  # Coordinates adjusted to the cropped and padded area
>>>
>>> # Example 3: Crop with reflection padding and custom position
>>> transform_reflect = A.Compose([
...     A.Crop(
...         x_min=-20, y_min=-20, x_max=80, y_max=80,  # Negative coordinates (outside image)
...         pad_if_needed=True,
...         border_mode=cv2.BORDER_REFLECT_101,  # Reflect image for padding
...         pad_position="top_left"  # Apply padding at top-left
...     ),
... ], bbox_params=A.BboxParams(coord_format='pascal_voc', label_fields=['bbox_labels']))
>>>
>>> # The resulting crop will use reflection padding for the negative coordinates
>>> reflect_result = transform_reflect(
...     image=image,
...     bboxes=bboxes,
...     bbox_labels=bbox_labels
... )
Notes
  • The crop coordinates are applied as follows: x_min <= x < x_max and y_min <= y < y_max.
  • If pad_if_needed is False and crop region extends beyond image boundaries, it will be clipped.
  • If pad_if_needed is True, image will be padded to accommodate the full crop region.
  • For bounding boxes and keypoints, coordinates are adjusted appropriately for both padding and cropping.