Center crops the image inside a dataset item. For detection items, bounding box coordinates are adjusted to remain correct after cropping. For segmentation items, both the image and the masks are center cropped. If the image is smaller than the requested crop size along any edge, the image is padded with zeros and then center cropped.

item_transform_center_crop(x, size)

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.

size

Desired output size. If size is an integer vector of length 2 like c(h, w), the output will be matched to this. If size is a bare integer, a square crop of c(size, size) is made.

Value

A dataset item of the same class with the image and target center cropped.

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"))
class(before) <- c("image_with_bounding_box", "list")

after <- item_transform_center_crop(before, size = c(1500, 1500))

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

before_plot <- transform_resize(before_plot, c(1500, 1500))
after_plot <- transform_resize(after_plot, c(1500, 1500))

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