item_transform_random_crop: Randomly crop a dataset item

View source: R/item-transforms-random-geometry.R

item_transform_random_cropR Documentation

Randomly crop a dataset item

Description

Crops the image inside a dataset item at a random location. When a crop occurs, the same coordinate adjustments used by item_transform_crop are applied to bounding boxes and masks so that the targets stay aligned with the cropped image.

Usage

item_transform_random_crop(
  x,
  size,
  padding = NULL,
  pad_if_needed = FALSE,
  fill = 0,
  padding_mode = "constant"
)

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 of the crop. If size is an int instead of a sequence like c(h, w), a square crop c(size, size) is made. If a sequence of length 1 is provided it is interpreted as c(size[1], size[1]).

padding

(int or vector, optional) Optional padding on each border of the image, applied before cropping. Default is NULL. If a single int is provided this is used to pad all borders. If a vector of length 2 is provided this is the padding on left/right and top/bottom respectively. If a vector of length 4 is provided this is the padding for the left, top, right and bottom borders respectively.

pad_if_needed

(logical) It will pad the image if smaller than the desired size to avoid raising an exception. Since cropping is done after padding, the padding seems to be done at a random offset.

fill

(numeric or vector) Pixel fill value for constant fill. Default is 0. If a tuple of length 3, it is used to fill R, G, B channels respectively. This value is only used when the padding_mode is constant.

padding_mode

(str) Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant.

Details

If the image is smaller than the requested crop size, it can optionally be padded first (see padding and pad_if_needed).

Value

A dataset item of the same class with the image and target randomly cropped to the requested size.

See Also

Other item_random_transforms: item_transform_random_affine(), item_transform_random_erasing(), item_transform_random_horizontal_flip(), item_transform_random_perspective(), item_transform_random_resize_crop(), item_transform_random_rotation(), item_transform_random_vertical_flip()

Examples

## Not run: 
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_crop(before, size = c(800, 1200))

# the crop will changes the image size, so resize is needed before vision_make_grid
before_plot <- before %>%
  draw_bounding_boxes(colors = "blue", width = 10) %>%
  transform_resize(c(600, 600))
after_plot <- after %>%
  draw_bounding_boxes(colors = "red", width = 10) %>%
  transform_resize(c(600, 600))

tensor_image_browse(vision_make_grid(before_plot, after_plot))

## End(Not run)


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