albumentations.augmentations.geometric.rotate


Transforms for rotating images and associated data. This module provides classes for rotating images, masks, bounding boxes, and keypoints. Includes transforms for 90-degree rotations and arbitrary angle rotations with various border handling options.

RandomRotate90class

RandomRotate90(
    p: float = 1
)

Randomly rotate the input by 90 degrees zero or more times. Even with p=1.0, the transform has a 1/4 probability of being identity: - With probability p * 1/4: no rotation (0 degrees) - With probability p * 1/4: rotate 90 degrees - With probability p * 1/4: rotate 180 degrees - With probability p * 1/4: rotate 270 degrees For example: - With p=1.0: Each rotation angle (including 0°) has 0.25 probability - With p=0.8: Each rotation angle has 0.2 probability, and no transform has 0.2 probability - With p=0.5: Each rotation angle has 0.125 probability, and no transform has 0.5 probability Common applications: - Aerial/satellite imagery: Objects can appear in any orientation - Medical imaging: Scans/slides may not have a consistent orientation - Document analysis: Pages or symbols might be rotated - Microscopy: Cell orientation is often arbitrary - Game development: Sprites/textures that should work in multiple orientations Not recommended for: - Natural scene images where gravity matters (e.g., landscape photography) - Face detection/recognition tasks - Text recognition (unless text can appear rotated) - Tasks where object orientation is important for classification Note: If your domain has both 90-degree rotation AND flip symmetries (e.g., satellite imagery, microscopy), consider using `D4` transform instead. `D4` is more efficient and mathematically correct as it: - Samples uniformly from all 8 possible combinations of rotations and flips - Properly represents the dihedral group D4 symmetries - Avoids potential correlation between separate rotation and flip augmentations

Parameters

NameTypeDefaultDescription
pfloat1probability of applying the transform. Default: 1.0. Note that even with p=1.0, there's still a 0.25 probability of getting a 0-degree rotation (identity transform).

Notes

If your domain has both 90-degree rotation AND flip symmetries (e.g., satellite imagery, microscopy), consider using `D4` transform instead. `D4` is more efficient and mathematically correct as it: - Samples uniformly from all 8 possible combinations of rotations and flips - Properly represents the dihedral group D4 symmetries - Avoids potential correlation between separate rotation and flip augmentations

Rotateclass

Rotate(
    limit: tuple[float, float] | float = (-90, 90),
    interpolation: Literal[cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4] = 1,
    border_mode: Literal[cv2.BORDER_CONSTANT, cv2.BORDER_REPLICATE, cv2.BORDER_REFLECT, cv2.BORDER_WRAP, cv2.BORDER_REFLECT_101] = 0,
    rotate_method: Literal['largest_box', 'ellipse'] = largest_box,
    crop_border: bool = False,
    mask_interpolation: Literal[cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4] = 0,
    fill: tuple[float, ...] | float = 0,
    fill_mask: tuple[float, ...] | float = 0,
    p: float = 0.5
)

Rotate the input by an angle selected randomly from the uniform distribution.

Parameters

NameTypeDefaultDescription
limit
One of:
  • tuple[float, float]
  • float
(-90, 90)Range from which a random angle is picked. If limit is a single float, an angle is picked from (-limit, limit). Default: (-90, 90)
interpolation
One of:
  • cv2.INTER_NEAREST
  • cv2.INTER_LINEAR
  • cv2.INTER_CUBIC
  • cv2.INTER_AREA
  • cv2.INTER_LANCZOS4
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.
border_mode
One of:
  • cv2.BORDER_CONSTANT
  • cv2.BORDER_REPLICATE
  • cv2.BORDER_REFLECT
  • cv2.BORDER_WRAP
  • cv2.BORDER_REFLECT_101
0Flag that is used to specify the pixel extrapolation method. Should be one of: cv2.BORDER_CONSTANT, cv2.BORDER_REPLICATE, cv2.BORDER_REFLECT, cv2.BORDER_WRAP, cv2.BORDER_REFLECT_101. Default: cv2.BORDER_CONSTANT
rotate_method
One of:
  • 'largest_box'
  • 'ellipse'
largest_boxMethod to rotate bounding boxes. Should be 'largest_box' or 'ellipse'. Default: 'largest_box'
crop_borderboolFalseWhether to crop border after rotation. If True, the output image size might differ from the input. Default: False
mask_interpolation
One of:
  • cv2.INTER_NEAREST
  • cv2.INTER_LINEAR
  • cv2.INTER_CUBIC
  • cv2.INTER_AREA
  • cv2.INTER_LANCZOS4
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.
fill
One of:
  • tuple[float, ...]
  • float
0Padding value if border_mode is cv2.BORDER_CONSTANT.
fill_mask
One of:
  • tuple[float, ...]
  • float
