Applies an affine transformation (rotation, translation, scale and shear) to the image inside a dataset item, keeping the image size unchanged. The target is transformed with the same geometry so that it stays aligned with the image.

item_transform_affine(
  x,
  angle = 0,
  translate = c(0, 0),
  scale = 1,
  shear = 0,
  interpolation = 0,
  fill = NULL,
  center = NULL
)

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.

angle

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

translate

(integer vector of length 2): Horizontal and vertical translations (post-rotation translation).

scale

(numeric): Overall scale.

shear

(numeric vector or numeric): Shear angle value in degrees between -180 to 180, clockwise direction. If a numeric vector is specified, the first value corresponds to a shear parallel to the x-axis, while the second value corresponds to a shear parallel to the y-axis.

interpolation

(integer or character): Interpolation mode. Supported values are 0 / "nearest" and 2 / "bilinear". Default is 0.

fill

Fill color for area outside the transform. Default is NULL.

center

(numeric vector of length 2, optional): Optional center of rotation, c(x, y). Default is image center.

Value

A dataset item with the image and target transformed. Detection items are returned as image_with_rotated_box; segmentation items keep their class.

Details

For detection items, the bounding boxes are transformed via target_transform_affine() so that they follow the image, and the item is returned as an image_with_rotated_box with boxes in xyxyr format. For segmentation items, the masks are transformed 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"
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",
                                  image_height = img$shape[2], image_width = img$shape[3]))
class(before) <- c("image_with_bounding_box", "list")

after <- item_transform_affine(before, angle = 20, translate = c(50, 30),
                               scale = 1.1, shear = 10)

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)
} # }