Rotates a dataset item by a given angle around its center. The canvas is expanded so that the entire rotated image is visible with no cropping. Empty regions are filled with given fill color.

item_transform_rotate(x, angle, interpolation = 2, expand = TRUE, fill = 0)

Arguments

x

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

angle

(numeric): Rotation angle value in degrees, counter-clockwise.

interpolation

(integer, optional): Interpolation mode. 0 for nearest, 2 for bilinear. Default is 0 (nearest).

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.

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.

Details

The bounding boxes (if present) are rotated around the original image centre and shifted onto the (possibly expanded) output canvas, while their physical size is left unchanged. The result is always in xyxyr format with the accumulated rotation angle. For segmentation items, the masks are rotated alongside the image using nearest-neighbour sampling.

Examples

if (FALSE) { # \dontrun{
url <- "https://upload.wikimedia.org/wikipedia/commons/b/b6/Felis_catus-cat_on_snow.jpg"
angle <- 30

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_rotate(before, angle = angle)

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

grid <- vision_make_grid(transform_resize(before_plot, c(600, 600)),
                         transform_resize(after_plot, c(600, 600)),
  scale = TRUE
)
tensor_image_browse(grid)
} # }