0Padding value if border_mode is cv2.BORDER_CONSTANT applied for masks.
pfloat0.5Probability of applying the transform. Default: 0.5.

Example

>>> import numpy as np
>>> import albumentations as A
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> transform = A.Rotate(limit=45, p=1.0)
>>> result = transform(image=image)
>>> rotated_image = result['image']
# rotated_image will be the input image rotated by a random angle between -45 and 45 degrees

Notes

- The rotation angle is randomly selected for each execution within the range specified by 'limit'. - When 'crop_border' is False, the output image will have the same size as the input, potentially introducing black triangles in the corners. - When 'crop_border' is True, the output image is cropped to remove black triangles, which may result in a smaller image. - Bounding boxes are rotated and may change size or shape. - Keypoints are rotated around the center of the image.

RotateInitSchemaclass

RotateInitSchema(
    p: Annotated,
    strict: bool = False,
    limit: Annotated,
    interpolation: Literal,
    mask_interpolation: Literal,
    border_mode: Literal,
    fill: tuple[float, ...] | float,
    fill_mask: tuple[float, ...] | float | None
)

Parameters

NameTypeDefaultDescription
pAnnotated--
strictboolFalse-
limitAnnotated--
interpolationLiteral--
mask_interpolationLiteral--
border_modeLiteral--
fill
One of:
  • tuple[float, ...]
  • float
--
fill_mask
One of:
  • tuple[float, ...]
  • float
  • None
--

SafeRotateclass

SafeRotate(
    limit: tuple[float, float] | float = (-90, 90),
    interpolation: Literal[cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4] = 1,
    border_mode: Literal[cv2.BORDER_CONSTANT, cv2.BORDER_REPLICATE, cv2.BORDER_REFLECT, cv2.BORDER_WRAP, cv2.BORDER_REFLECT_101] = 0,
    rotate_method: Literal['largest_box', 'ellipse'] = largest_box,
    mask_interpolation: Literal[cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4] = 0,
    fill: tuple[float, ...] | float = 0,
    fill_mask: tuple[float, ...] | float = 0,
    p: float = 0.5
)

Rotate the input inside the input's frame by an angle selected randomly from the uniform distribution. This transformation ensures that the entire rotated image fits within the original frame by scaling it down if necessary. The resulting image maintains its original dimensions but may contain artifacts due to the rotation and scaling process.

Parameters

NameTypeDefaultDescription
limit
One of:
  • tuple[float, float]
  • float
(-90, 90)Range from which a random angle is picked. If limit is a single float, an angle is picked from (-limit, limit). Default: (-90, 90)
interpolation
One of:
  • cv2.INTER_NEAREST
  • cv2.INTER_LINEAR
  • cv2.INTER_CUBIC
  • cv2.INTER_AREA
  • cv2.INTER_LANCZOS4
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.
border_mode
One of:
  • cv2.BORDER_CONSTANT
  • cv2.BORDER_REPLICATE
  • cv2.BORDER_REFLECT
  • cv2.BORDER_WRAP
  • cv2.BORDER_REFLECT_101
0Flag that is used to specify the pixel extrapolation method. Should be one of: cv2.BORDER_CONSTANT, cv2.BORDER_REPLICATE, cv2.BORDER_REFLECT, cv2.BORDER_WRAP, cv2.BORDER_REFLECT_101. Default: cv2.BORDER_REFLECT_101
rotate_method
One of:
  • 'largest_box'
  • 'ellipse'
largest_boxMethod to rotate bounding boxes. Should be 'largest_box' or 'ellipse'. Default: 'largest_box'
mask_interpolation
One of:
  • cv2.INTER_NEAREST
  • cv2.INTER_LINEAR
  • cv2.INTER_CUBIC
  • cv2.INTER_AREA
  • cv2.INTER_LANCZOS4
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.
fill
One of:
  • tuple[float, ...]
  • float
0Padding value if border_mode is cv2.BORDER_CONSTANT.
fill_mask
One of:
  • tuple[float, ...]
  • float
0Padding value if border_mode is cv2.BORDER_CONSTANT applied for masks.
pfloat0.5Probability of applying the transform. Default: 0.5.

Example

>>> import numpy as np
>>> import albumentations as A
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> transform = A.SafeRotate(limit=45, p=1.0)
>>> result = transform(image=image)
>>> rotated_image = result['image']
# rotated_image will be the input image rotated by a random angle between -45 and 45 degrees,
# scaled to fit within the original 100x100 frame

Notes

- The rotation is performed around the center of the image. - After rotation, the image is scaled to fit within the original frame, which may cause some distortion. - The output image will always have the same dimensions as the input image. - Bounding boxes and keypoints are transformed along with the image.