Interested in advertising?

Contact us

Stay updated

News & Insights
transform
utils
check_version
transforms

albumentations.augmentations.crops.transforms


Transform classes for cropping operations on images and other data types. This module provides various crop transforms that can be applied to images, masks, bounding boxes, and keypoints. The transforms include simple cropping, random cropping, center cropping, cropping near bounding boxes, and other specialized cropping operations that maintain the integrity of bounding boxes. These transforms are designed to work within the albumentations pipeline and can be used for data augmentation in computer vision tasks.

AtLeastOneBBoxRandomCropclass

Crop an area from image while ensuring at least one bounding box is present in the crop. Similar to BBoxSafeRandomCrop, but with a key difference: - BBoxSafeRandomCrop ensures ALL bounding boxes are preserved in the crop - AtLeastOneBBoxRandomCrop ensures AT LEAST ONE bounding box is present in the crop This makes AtLeastOneBBoxRandomCrop more flexible for scenarios where: - You want to focus on individual objects rather than all objects - You're willing to lose some bounding boxes to get more varied crops - The image has many bounding boxes and keeping all of them would be too restrictive The algorithm: 1. If bounding boxes exist: - Randomly selects a reference bounding box from available boxes - Computes an eroded version of this box (shrunk by erosion_factor) - Calculates valid crop bounds that ensure overlap with the eroded box - Randomly samples crop coordinates within these bounds 2. If no bounding boxes exist: - Uses full image dimensions as valid bounds - Randomly samples crop coordinates within these bounds

Parameters

NameTypeDefaultDescription
heightint-Fixed height of the crop
widthint-Fixed width of the crop
erosion_factorfloat0.0Factor by which to erode (shrink) the reference bounding box when computing valid crop regions. Must be in range [0.0, 1.0]. - 0.0 means no erosion (crop must fully contain the reference box) - 1.0 means maximum erosion (crop can be anywhere that intersects the reference box) Defaults to 0.0.
pfloat1.0Probability of applying the transform. Defaults to 1.0.

Examples

