R/item-transforms-random-geometry.R
item_transform_random_rotation.RdDraws 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.
item_transform_random_rotation(
x,
degrees,
interpolation = 2,
expand = FALSE,
fill = 0
)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.
(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).
(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.
(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.
(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.
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.
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.
if (FALSE) { # \dontrun{
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)
} # }