CenterCrop

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

Crop the central part of the input.

This transform crops the center of the input image, mask, bounding boxes, and keypoints to the specified dimensions. It's useful when you want to focus on the central region of the input, discarding peripheral information.

Arguments
height
int

The height of the crop. Must be greater than 0.

width
int

The width of the crop. Must be greater than 0.

pad_if_needed
bool
false

Whether to pad if crop size exceeds image size. 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 for images if border_mode is cv2.BORDER_CONSTANT. Default: 0.

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

Padding value for masks if border_mode is cv2.BORDER_CONSTANT. 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 center crop without padding
>>> transform = A.Compose([
...     A.CenterCrop(height=64, width=64),
... ], 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 64x64
>>> transformed_mask = transformed['mask']    # Will be 64x64
>>> transformed_bboxes = transformed['bboxes']  # Bounding boxes adjusted to the cropped area
>>> transformed_bbox_labels = transformed['bbox_labels']  # Labels for boxes that remain after cropping
>>> transformed_keypoints = transformed['keypoints']  # Keypoints adjusted to the cropped area
>>> transformed_keypoint_labels = transformed['keypoint_labels']  # Labels for keypoints that remain
>>>
>>> # Example 2: Center crop with padding when needed
>>> transform_padded = A.Compose([
...     A.CenterCrop(
...         height=120,  # Larger than original image height
...         width=120,   # Larger than original image width
...         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 120x120 with padding
>>> padded_image = padded_transformed['image']
>>> padded_mask = padded_transformed['mask']
>>> padded_bboxes = padded_transformed['bboxes']  # Coordinates adjusted to the new dimensions
>>> padded_keypoints = padded_transformed['keypoints']  # Coordinates adjusted to the new dimensions
Notes
  • If pad_if_needed is False and crop size exceeds image dimensions, it will raise a CropSizeError.
  • If pad_if_needed is True and crop size exceeds image dimensions, the image will be padded.
  • For bounding boxes and keypoints, coordinates are adjusted appropriately for both padding and cropping.