>>> import numpy as np
>>> import albumentations as A
>>> import cv2
>>>
>>> # Prepare sample data
>>> image = np.random.randint(0, 256, (300, 300, 3), dtype=np.uint8)
>>> mask = np.random.randint(0, 2, (300, 300), dtype=np.uint8)
>>> # Create multiple bounding boxes - the transform will ensure at least one is in the crop
>>> bboxes = np.array([
...     [30, 50, 100, 140],   # first box
...     [150, 120, 270, 250], # second box
...     [200, 30, 280, 90]    # third box
... ], dtype=np.float32)
>>> bbox_labels = [1, 2, 3]
>>> keypoints = np.array([
...     [50, 70],    # keypoint inside first box
...     [190, 170],  # keypoint inside second box
...     [240, 60]    # keypoint inside third box
... ], dtype=np.float32)
>>> keypoint_labels = [0, 1, 2]
>>>
>>> # Define transform with different erosion_factor values
>>> transform = A.Compose([
...     A.AtLeastOneBBoxRandomCrop(
...         height=200,
...         width=200,
...         erosion_factor=0.2,  # Allows moderate flexibility in crop placement
...         p=1.0
...     ),
... ], bbox_params=A.BboxParams(format='pascal_voc', label_fields=['bbox_labels']),
...    keypoint_params=A.KeypointParams(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']       # Shape: (200, 200, 3)
>>> transformed_mask = transformed['mask']         # Shape: (200, 200)
>>> transformed_bboxes = transformed['bboxes']     # At least one bbox is guaranteed
>>> transformed_bbox_labels = transformed['bbox_labels']  # Labels for the preserved bboxes
>>> transformed_keypoints = transformed['keypoints']      # Only keypoints in crop are kept
>>> transformed_keypoint_labels = transformed['keypoint_labels']  # Their labels
>>>
>>> # Verify that at least one bounding box was preserved
>>> assert len(transformed_bboxes) > 0, "Should have at least one bbox in the crop"
>>>
>>> # With erosion_factor=0.0, the crop must fully contain the selected reference bbox
>>> conservative_transform = A.Compose([
...     A.AtLeastOneBBoxRandomCrop(
...         height=200,
...         width=200,
...         erosion_factor=0.0,  # No erosion - crop must fully contain a bbox
...         p=1.0
...     ),
... ], bbox_params=A.BboxParams(format='pascal_voc', label_fields=['bbox_labels']))
>>>
>>> # With erosion_factor=1.0, the crop must only intersect with the selected reference bbox
>>> flexible_transform = A.Compose([
...     A.AtLeastOneBBoxRandomCrop(
...         height=200,
...         width=200,
...         erosion_factor=1.0,  # Maximum erosion - crop only needs to intersect a bbox
...         p=1.0
...     ),
... ], bbox_params=A.BboxParams(format='pascal_voc', label_fields=['bbox_labels']))

Notes

- Uses fixed crop dimensions (height and width) - Bounding boxes that end up partially outside the crop will be adjusted - Bounding boxes that end up completely outside the crop will be removed - If no bounding boxes are provided, acts as a regular random crop

BBoxSafeRandomCropclass

Crop an area from image while ensuring all bounding boxes are preserved in the crop. Similar to AtLeastOneBboxRandomCrop, but with a key difference: - BBoxSafeRandomCrop ensures ALL bounding boxes are preserved in the crop when erosion_rate=0.0 - AtLeastOneBboxRandomCrop ensures AT LEAST ONE bounding box is present in the crop This makes BBoxSafeRandomCrop more suitable for scenarios where: - You need to preserve all objects in the scene - Losing any bounding box would be problematic (e.g., rare object classes) - You're training a model that needs to detect multiple objects simultaneously The algorithm: 1. If bounding boxes exist: - Computes the union of all bounding boxes - Applies erosion based on erosion_rate to this union - Clips the eroded union to valid image coordinates [0,1] - Randomly samples crop coordinates within the clipped union area 2. If no bounding boxes exist: - Computes crop height based on erosion_rate - Sets crop width to maintain original aspect ratio - Randomly places the crop within the image

Parameters

NameTypeDefaultDescription
erosion_ratefloat0.0Controls how much the valid crop region can deviate from the bbox union. Must be in range [0.0, 1.0]. - 0.0: crop must contain the exact bbox union (safest option that guarantees all boxes are preserved) - 1.0: crop can deviate maximally from the bbox union (increases likelihood of cutting off some boxes) Defaults to 0.0.
pfloat1.0Probability of applying the transform. Defaults to 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 erosion_rate parameter
>>> transform = A.Compose([
...     A.BBoxSafeRandomCrop(erosion_rate=0.2),
... ], bbox_params=A.BboxParams(format='pascal_voc', label_fields=['bbox_labels']),
...    keypoint_params=A.KeypointParams(format='xy', label_fields=['keypoint_labels']))
>>>
>>> # Apply the transform
>>> result = transform(
...     image=image,
...     mask=mask,
...     bboxes=bboxes,
...     bbox_labels=bbox_labels,
...     keypoints=keypoints,
...     keypoint_labels=keypoint_labels
... )
>>>
>>> # Get the transformed data
>>> transformed_image = result['image']  # Cropped image containing all bboxes
>>> transformed_mask = result['mask']    # Cropped mask
>>> transformed_bboxes = result['bboxes']  # All bounding boxes preserved with adjusted coordinates
>>> transformed_bbox_labels = result['bbox_labels']  # Original labels preserved
>>> transformed_keypoints = result['keypoints']  # Keypoints with adjusted coordinates
>>> transformed_keypoint_labels = result['keypoint_labels']  # Original keypoint labels preserved
>>>
>>> # Example with a different erosion_rate
>>> transform_more_flexible = A.Compose([
...     A.BBoxSafeRandomCrop(erosion_rate=0.5),  # More flexibility in crop placement
... ], bbox_params=A.BboxParams(format='pascal_voc', label_fields=['bbox_labels']))
>>>
>>> # Apply transform with only image and bboxes
>>> result_bboxes_only = transform_more_flexible(
...     image=image,
...     bboxes=bboxes,
...     bbox_labels=bbox_labels
... )
>>> transformed_image = result_bboxes_only['image']
>>> transformed_bboxes = result_bboxes_only['bboxes']  # All bboxes still preserved

Notes

- IMPORTANT: Using erosion_rate > 0.0 may result in some bounding boxes being cut off, particularly narrow boxes at the boundary of the union area. For guaranteed preservation of all bounding boxes, use erosion_rate=0.0. - Aspect ratio is preserved only when no bounding boxes are present - May be more restrictive in crop placement compared to AtLeastOneBboxRandomCrop - The crop size is determined by the bounding boxes when present

BaseCropclass

Base class for transforms that only perform cropping. This abstract class provides the foundation for all cropping transformations. It handles cropping of different data types including images, masks, bounding boxes, keypoints, and volumes while keeping their spatial relationships intact. Child classes must implement the `get_params_dependent_on_data` method to determine crop coordinates based on transform-specific logic. This method should return a dictionary containing at least a 'crop_coords' key with a tuple value (x_min, y_min, x_max, y_max).

Parameters

NameTypeDefaultDescription
pfloat0.5Probability of applying the transform. Default: 1.0.

Examples

>>> import numpy as np
>>> import albumentations as A
>>> from albumentations.augmentations.crops.transforms import BaseCrop
>>>
>>> # Example of a custom crop transform that inherits from BaseCrop
>>> class CustomCenterCrop(BaseCrop):
...     '''A simple custom center crop with configurable size'''
...     def __init__(self, crop_height, crop_width, p=1.0):
...         super().__init__(p=p)
...         self.crop_height = crop_height
...         self.crop_width = crop_width
...
...     def get_params_dependent_on_data(self, params, data):
...         '''Calculate crop coordinates based on center of image'''
...         image_height, image_width = params["shape"][:2]
...
...         # Calculate center crop coordinates
...         x_min = max(0, (image_width - self.crop_width) // 2)
...         y_min = max(0, (image_height - self.crop_height) // 2)
...         x_max = min(image_width, x_min + self.crop_width)
...         y_max = min(image_height, y_min + self.crop_height)
...
...         return {"crop_coords": (x_min, y_min, x_max, y_max)}
>>>
>>> # 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]
>>>
>>> # Use the custom transform in a pipeline
>>> transform = A.Compose(
...     [CustomCenterCrop(crop_height=80, crop_width=80)],
...     bbox_params=A.BboxParams(format='pascal_voc', label_fields=['bbox_labels']),
...     keypoint_params=A.KeypointParams(format='xy', label_fields=['keypoint_labels'])
... )
>>>
>>> # Apply the transform to data
>>> result = transform(
...     image=image,
...     mask=mask,
...     bboxes=bboxes,
...     bbox_labels=bbox_labels,
...     keypoints=keypoints,
...     keypoint_labels=keypoint_labels
... )
>>>
>>> # Get the transformed data
>>> transformed_image = result['image']  # Will be 80x80
>>> transformed_mask = result['mask']    # Will be 80x80
>>> transformed_bboxes = result['bboxes']  # Bounding boxes adjusted to the cropped area
>>> transformed_bbox_labels = result['bbox_labels']  # Labels for bboxes that remain after cropping
>>> transformed_keypoints = result['keypoints']  # Keypoints adjusted to the cropped area
>>> transformed_keypoint_labels = result['keypoint_labels']  # Labels for keypoints that remain after cropping

Notes

This class is not meant to be used directly. Instead, use or create derived transforms that implement the specific cropping behavior required.

BaseCropAndPadclass

Base class for transforms that need both cropping and padding. This abstract class extends BaseCrop by adding padding capabilities. It's the foundation for transforms that may need to both crop parts of the input and add padding, such as when converting inputs to a specific target size. The class handles the complexities of applying these operations to different data types (images, masks, bounding boxes, keypoints) while maintaining their spatial relationships. Child classes must implement the `get_params_dependent_on_data` method to determine crop coordinates and padding parameters based on transform-specific logic.

Parameters

NameTypeDefaultDescription
pad_if_neededbool-Whether to pad the input if the crop size exceeds input dimensions.
border_mode
One of:
  • cv2.BORDER_CONSTANT
  • cv2.BORDER_REPLICATE
  • cv2.BORDER_REFLECT
  • cv2.BORDER_WRAP
  • cv2.BORDER_REFLECT_101
-OpenCV border mode used for padding.
fill
One of:
  • tuple[float, ...]
  • float
-Value to fill the padded area if border_mode is BORDER_CONSTANT. For multi-channel images, this can be a tuple with a value for each channel.
fill_mask
One of:
  • tuple[float, ...]
  • float
-Value to fill the padded area in masks.
pad_position
One of:
  • 'center'
  • 'top_left'
  • 'top_right'
  • 'bottom_left'
  • 'bottom_right'
  • 'random'
-Position of padding when pad_if_needed is True.
pfloat-Probability of applying the transform. Default: 1.0.

Examples

>>> import numpy as np
>>> import cv2
>>> import albumentations as A
>>> from albumentations.augmentations.crops.transforms import BaseCropAndPad
>>>
>>> # Example of a custom transform that inherits from BaseCropAndPad
>>> # This transform crops to a fixed size, padding if needed to maintain dimensions
>>> class CustomFixedSizeCrop(BaseCropAndPad):
...     '''A custom fixed-size crop that pads if needed to maintain output size'''
...     def __init__(
...         self,
...         height=224,
...         width=224,
...         offset_x=0,  # Offset for crop position
...         offset_y=0,  # Offset for crop position
...         pad_if_needed=True,
...         border_mode=cv2.BORDER_CONSTANT,
...         fill=0,
...         fill_mask=0,
...         pad_position="center",
...         p=1.0,
...     ):
...         super().__init__(
...             pad_if_needed=pad_if_needed,
...             border_mode=border_mode,
...             fill=fill,
...             fill_mask=fill_mask,
...             pad_position=pad_position,
...             p=p,
...         )
...         self.height = height
...         self.width = width
...         self.offset_x = offset_x
...         self.offset_y = offset_y
...
...     def get_params_dependent_on_data(self, params, data):
...         '''Calculate crop coordinates and padding if needed'''
...         image_shape = params["shape"][:2]
...         image_height, image_width = image_shape
...
...         # Calculate crop coordinates with offsets
...         x_min = self.offset_x
...         y_min = self.offset_y
...         x_max = min(x_min + self.width, image_width)
...         y_max = min(y_min + self.height, image_height)
...
...         # Get padding params if needed
...         pad_params = self._get_pad_params(
...             image_shape,
...             (self.height, self.width)
...         ) if self.pad_if_needed else None
...
...         return {
...             "crop_coords": (x_min, y_min, x_max, y_max),
...             "pad_params": pad_params,
...         }
>>>
>>> # 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]
>>>
>>> # Use the custom transform in a pipeline
>>> # This will create a 224x224 crop with padding as needed
>>> transform = A.Compose(
...     [CustomFixedSizeCrop(
...         height=224,
...         width=224,
...         offset_x=20,
...         offset_y=10,
...         fill=127,  # Gray color for padding
...         fill_mask=0
...     )],
...     bbox_params=A.BboxParams(format='pascal_voc', label_fields=['bbox_labels']),
...     keypoint_params=A.KeypointParams(format='xy', label_fields=['keypoint_labels']))
>>>
>>> # Apply the transform to data
>>> result = transform(
...     image=image,
...     mask=mask,
...     bboxes=bboxes,
...     bbox_labels=bbox_labels,
...     keypoints=keypoints,
...     keypoint_labels=keypoint_labels
... )
>>>
>>> # Get the transformed data
>>> transformed_image = result['image']  # Will be 224x224 with padding
>>> transformed_mask = result['mask']    # Will be 224x224 with padding
>>> transformed_bboxes = result['bboxes']  # Bounding boxes adjusted to the cropped and padded area
>>> transformed_bbox_labels = result['bbox_labels']  # Bounding box labels after crop
>>> transformed_keypoints = result['keypoints']  # Keypoints adjusted to the cropped and padded area
>>> transformed_keypoint_labels = result['keypoint_labels']  # Keypoint labels after crop

Notes

This class is not meant to be used directly. Instead, use or create derived transforms that implement the specific cropping and padding behavior required.

BaseRandomSizedCropInitSchemaclass

Parameters

NameTypeDefaultDescription
pAnnotated--
strictbool--
sizeAnnotated--

CenterCropclass

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.

Parameters

NameTypeDefaultDescription
heightint-The height of the crop. Must be greater than 0.
widthint-The width of the crop. Must be greater than 0.
pad_if_neededboolFalseWhether to pad if crop size exceeds image size. Default: False.
pad_position
One of:
  • 'center'
  • 'top_left'
  • 'top_right'
  • 'bottom_left'
  • 'bottom_right'
  • 'random'
centerPosition of padding. Default: 'center'.
border_mode
One of:
  • cv2.BORDER_CONSTANT
  • cv2.BORDER_REPLICATE
  • cv2.BORDER_REFLECT
  • cv2.BORDER_WRAP
  • cv2.BORDER_REFLECT_101
0OpenCV border mode used for padding. Default: cv2.BORDER_CONSTANT.
fill
One of:
  • tuple[float, ...]
  • float
0.0Padding value for images if border_mode is cv2.BORDER_CONSTANT. Default: 0.
fill_mask
One of:
  • tuple[float, ...]
  • float
0.0Padding value for masks if border_mode is cv2.BORDER_CONSTANT. Default: 0.
pfloat1.0Probability 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(format='pascal_voc', label_fields=['bbox_labels']),
...    keypoint_params=A.KeypointParams(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(format='pascal_voc', label_fields=['bbox_labels']),
...    keypoint_params=A.KeypointParams(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.

Cropclass

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.

Parameters

NameTypeDefaultDescription
x_minint0Minimum x-coordinate of the crop region (left edge). Must be >= 0. Default: 0.
y_minint0Minimum y-coordinate of the crop region (top edge). Must be >= 0. Default: 0.
x_maxint1024Maximum x-coordinate of the crop region (right edge). Must be > x_min. Default: 1024.
y_maxint1024Maximum y-coordinate of the crop region (bottom edge). Must be > y_min. Default: 1024.
pad_if_neededboolFalseWhether to pad if crop coordinates exceed image dimensions. Default: False.
pad_position
One of:
  • 'center'
  • 'top_left'
  • 'top_right'
  • 'bottom_left'
  • 'bottom_right'
  • 'random'
centerPosition of padding. Default: 'center'.
border_mode
One of:
  • cv2.BORDER_CONSTANT
  • cv2.BORDER_REPLICATE
  • cv2.BORDER_REFLECT
  • cv2.BORDER_WRAP
  • cv2.BORDER_REFLECT_101
0OpenCV border mode used for padding. Default: cv2.BORDER_CONSTANT.
fill
One of:
  • tuple[float, ...]
  • float
0Padding value if border_mode is cv2.BORDER_CONSTANT. Default: 0.
fill_mask
One of:
  • tuple[float, ...]
  • float
0Padding value for masks. Default: 0.
pfloat1.0Probability 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(format='pascal_voc', label_fields=['bbox_labels']),
...    keypoint_params=A.KeypointParams(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(format='pascal_voc', label_fields=['bbox_labels']),
...    keypoint_params=A.KeypointParams(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(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.

CropAndPadclass

Crop and pad images by pixel amounts or fractions of image sizes. This transform allows for simultaneous cropping and padding of images. Cropping removes pixels from the sides (i.e., extracts a subimage), while padding adds pixels to the sides (e.g., black pixels). The amount of cropping/padding can be specified either in absolute pixels or as a fraction of the image size.

Parameters

NameTypeDefaultDescription
px
One of:
  • int
  • list[int]
  • None
NoneThe number of pixels to crop (negative values) or pad (positive values) on each side of the image. Either this or the parameter `percent` may be set, not both at the same time. - If int: crop/pad all sides by this value. - If tuple of 2 ints: crop/pad by (top/bottom, left/right). - If tuple of 4 ints: crop/pad by (top, right, bottom, left). - Each int can also be a tuple of 2 ints for a range, or a list of ints for discrete choices. Default: None.
percent
One of:
  • float
  • list[float]
  • None
NoneThe fraction of the image size to crop (negative values) or pad (positive values) on each side. Either this or the parameter `px` may be set, not both at the same time. - If float: crop/pad all sides by this fraction. - If tuple of 2 floats: crop/pad by (top/bottom, left/right) fractions. - If tuple of 4 floats: crop/pad by (top, right, bottom, left) fractions. - Each float can also be a tuple of 2 floats for a range, or a list of floats for discrete choices. Default: None.
keep_sizeboolTrueIf True, the output image will be resized to the input image size after cropping/padding. Default: True.
sample_independentlyboolTrueIf True and ranges are used for px/percent, sample a value for each side independently. If False, sample one value and use it for all sides. Default: True.
interpolation
One of:
  • cv2.INTER_NEAREST
  • cv2.INTER_NEAREST_EXACT
  • cv2.INTER_LINEAR
  • cv2.INTER_CUBIC
  • cv2.INTER_AREA
  • cv2.INTER_LANCZOS4
  • cv2.INTER_LINEAR_EXACT
1OpenCV interpolation flag used for resizing if keep_size is True. Default: cv2.INTER_LINEAR.
mask_interpolation
One of:
  • cv2.INTER_NEAREST
  • cv2.INTER_NEAREST_EXACT
  • cv2.INTER_LINEAR
  • cv2.INTER_CUBIC
  • cv2.INTER_AREA
  • cv2.INTER_LANCZOS4
  • cv2.INTER_LINEAR_EXACT
0OpenCV interpolation flag used for resizing if keep_size is True. Should be one of: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4. Default: cv2.INTER_NEAREST.
border_mode
One of:
  • cv2.BORDER_CONSTANT
  • cv2.BORDER_REPLICATE
  • cv2.BORDER_REFLECT
  • cv2.BORDER_WRAP
  • cv2.BORDER_REFLECT_101
0OpenCV border mode used for padding. Default: cv2.BORDER_CONSTANT.
fill
One of:
  • tuple[float, ...]
  • float
0The constant value to use for padding if border_mode is cv2.BORDER_CONSTANT. Default: 0.
fill_mask
One of:
  • tuple[float, ...]
  • float
0Same as fill but used for mask padding. Default: 0.
pfloat1.0Probability 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: Using px parameter with specific values for each side
>>> # Crop 10px from top, pad 20px on right, pad 30px on bottom, crop 40px from left
>>> transform_px = A.Compose([
...     A.CropAndPad(
...         px=(-10, 20, 30, -40),  # (top, right, bottom, left)
...         border_mode=cv2.BORDER_CONSTANT,
...         fill=128,  # Gray padding color
...         fill_mask=0,
...         keep_size=False,  # Don't resize back to original dimensions
...         p=1.0
...     ),
... ], bbox_params=A.BboxParams(format='pascal_voc', label_fields=['bbox_labels']),
...    keypoint_params=A.KeypointParams(format='xy', label_fields=['keypoint_labels']))
>>>
>>> # Apply the transform
>>> result_px = transform_px(
...     image=image,
...     mask=mask,
...     bboxes=bboxes,
...     bbox_labels=bbox_labels,
...     keypoints=keypoints,
...     keypoint_labels=keypoint_labels
... )
>>>
>>> # Get the transformed data with px parameters
>>> transformed_image_px = result_px['image']  # Shape will be different from original
>>> transformed_mask_px = result_px['mask']
>>> transformed_bboxes_px = result_px['bboxes']  # Adjusted to new dimensions
>>> transformed_bbox_labels_px = result_px['bbox_labels']  # Bounding box labels after crop
>>> transformed_keypoints_px = result_px['keypoints']  # Adjusted to new dimensions
>>> transformed_keypoint_labels_px = result_px['keypoint_labels']  # Keypoint labels after crop
>>>
>>> # Example 2: Using percent parameter as a single value
>>> # This will pad all sides by 10% of image dimensions
>>> transform_percent = A.Compose([
...     A.CropAndPad(
...         percent=0.1,  # Pad all sides by 10%
...         border_mode=cv2.BORDER_REFLECT,  # Use reflection padding
...         keep_size=True,  # Resize back to original dimensions
...         p=1.0
...     ),
... ], bbox_params=A.BboxParams(format='pascal_voc', label_fields=['bbox_labels']),
...    keypoint_params=A.KeypointParams(format='xy', label_fields=['keypoint_labels']))
>>>
>>> # Apply the transform
>>> result_percent = transform_percent(
...     image=image,
...     mask=mask,
...     bboxes=bboxes,
...     bbox_labels=bbox_labels,
...     keypoints=keypoints,
...     keypoint_labels=keypoint_labels
... )
>>>
>>> # Get the transformed data with percent parameters
>>> # Since keep_size=True, image dimensions remain the same (100x100)
>>> transformed_image_pct = result_percent['image']
>>> transformed_mask_pct = result_percent['mask']
>>> transformed_bboxes_pct = result_percent['bboxes']
>>> transformed_bbox_labels_pct = result_percent['bbox_labels']
>>> transformed_keypoints_pct = result_percent['keypoints']
>>> transformed_keypoint_labels_pct = result_percent['keypoint_labels']
>>>
>>> # Example 3: Random padding within a range
>>> # Pad top and bottom by 5-15%, left and right by 10-20%
>>> transform_random = A.Compose([
...     A.CropAndPad(
...         percent=[(0.05, 0.15), (0.1, 0.2), (0.05, 0.15), (0.1, 0.2)],  # (top, right, bottom, left)
...         sample_independently=True,  # Sample each side independently
...         border_mode=cv2.BORDER_CONSTANT,
...         fill=0,  # Black padding
...         keep_size=False,
...         p=1.0
...     ),
... ], bbox_params=A.BboxParams(format='pascal_voc', label_fields=['bbox_labels']),
...    keypoint_params=A.KeypointParams(format='xy', label_fields=['keypoint_labels']))
>>>
>>> # Result dimensions will vary based on the random padding values chosen

Notes

- This transform will never crop images below a height or width of 1. - When using pixel values (px), the image will be cropped/padded by exactly that many pixels. - When using percentages (percent), the amount of crop/pad will be calculated based on the image size. - Bounding boxes that end up fully outside the image after cropping will be removed. - Keypoints that end up outside the image after cropping will be removed.

CropNonEmptyMaskIfExistsclass

Crop area with mask if mask is non-empty, else make random crop. This transform attempts to crop a region containing a mask (non-zero pixels). If the mask is empty or not provided, it falls back to a random crop. This is particularly useful for segmentation tasks where you want to focus on regions of interest defined by the mask.

Parameters

NameTypeDefaultDescription
heightint-Vertical size of crop in pixels. Must be > 0.
widthint-Horizontal size of crop in pixels. Must be > 0.
ignore_values
One of:
  • list[int]
  • None
NoneValues to ignore in mask, `0` values are always ignored. For example, if background value is 5, set `ignore_values=[5]` to ignore it. Default: None.
ignore_channels
One of:
  • list[int]
  • None
NoneChannels to ignore in mask. For example, if background is the first channel, set `ignore_channels=[0]` to ignore it. Default: None.
pfloat1.0Probability of applying the transform. Default: 1.0.

Examples

>>> import numpy as np
>>> import albumentations as A
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> mask = np.zeros((100, 100), dtype=np.uint8)
>>> mask[25:75, 25:75] = 1  # Create a non-empty region in the mask
>>> transform = A.Compose([
...     A.CropNonEmptyMaskIfExists(height=50, width=50, p=1.0),
... ])
>>> transformed = transform(image=image, mask=mask)
>>> transformed_image = transformed['image']
>>> transformed_mask = transformed['mask']
# The resulting crop will likely include part of the non-zero region in the mask

Notes

- If a mask is provided, the transform will try to crop an area containing non-zero (or non-ignored) pixels. - If no suitable area is found in the mask or no mask is provided, it will perform a random crop. - The crop size (height, width) must not exceed the original image dimensions. - Bounding boxes and keypoints are also cropped along with the image and mask.

CropSizeErrorclass

RandomCropclass

Crop a random part of the input.

Parameters

NameTypeDefaultDescription
heightint-height of the crop.
widthint-width of the crop.
pad_if_neededboolFalseWhether to pad if crop size exceeds image size. Default: False.
pad_position
One of:
  • 'center'
  • 'top_left'
  • 'top_right'
  • 'bottom_left'
  • 'bottom_right'
  • 'random'
centerPosition of padding. Default: 'center'.
border_mode
One of:
  • cv2.BORDER_CONSTANT
  • cv2.BORDER_REPLICATE
  • cv2.BORDER_REFLECT
  • cv2.BORDER_WRAP
  • cv2.BORDER_REFLECT_101
0OpenCV border mode used for padding. Default: cv2.BORDER_CONSTANT.
fill
One of:
  • tuple[float, ...]
  • float
0.0Padding value for images if border_mode is cv2.BORDER_CONSTANT. Default: 0.
fill_mask
One of:
  • tuple[float, ...]
  • float
0.0Padding value for masks if border_mode is cv2.BORDER_CONSTANT. Default: 0.
pfloat1.0Probability 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 random crop
>>> transform = A.Compose([
...     A.RandomCrop(height=64, width=64),
... ], bbox_params=A.BboxParams(format='pascal_voc', label_fields=['bbox_labels']),
...    keypoint_params=A.KeypointParams(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: Random crop with padding when needed
>>> # This is useful when you want to crop to a size larger than some images
>>> transform_padded = A.Compose([
...     A.RandomCrop(
...         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(format='pascal_voc', label_fields=['bbox_labels']),
...    keypoint_params=A.KeypointParams(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

Notes

If pad_if_needed is True and crop size exceeds image dimensions, the image will be padded before applying the random crop.

RandomCropFromBordersclass

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.

Parameters

NameTypeDefaultDescription
crop_leftfloat0.1The maximum fraction of width to crop from the left side. Must be in the range [0.0, 1.0]. Default: 0.1
crop_rightfloat0.1The maximum fraction of width to crop from the right side. Must be in the range [0.0, 1.0]. Default: 0.1
crop_topfloat0.1The maximum fraction of height to crop from the top. Must be in the range [0.0, 1.0]. Default: 0.1
crop_bottomfloat0.1The maximum fraction of height to crop from the bottom. Must be in the range [0.0, 1.0]. Default: 0.1
pfloat1.0Probability 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(format='pascal_voc', label_fields=['bbox_labels']),
...    keypoint_params=A.KeypointParams(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.

RandomCropNearBBoxclass

Crop bbox from image with random shift by x,y coordinates

Parameters

NameTypeDefaultDescription
max_part_shift
One of:
  • tuple[float, float]
  • float
(0, 0.3)Max shift in `height` and `width` dimensions relative to `cropping_bbox` dimension. If max_part_shift is a single float, the range will be (0, max_part_shift). Default (0, 0.3).
cropping_bbox_keystrcropping_bboxAdditional target key for cropping box. Default `cropping_bbox`.
pfloat1.0probability of applying the transform. Default: 1.

Examples

>>> aug = Compose([RandomCropNearBBox(max_part_shift=(0.1, 0.5), cropping_bbox_key='test_bbox')],
>>>              bbox_params=BboxParams("pascal_voc"))
>>> result = aug(image=image, bboxes=bboxes, test_bbox=[0, 5, 10, 20])

RandomResizedCropclass

Crop a random part of the input and rescale it to a specified size. This transform first crops a random portion of the input image (or mask, bounding boxes, keypoints) and then resizes the crop to a specified size. It's particularly useful for training neural networks on images of varying sizes and aspect ratios.

Parameters

NameTypeDefaultDescription
sizetuple[int, int]-Target size for the output image, i.e. (height, width) after crop and resize.
scaletuple[float, float](0.08, 1.0)Range of the random size of the crop relative to the input size. For example, (0.08, 1.0) means the crop size will be between 8% and 100% of the input size. Default: (0.08, 1.0)
ratiotuple[float, float](0.75, 1.3333333333333333)Range of aspect ratios of the random crop. For example, (0.75, 1.3333) allows crop aspect ratios from 3:4 to 4:3. Default: (0.75, 1.3333333333333333)
interpolation
One of:
  • cv2.INTER_NEAREST
  • cv2.INTER_NEAREST_EXACT
  • cv2.INTER_LINEAR
  • cv2.INTER_CUBIC
  • cv2.INTER_AREA
  • cv2.INTER_LANCZOS4
  • cv2.INTER_LINEAR_EXACT
1Flag that is used to specify the interpolation algorithm. Should be one of: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4. Default: cv2.INTER_LINEAR
mask_interpolation
One of:
  • cv2.INTER_NEAREST
  • cv2.INTER_NEAREST_EXACT
  • cv2.INTER_LINEAR
  • cv2.INTER_CUBIC
  • cv2.INTER_AREA
  • cv2.INTER_LANCZOS4
  • cv2.INTER_LINEAR_EXACT
0Flag that is used to specify the interpolation algorithm for mask. Should be one of: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4. Default: cv2.INTER_NEAREST
area_for_downscale
One of:
  • None
  • 'image'
  • 'image_mask'
NoneControls automatic use of INTER_AREA interpolation for downscaling. Options: - None: No automatic interpolation selection, always use the specified interpolation method - "image": Use INTER_AREA when downscaling images, retain specified interpolation for upscaling and masks - "image_mask": Use INTER_AREA when downscaling both images and masks Default: None.
pfloat1.0Probability 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]
>>>
>>> # Define transform with parameters as tuples
>>> transform = A.Compose([
...     A.RandomResizedCrop(
...         size=(64, 64),
...         scale=(0.5, 0.9),  # Crop size will be 50-90% of original image
...         ratio=(0.75, 1.33),  # Aspect ratio will vary from 3:4 to 4:3
...         interpolation=cv2.INTER_LINEAR,
...         mask_interpolation=cv2.INTER_NEAREST,
...         area_for_downscale="image",  # Use INTER_AREA for image downscaling
...         p=1.0
...     ),
... ], bbox_params=A.BboxParams(format='pascal_voc', label_fields=['bbox_labels']),
...    keypoint_params=A.KeypointParams(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']       # Shape: (64, 64, 3)
>>> transformed_mask = transformed['mask']         # Shape: (64, 64)
>>> transformed_bboxes = transformed['bboxes']     # Bounding boxes adjusted to new crop and size
>>> transformed_bbox_labels = transformed['bbox_labels']  # Labels for the preserved bboxes
>>> transformed_keypoints = transformed['keypoints']      # Keypoints adjusted to new crop and size
>>> transformed_keypoint_labels = transformed['keypoint_labels']  # Labels for the preserved keypoints

Notes

- This transform attempts to crop a random area with an aspect ratio and relative size specified by 'ratio' and 'scale' parameters. If it fails to find a suitable crop after 10 attempts, it will return a crop from the center of the image. - The crop's aspect ratio is defined as width / height. - Bounding boxes that end up fully outside the cropped area will be removed. - Keypoints that end up outside the cropped area will be removed. - After cropping, the result is resized to the specified size. - When area_for_downscale is set, INTER_AREA interpolation will be used automatically for downscaling (when the crop is larger than the target size), which provides better quality for size reduction.

RandomSizedBBoxSafeCropclass

Crop a random part of the input and rescale it to a specific size without loss of bounding boxes. This transform first attempts to crop a random portion of the input image while ensuring that all bounding boxes remain within the cropped area. It then resizes the crop to the specified size. This is particularly useful for object detection tasks where preserving all objects in the image is crucial while also standardizing the image size.

Parameters

NameTypeDefaultDescription
heightint-Height of the output image after resizing.
widthint-Width of the output image after resizing.
erosion_ratefloat0.0A value between 0.0 and 1.0 that determines the minimum allowable size of the crop as a fraction of the original image size. For example, an erosion_rate of 0.2 means the crop will be at least 80% of the original image height and width. Default: 0.0 (no minimum size).
interpolation
One of:
  • cv2.INTER_NEAREST
  • cv2.INTER_NEAREST_EXACT
  • cv2.INTER_LINEAR
  • cv2.INTER_CUBIC
  • cv2.INTER_AREA
  • cv2.INTER_LANCZOS4
  • cv2.INTER_LINEAR_EXACT
1Flag that is used to specify the interpolation algorithm. Should be one of: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.AREA, cv2.INTER_LANCZOS4. Default: cv2.INTER_LINEAR.
mask_interpolation
One of:
  • cv2.INTER_NEAREST
  • cv2.INTER_NEAREST_EXACT
  • cv2.INTER_LINEAR
  • cv2.INTER_CUBIC
  • cv2.INTER_AREA
  • cv2.INTER_LANCZOS4
  • cv2.INTER_LINEAR_EXACT
0Flag that is used to specify the interpolation algorithm for mask. Should be one of: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.AREA, cv2.INTER_LANCZOS4. Default: cv2.INTER_NEAREST.
pfloat1.0Probability 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, (300, 300, 3), dtype=np.uint8)
>>> mask = np.random.randint(0, 2, (300, 300), dtype=np.uint8)
>>>
>>> # Create bounding boxes with some overlap and separation
>>> bboxes = np.array([
...     [10, 10, 80, 80],    # top-left box
...     [100, 100, 200, 200], # center box
...     [210, 210, 290, 290]  # bottom-right box
... ], dtype=np.float32)
>>> bbox_labels = ['cat', 'dog', 'bird']
>>>
>>> # Create keypoints inside the bounding boxes
>>> keypoints = np.array([
...     [45, 45],    # inside first box
...     [150, 150],  # inside second box
...     [250, 250]   # inside third box
... ], dtype=np.float32)
>>> keypoint_labels = ['nose', 'eye', 'tail']
>>>
>>> # Example 1: Basic usage with default parameters
>>> transform_basic = A.Compose([
...     A.RandomSizedBBoxSafeCrop(height=224, width=224, p=1.0),
... ], bbox_params=A.BboxParams(
...     format='pascal_voc',
...     label_fields=['bbox_labels']
... ), keypoint_params=A.KeypointParams(
...     format='xy',
...     label_fields=['keypoint_labels']
... ))
>>>
>>> # Apply the transform
>>> result_basic = transform_basic(
...     image=image,
...     mask=mask,
...     bboxes=bboxes,
...     bbox_labels=bbox_labels,
...     keypoints=keypoints,
...     keypoint_labels=keypoint_labels
... )
>>>
>>> # Access the transformed data
>>> transformed_image = result_basic['image']  # Shape will be (224, 224, 3)
>>> transformed_mask = result_basic['mask']    # Shape will be (224, 224)
>>> transformed_bboxes = result_basic['bboxes']  # All original bounding boxes preserved
>>> transformed_bbox_labels = result_basic['bbox_labels']  # Original labels preserved
>>> transformed_keypoints = result_basic['keypoints']  # Keypoints adjusted to new coordinates
>>> transformed_keypoint_labels = result_basic['keypoint_labels']  # Original labels preserved
>>>
>>> # Example 2: With erosion_rate for more flexibility in crop placement
>>> transform_erosion = A.Compose([
...     A.RandomSizedBBoxSafeCrop(
...         height=256,
...         width=256,
...         erosion_rate=0.2,  # Allows 20% flexibility in crop placement
...         interpolation=cv2.INTER_CUBIC,  # Higher quality interpolation
...         mask_interpolation=cv2.INTER_NEAREST,  # Preserve mask edges
...         p=1.0
...     ),
... ], bbox_params=A.BboxParams(
...     format='pascal_voc',
...     label_fields=['bbox_labels'],
...     min_visibility=0.3  # Only keep bboxes with at least 30% visibility
... ), keypoint_params=A.KeypointParams(
...     format='xy',
...     label_fields=['keypoint_labels'],
...     remove_invisible=True  # Remove keypoints outside the crop
... ))
>>>
>>> # Apply the transform with erosion
>>> result_erosion = transform_erosion(
...     image=image,
...     mask=mask,
...     bboxes=bboxes,
...     bbox_labels=bbox_labels,
...     keypoints=keypoints,
...     keypoint_labels=keypoint_labels
... )
>>>
>>> # With erosion_rate=0.2, the crop has more flexibility in placement
>>> # while still ensuring all bounding boxes are included

Notes

- This transform ensures that all bounding boxes in the original image are fully contained within the cropped area. If it's not possible to find such a crop (e.g., when bounding boxes are too spread out), it will default to cropping the entire image. - After cropping, the result is resized to the specified (height, width) size. - Bounding box coordinates are adjusted to match the new image size. - Keypoints are moved along with the crop and scaled to the new image size. - If there are no bounding boxes in the image, it will fall back to a random crop.

RandomSizedCropclass

Crop a random part of the input and rescale it to a specific size. This transform first crops a random portion of the input and then resizes it to a specified size. The size of the random crop is controlled by the 'min_max_height' parameter.

Parameters

NameTypeDefaultDescription
min_max_heighttuple[int, int]-Minimum and maximum height of the crop in pixels.
sizetuple[int, int]-Target size for the output image, i.e. (height, width) after crop and resize.
w2h_ratiofloat1.0Aspect ratio (width/height) of crop. Default: 1.0
interpolation
One of:
  • cv2.INTER_NEAREST
  • cv2.INTER_NEAREST_EXACT
  • cv2.INTER_LINEAR
  • cv2.INTER_CUBIC
  • cv2.INTER_AREA
  • cv2.INTER_LANCZOS4
  • cv2.INTER_LINEAR_EXACT
1Flag that is used to specify the interpolation algorithm. Should be one of: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4. Default: cv2.INTER_LINEAR.
mask_interpolation
One of:
  • cv2.INTER_NEAREST
  • cv2.INTER_NEAREST_EXACT
  • cv2.INTER_LINEAR
  • cv2.INTER_CUBIC
  • cv2.INTER_AREA
  • cv2.INTER_LANCZOS4
  • cv2.INTER_LINEAR_EXACT
0Flag that is used to specify the interpolation algorithm for mask. Should be one of: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4. Default: cv2.INTER_NEAREST.
area_for_downscale
One of:
  • None
  • 'image'
  • 'image_mask'
NoneControls automatic use of INTER_AREA interpolation for downscaling. Options: - None: No automatic interpolation selection, always use the specified interpolation method - "image": Use INTER_AREA when downscaling images, retain specified interpolation for upscaling and masks - "image_mask": Use INTER_AREA when downscaling both images and masks Default: None.
pfloat1.0Probability 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]
>>>
>>> # Define transform with parameters as tuples
>>> transform = A.Compose([
...     A.RandomSizedCrop(
...         min_max_height=(50, 80),
...         size=(64, 64),
...         w2h_ratio=1.0,
...         interpolation=cv2.INTER_LINEAR,
...         mask_interpolation=cv2.INTER_NEAREST,
...         area_for_downscale="image",  # Use INTER_AREA for image downscaling
...         p=1.0
...     ),
... ], bbox_params=A.BboxParams(format='pascal_voc', label_fields=['bbox_labels']),
...    keypoint_params=A.KeypointParams(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']       # Shape: (64, 64, 3)
>>> transformed_mask = transformed['mask']         # Shape: (64, 64)
>>> transformed_bboxes = transformed['bboxes']     # Bounding boxes adjusted to new crop and size
>>> transformed_bbox_labels = transformed['bbox_labels']  # Labels for the preserved bboxes
>>> transformed_keypoints = transformed['keypoints']      # Keypoints adjusted to new crop and size
>>> transformed_keypoint_labels = transformed['keypoint_labels']  # Labels for the preserved keypoints

Notes

- The crop size is randomly selected for each execution within the range specified by 'min_max_height'. - The aspect ratio of the crop is determined by the 'w2h_ratio' parameter. - After cropping, the result is resized to the specified 'size'. - Bounding boxes that end up fully outside the cropped area will be removed. - Keypoints that end up outside the cropped area will be removed. - This transform differs from RandomResizedCrop in that it allows more control over the crop size through the 'min_max_height' parameter, rather than using a scale parameter. - When area_for_downscale is set, INTER_AREA interpolation will be used automatically for downscaling (when the crop is larger than the target size), which provides better quality for size reduction.