← Back to all transforms
SafeRotate
Description
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. Args: limit (float | tuple[float, float]): 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 (OpenCV flag): 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. border_mode (OpenCV flag): Flag 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 value (int, float, list of int, list of float): Padding value if border_mode is cv2.BORDER_CONSTANT. mask_value (int, float, list of int, list of float): Padding value if border_mode is cv2.BORDER_CONSTANT applied for masks. rotate_method (str): Method to rotate bounding boxes. Should be 'largest_box' or 'ellipse'. Default: 'largest_box' mask_interpolation (OpenCV flag): 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. p (float): Probability of applying the transform. Default: 0.5. Targets: image, mask, bboxes, keypoints Image types: uint8, float32 Note: - 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. Mathematical Details: 1. An angle θ is randomly sampled from the range specified by 'limit'. 2. The image is rotated around its center by θ degrees. 3. The rotation matrix R is: R = [cos(θ) -sin(θ)] [sin(θ) cos(θ)] 4. The scaling factor s is calculated to ensure the rotated image fits within the original frame: s = min(width / (width * |cos(θ)| + height * |sin(θ)|), height / (width * |sin(θ)| + height * |cos(θ)|)) 5. The combined transformation matrix T is: T = [s*cos(θ) -s*sin(θ) tx] [s*sin(θ) s*cos(θ) ty] where tx and ty are translation factors to keep the image centered. 6. Each point (x, y) in the image is transformed to (x', y') by: [x'] [s*cos(θ) s*sin(θ)] [x - cx] [cx] [y'] = [-s*sin(θ) s*cos(θ)] [y - cy] + [cy] where (cx, cy) is the center of the image. 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
Parameters
- limit: int | tuple[int, int] | float | tuple[float, float] (default: (-90, 90))
- interpolation: Literal['cv2.INTER_NEAREST', 'cv2.INTER_LINEAR', 'cv2.INTER_CUBIC', 'cv2.INTER_AREA', 'cv2.INTER_LANCZOS4', 'cv2.INTER_BITS', 'cv2.INTER_NEAREST_EXACT', 'cv2.INTER_MAX'] (default: 1)
- border_mode: Literal['cv2.BORDER_CONSTANT', 'cv2.BORDER_REPLICATE', 'cv2.BORDER_REFLECT', 'cv2.BORDER_WRAP', 'cv2.BORDER_DEFAULT', 'cv2.BORDER_TRANSPARENT'] (default: 4)
- value: float | Sequence[float] | None (default: null)
- mask_value: float | Sequence[float] | None (default: null)
- rotate_method: Literal['largest_box', 'ellipse'] (default: 'largest_box')
- mask_interpolation: Literal['cv2.INTER_NEAREST', 'cv2.INTER_LINEAR', 'cv2.INTER_CUBIC', 'cv2.INTER_AREA', 'cv2.INTER_LANCZOS4', 'cv2.INTER_BITS', 'cv2.INTER_NEAREST_EXACT', 'cv2.INTER_MAX'] (default: 0)
- p: float (default: 0.5)
Targets
- Image
- Mask
- BBoxes
- Keypoints
Try it out
ⓘ