item_transform_pad: Pad a dataset item

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

item_transform_padR Documentation

Pad a dataset item

Description

Pads the image inside a dataset item on all sides with the given "pad" value. For detection items, bounding box coordinates are shifted to remain correct after padding. For segmentation items, both the image and the masks are padded.

Usage

item_transform_pad(x, padding, 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.

padding

(integer vector or integer): Padding on each border. If a single integer is provided, this value is used to pad all borders. If an integer vector of length 2 is provided, it gives the padding on left/right and top/bottom respectively. If an integer vector of length 4 is provided, it gives the padding for the left, right, top and bottom borders respectively.

fill

(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.

padding_mode

(character): Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant. Mode symmetric is not yet supported for Tensor inputs.

  • constant: pads with a constant value, this value is specified with fill

  • edge: pads with the last value on the edge of the image

  • reflect: pads with reflection of image (without repeating the last value on the edge) padding ⁠[1, 2, 3, 4]⁠ with 2 elements on both sides in reflect mode will result in ⁠[3, 2, 1, 2, 3, 4, 3, 2]⁠

  • symmetric: pads with reflection of image (repeating the last value on the edge) padding ⁠[1, 2, 3, 4]⁠ with 2 elements on both sides in symmetric mode will result in ⁠[2, 1, 1, 2, 3, 4, 4, 3]⁠

Value

A dataset item of the same class with the padded image and adjusted target.

See Also

Other item_unitary_transforms: item_transform_affine(), item_transform_center_crop(), item_transform_crop(), item_transform_hflip(), item_transform_perspective(), item_transform_resize(), item_transform_rotate(), item_transform_vflip()

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_pad(before, padding = 100)

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(2000, 3000))
after_plot <- transform_resize(after_plot, c(2000, 3000))

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.