target_transform_rotate: Convert bounding boxes to rotated format

View source: R/target-transforms-detection.R

target_transform_rotateR Documentation

Convert bounding boxes to rotated format

Description

Converts bounding boxes of a detection target from (x_{min}, y_{min}, x_{max}, y_{max}) (xyxy) format to (x_{min}, y_{min}, x_{max}, y_{max}, r) (xyxyr) format, where r is the rotation angle in degrees (counter-clockwise). For axis-aligned boxes, r = 0.

Usage

target_transform_rotate(target, angle = 0)

## S3 method for class 'object_detection_target'
target_transform_rotate(target, angle = 0)

## S3 method for class 'dataset'
target_transform_rotate(target, angle = 0)

Arguments

target

An object_detection_target, as returned by the .getitem() method of an object detection dataset, or a bare list containing at least:

  • boxes — tensor of shape ⁠(N, 4)⁠ with bounding boxes in xyxy format

  • image_height (optional) — original image height, used for clamping boxes to remain within image bounds

  • image_width (optional) — original image width, used for clamping boxes to remain within image bounds

  • Other fields (labels, etc.) are preserved unchanged

angle

(numeric): Rotation angle in degrees (counter-clockwise). Default is 0.

Value

A list with the same structure as the input target, where boxes is a tensor of shape ⁠(N, 5)⁠ in xyxyr format. When applied to a dataset, returns the same dataset with its .getitem method modified to return rotated-box targets.

See Also

Other target_transforms_detection: prepare_sahi_split(), target_transform_affine(), target_transform_resize()

Examples

## Not run: 
url <- "https://upload.wikimedia.org/wikipedia/commons/6/66/The_Leaning_Tower_of_Pisa_SB.jpeg"
img <- base_loader(url) |> transform_to_tensor()

boxes <- torch_tensor(matrix(c(720, 620, 1900, 3700), ncol = 4), dtype = torch_float32())

# Original boxes (blue, axis-aligned)
before_plot <- draw_bounding_boxes(img, boxes = boxes, colors = "blue", width = 10)

# Transform boxes to xyxyr with 4 degree rotation
target <- list(
  boxes = boxes,
  image_height = img$shape[2],
  image_width = img$shape[3]
)
class(target) <- c("object_detection_target", "list")
rotated_target <- target_transform_rotate(target, angle = 4)

# Rotated boxes (red, drawn as polygons)
after_plot <- draw_bounding_boxes(img, boxes = rotated_target$boxes, colors = "red", width = 10)

tensor_image_browse(vision_make_grid(before_plot, after_plot, scale = TRUE))

## End(Not run)


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