R/item-transforms-random-geometry.R
item_transform_random_perspective.RdApplies a random perspective transformation to the image inside a dataset
item with a given probability. The degree of distortion is controlled by
distortion_scale. When a transform occurs, the same perspective
mapping is applied to bounding boxes and masks so that the targets stay
aligned with the image.
item_transform_random_perspective(
x,
distortion_scale = 0.5,
p = 0.5,
interpolation = 2,
fill = 0
)A dataset item, typically an image_with_bounding_box or
image_with_segmentation_mask object containing an image tensor
and associated target data.
(numeric): Argument to control the degree of
distortion and ranges from 0 to 1. Default is 0.5.
(numeric): Probability of the image being transformed. Default
is 0.5.
(integer, optional): Desired interpolation. An integer
0 = nearest, 2 = bilinear, and 3 = bicubic or a name from
magick::filter_types().
(numeric vector or numeric): Pixel fill value for constant fill.
Default is 0. If a numeric vector of length 3, it is used to fill R, G,
B channels respectively. This value is only used when the padding_mode
is constant. Only integer values are supported for Tensors.
A dataset item of the same class. With probability p, the
image and targets are perspective transformed; otherwise they are
returned unchanged.
The transformation is drawn again for every item, so that a dataset wrapped with this transform yields a different perspective on each access.
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_perspective(before)
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)
} # }