Skip to content
class

SafeRotate

extendsRotate
SafeRotate(limit: float | tuple[float, float] = 90, interpolation: int | str | Interpolation = 1, border_mode: int = 4, value: float = 0.0, p: float = 0.5)

Rotate by a random angle, expanding the canvas to keep all content (Albumentations SafeRotate).

Like Rotate but enlarges the output canvas so the full rotated rectangle fits without clipping. The new size is

(W′, H′)=(⌈Wcos⁡θ+Hsin⁡θ⌉,  ⌈Wsin⁡θ+Hcos⁡θ⌉),(W',\, H') = \left(\lceil W\cos\theta + H\sin\theta\rceil,\; \lceil W\sin\theta + H\cos\theta\rceil\right),

with θ\theta sampled per call from limit. The affine matrix is built as "rotate about the old center, then translate into the centre of the new canvas" — so straight rotations (multiples of 90°) preserve pixel positions exactly within their new canvas, and content never leaves the frame.

Parameters

limitfloat or (float, float)= 90
Range from which the rotation angle (degrees, image-space CW) is sampled uniformly. A scalar a is treated as (-a, a).
interpolationint, str, or Interpolation= 1
Resampling mode; accepts the integer / name aliases used by the reference framework (0=nearest, 1=bilinear).
border_modeint= 4
Border handling mode (reference-framework convention). 0 is constant fill with value; other modes mirror / reflect the boundary.
valuefloat= 0.0
Constant fill value used when border_mode == 0.
pfloat= 0.5
Probability of applying the transform.

Examples

>>> import lucid
>>> import lucid.utils.transforms as T
>>> tf = T.SafeRotate(limit=(45, 45), p=1.0)
>>> out = tf(T.Image(lucid.rand(3, 24, 32))).data
>>> out.shape[0]               # channels preserved
3
>>> out.shape[-1] >= 32        # width expanded to fit rotated content
True

Used by 1

Instance methods

1
fn

make_params

→WarpParams
make_params(img: Tensor)

Sample per-call random parameters for SafeRotate.

Parameters

Image tensor; its spatial size is read to compute the expanded output canvas that fully contains the rotated rectangle.

Returns

WarpParams

Carries the composed (3, 3) matrix (rotate about the old centre, then translate into the centre of the new canvas) and out_hw equal to the expanded (new_h, new_w).

Notes

new_w = ceil(W*|cos| + H*|sin|) and new_h = ceil(W*|sin| + H*|cos|) so no rotated content is clipped.

Dunder methods

1