Resize

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

Resize the input to the given height and width.

Arguments
height
int

desired height of the output.

width
int

desired width of the output.

interpolation
0 | 6 | 1 | 2 | 3 | 4 | 5
1

flag 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
0 | 6 | 1 | 2 | 3 | 4 | 5
0

flag 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
image | image_mask |

Controls 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.
p
float
1

probability of applying the transform. Default: 1.

Examples
>>> import numpy as np
>>> import albumentations as A
>>> import cv2
>>>
>>> # Create sample data for demonstration
>>> image = np.zeros((100, 100, 3), dtype=np.uint8)
>>> # Add some shapes to visualize resize effects
>>> cv2.rectangle(image, (25, 25), (75, 75), (255, 0, 0), -1)  # Red square
>>> cv2.circle(image, (50, 50), 10, (0, 255, 0), -1)  # Green circle
>>>
>>> # Create a mask for segmentation
>>> mask = np.zeros((100, 100), dtype=np.uint8)
>>> mask[25:75, 25:75] = 1  # Mask covering the red square
>>>
>>> # Create bounding boxes and keypoints
>>> bboxes = np.array([[25, 25, 75, 75]])  # Box around the red square
>>> bbox_labels = [1]
>>> keypoints = np.array([[50, 50]])  # Center of circle
>>> keypoint_labels = [0]
>>>
>>> # Resize all data to 224x224 (common input size for many CNNs)
>>> transform = A.Compose([
...     A.Resize(
...         height=224,
...         width=224,
...         interpolation=cv2.INTER_LINEAR,
...         mask_interpolation=cv2.INTER_NEAREST,
...         area_for_downscale="image",  # Use INTER_AREA when downscaling images
...         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 the transform to all targets
>>> result = transform(
...     image=image,
...     mask=mask,
...     bboxes=bboxes,
...     bbox_labels=bbox_labels,
...     keypoints=keypoints,
...     keypoint_labels=keypoint_labels
... )
>>>
>>> # Get the transformed results
>>> resized_image = result['image']        # Shape will be (224, 224, 3)
>>> resized_mask = result['mask']          # Shape will be (224, 224)
>>> resized_bboxes = result['bboxes']      # Bounding boxes scaled to new dimensions
>>> resized_bbox_labels = result['bbox_labels']  # Labels remain unchanged
>>> resized_keypoints = result['keypoints']      # Keypoints scaled to new dimensions
>>> resized_keypoint_labels = result['keypoint_labels']  # Labels remain unchanged
>>>
>>> # Note: When resizing from 100x100 to 224x224:
>>> # - The red square will be scaled from (25-75) to approximately (56-168)
>>> # - The keypoint at (50, 50) will move to approximately (112, 112)
>>> # - All spatial relationships are preserved but coordinates are scaled