Skip to contents

The image_tensor class provides a convenient way to work with image data in tensor format. It extends the base array class and provides methods for conversion to/from various image formats, plotting, and printing.

An image_tensor object represents image data in the format "h w c" (height, width, channels) for single images, or "b h w c" (batch, height, width, channels) for batches of images, which is a common format for deep learning frameworks. It also can be a 2D array, in which case it is treated as a black and white image and shown as such.

The main utility of wrapping image data in the image_tensor class is that printing of the object will automatically display the image as a plot, as long as the imager package is installed. Otherwise it will simply print the dimension of the image.

Usage

as_image_tensor(x)

image_tensor(x)

# Default S3 method
as_image_tensor(x)

# S3 method for class 'cimg'
as_image_tensor(x)

# S3 method for class 'image_tensor'
as.cimg(x)

# S3 method for class 'image_tensor'
x[...]

# S3 method for class 'image_tensor'
plot(x, ...)

# S3 method for class 'image_tensor'
print(x, as_image = getOption("print_image_tensor_as_plot", TRUE), ...)

Arguments

x

An object to convert to or from image_tensor format.

...

Additional arguments passed to underlying methods. For [ and these are indexing arguments.

as_image

Logical. Whether to print the image as a plot. Default is controlled by the option print_image_tensor_as_plot (default: TRUE).

Value

  • as_image_tensor(): An object of class image_tensor

  • as.cimg(): A cimg object (from imager package)

  • [.image_tensor(): A subset of the image_tensor object. For 4D arrays with single index, returns a 3D slice without the batch dimension.

  • plot(): Invisibly returns the input object

  • print(): Invisibly returns the input object

Details

The image_tensor class provides the following methods (and more):

  • as_image_tensor(): Generic function to convert objects to image_tensor format. Takes in array-like objects of 2-4 dimensions. for 2 dimensional objects, it will convert them to 3D by repeating the data across 3 channels, essentially converting grayscale images to RGB.

  • as_image_tensor.default(): Default method that converts arrays to image_tensor

  • as_image_tensor.cimg(): Method to convert cimg objects (from imager package) to image_tensor

  • as.cimg.image_tensor(): Method to convert image_tensor objects back to cimg format

  • [.image_tensor(): Subset method for image_tensor objects

  • plot.image_tensor(): Plot method for image_tensor objects

  • print.image_tensor(): Print method for image_tensor objects

Format

An image_tensor object is an array with dimensions in "h w c" format for single images, or "b h w c" format for batches of images:

  • h: height dimension (image height in pixels)

  • w: width dimension (image width in pixels)

  • c: channel dimension (RGB, only for 3D & 4D arrays)

  • b: batch dimension (number of images, only for 4D arrays)

Options

The following options control the behavior of image_tensor methods:

  • plot_image_tensor_axes: Whether to show axes in plots (default: FALSE)

  • print_image_tensor_as_plot: Whether to print images as plots (default: TRUE)

Examples

# create from a matrix (grayscale)
img <- image_tensor(matrix(1:9, 3, 3))
print(img)
print(img[1:2, 1:2])

# create from a 3D array (RGB image)
img_rgb <- as_image_tensor(array(runif(27), dim = c(3, 3, 3)))
print(img_rgb)