SmallestMaxSize

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

Rescale an image so that minimum side is equal to max_size or sides meet max_size_hw constraints, keeping the aspect ratio.

Arguments
max_size
int | Sequence | None

Maximum size of smallest side of the image after the transformation. When using a list, max size will be randomly selected from the values in the list. Default: None.

max_size_hw
tuple[int | None, int | None] | None

Maximum (height, width) constraints. Supports:

  • (height, width): Both dimensions must be at least these values
  • (height, None): Only height is constrained, width scales proportionally
  • (None, width): Only width is constrained, height scales proportionally If specified, max_size must be None. Default: None.
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
>>> # Using max_size
>>> transform1 = A.SmallestMaxSize(max_size=120, area_for_downscale="image")
>>> # Input image (100, 150) -> Output (120, 180)
>>>
>>> # Using max_size_hw with both dimensions
>>> transform2 = A.SmallestMaxSize(max_size_hw=(100, 200), area_for_downscale="image_mask")
>>> # Input (80, 160) -> Output (100, 200)
>>> # Input (160, 80) -> Output (400, 200)
>>>
>>> # Using max_size_hw with only height
>>> transform3 = A.SmallestMaxSize(max_size_hw=(100, None))
>>> # Input (80, 160) -> Output (100, 200)
Notes
  • This transform scales images based on their smallest side:
    • If the smallest side is smaller than max_size: the image will be upscaled (scale > 1.0)
    • If the smallest side is equal to max_size: the image will not be resized (scale = 1.0)
    • If the smallest side is larger than max_size: the image will be downscaled (scale < 1.0)
  • This transform will not crop the image. The resulting image may be larger than specified in both dimensions.
  • For non-square images, both sides will be scaled proportionally to maintain the aspect ratio.
  • Bounding boxes and keypoints are scaled accordingly.
  • When area_for_downscale is set, INTER_AREA will be used for downscaling, providing better quality.