item_transform_random_rotation: Randomly rotate a dataset item

View source: R/item-transforms-random-geometry.R

item_transform_random_rotationR Documentation

Randomly rotate a dataset item

Description

Draws a random angle inside the given range and rotates the dataset item by that angle with item_transform_rotate. Image and target share the same draw, so that boxes and masks stay aligned with the rotated image.

Usage

item_transform_random_rotation(
  x,
  degrees,
  interpolation = 2,
  expand = FALSE,
  fill = 0
)

Arguments

x

A dataset item, typically an image_with_bounding_box, image_with_rotated_box or image_with_segmentation_mask object containing an image tensor and associated target data.

degrees

(numeric vector of length 2 or numeric): Range of degrees to select from. If degrees is a bare number instead of a numeric vector of length 2 like c(min, max), the range of degrees will be ⁠(-degrees, +degrees)⁠.

interpolation

(integer, optional): Interpolation mode. 0 for nearest, 2 for bilinear. Default is 2 (bilinear), as in item_transform_rotate. Masks are always resampled with nearest so that they keep their discrete values.

expand

(logical, optional): Optional expansion flag. If TRUE, expands the output to make it large enough to hold the entire rotated image. If FALSE or omitted, make the output image the same size as the input image. Note that the expand flag assumes rotation around the center and no translation.

fill

(numeric vector or numeric): Pixel fill value for area outside the rotated image. If a single numeric value, the value is used for all channels. Defaults to 0 for all channels. This option is not supported for Tensor input. Fill value for the area outside the transform in the output image is always 0.

Details

The angle is drawn again for every item, so that a dataset wrapped with this transform yields a different rotation on each access. Unlike item_transform_rotate, the canvas is not expanded by default, so that every item keeps the size it had and a wrapped dataset can still be collated into batches. Pass expand = TRUE to hold the whole rotated image instead.

Value

A dataset item with the image and target rotated. Detection items are returned as image_with_rotated_box with boxes in xyxyr format; segmentation items keep their class.

See Also

Other item_random_transforms: item_transform_random_affine(), item_transform_random_crop(), item_transform_random_erasing(), item_transform_random_horizontal_flip(), item_transform_random_perspective(), item_transform_random_resize_crop(), item_transform_random_vertical_flip()

Examples

## Not run: 
url <- "https://upload.wikimedia.org/wikipedia/commons/b/b6/Felis_catus-cat_on_snow.jpg"
img <- base_loader(url) |> transform_to_tensor()

boxes <- torch_tensor(matrix(c(600, 200, 2880, 1860), ncol = 4), dtype = torch_float32())

before <- list(x = img, y = list(boxes = boxes, labels = "cat"))
class(before) <- c("image_with_bounding_box", "list")

after <- item_transform_random_rotation(before, degrees = 30)

before_plot <- draw_bounding_boxes(before, colors = "blue", width = 10)
after_plot <- draw_bounding_boxes(after, colors = "red", width = 10)
tensor_image_browse(before_plot)
tensor_image_browse(after_plot)

## End(Not run)


torchvision documentation built on Oct. 14, 2026, 1:09 a.m.