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), ...)
Value
as_image_tensor()
: An object of classimage_tensor
as.cimg()
: Acimg
object (from imager package)[.image_tensor()
: A subset of theimage_tensor
object. For 4D arrays with single index, returns a 3D slice without the batch dimension.plot()
: Invisibly returns the input objectprint()
: Invisibly returns the input object
Details
The image_tensor
class provides the following methods (and more):
as_image_tensor()
: Generic function to convert objects toimage_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 toimage_tensor
as_image_tensor.cimg()
: Method to convertcimg
objects (from imager package) toimage_tensor
as.cimg.image_tensor()
: Method to convertimage_tensor
objects back tocimg
format[.image_tensor()
: Subset method forimage_tensor
objectsplot.image_tensor()
: Plot method forimage_tensor
objectsprint.image_tensor()
: Print method forimage_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)