RandomCropFromBorders

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

Randomly crops the input from its borders without resizing.

This transform randomly crops parts of the input (image, mask, bounding boxes, or keypoints) from each of its borders. The amount of cropping is specified as a fraction of the input's dimensions for each side independently.

Arguments
crop_left
float
0.1

The maximum fraction of width to crop from the left side. Must be in the range [0.0, 1.0]. Default: 0.1

crop_right
float
0.1

The maximum fraction of width to crop from the right side. Must be in the range [0.0, 1.0]. Default: 0.1

crop_top
float
0.1

The maximum fraction of height to crop from the top. Must be in the range [0.0, 1.0]. Default: 0.1

crop_bottom
float
0.1

The maximum fraction of height to crop from the bottom. Must be in the range [0.0, 1.0]. Default: 0.1

p
float
1

Probability of applying the transform. Default: 1.0

Examples
>>> import numpy as np
>>> import albumentations as A
>>>
>>> # 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]
>>>
>>> # Define transform with crop fractions for each border
>>> transform = A.Compose([
...     A.RandomCropFromBorders(
...         crop_left=0.1,     # Max 10% crop from left
...         crop_right=0.2,    # Max 20% crop from right
...         crop_top=0.15,     # Max 15% crop from top
...         crop_bottom=0.05,  # Max 5% crop from bottom
...         p=1.0
...     ),
... ], bbox_params=A.BboxParams(coord_format='pascal_voc', label_fields=['bbox_labels']),
...    keypoint_params=A.KeypointParams(coord_format='xy', label_fields=['keypoint_labels']))
>>>
>>> # Apply transform
>>> result = transform(
...     image=image,
...     mask=mask,
...     bboxes=bboxes,
...     bbox_labels=bbox_labels,
...     keypoints=keypoints,
...     keypoint_labels=keypoint_labels
... )
>>>
>>> # Access transformed data
>>> transformed_image = result['image']  # Reduced size image with borders cropped
>>> transformed_mask = result['mask']    # Reduced size mask with borders cropped
>>> transformed_bboxes = result['bboxes']  # Bounding boxes adjusted to new dimensions
>>> transformed_bbox_labels = result['bbox_labels']  # Bounding box labels after crop
>>> transformed_keypoints = result['keypoints']  # Keypoints adjusted to new dimensions
>>> transformed_keypoint_labels = result['keypoint_labels']  # Keypoint labels after crop
>>>
>>> # The resulting output shapes will be smaller, with dimensions reduced by
>>> # the random crop amounts from each side (within the specified maximums)
>>> print(f"Original image shape: (100, 100, 3)")
>>> print(f"Transformed image shape: {transformed_image.shape}")  # e.g., (85, 75, 3)
Notes
  • The actual amount of cropping for each side is randomly chosen between 0 and the specified maximum for each application of the transform.
  • The sum of crop_left and crop_right must not exceed 1.0, and the sum of crop_top and crop_bottom must not exceed 1.0. Otherwise, a ValueError will be raised.
  • This transform does not resize the input after cropping, so the output dimensions will be smaller than the input dimensions.
  • Bounding boxes that end up fully outside the cropped area will be removed.
  • Keypoints that end up outside the cropped area will be removed.