Learn R Programming

imager (version 0.42.19)

as.raster.cimg: Convert a cimg object to a raster object for plotting

Description

raster objects are used by R's base graphics for plotting. R wants hexadecimal RGB values for plotting, e.g. gray(0) yields #000000, meaning black. If you want to control precisely how numerical values are turned into colours for plotting, you need to specify a colour scale using the colourscale argument (see examples). Otherwise the default is "gray" for grayscale images, "rgb" for colour. These expect values in [0..1], so the default is to rescale the data to [0..1]. If you wish to over-ride that behaviour, set rescale=FALSE.

Usage

# S3 method for cimg
as.raster(
  x,
  frames,
  rescale = TRUE,
  colourscale = NULL,
  colorscale = NULL,
  col.na = rgb(0, 0, 0, 0),
  ...
)

Value

a raster object

Arguments

x

an image (of class cimg)

frames

which frames to extract (in case depth > 1)

rescale

rescale so that pixel values are in [0,1]? (subtract min and divide by range). default TRUE

colourscale

a function that returns RGB values in hexadecimal

colorscale

same as above in American spelling

col.na

which colour to use for NA values, as R rgb code. The default is "rgb(0,0,0,0)", which corresponds to a fully transparent colour.

...

ignored

Author

Simon Barthelme

See Also

plot.cimg, rasterImage

Examples

Run this code
#A raster is a simple array of RGB values
as.raster(boats) %>% str
#By default as.raster rescales input values, so that:
all.equal(as.raster(boats),as.raster(boats/2)) #TRUE
#Setting rescale to FALSE changes that
as.raster(boats,rescale=FALSE) %>% plot
as.raster(boats/2,rescale=FALSE) %>% plot
#For grayscale images, a colourmap should take a single value and
#return  an RGB code
#Example: mapping grayscale value to saturation
cscale <- function(v) hsv(.5,v,1)
grayscale(boats) %>% as.raster(colourscale=cscale) %>% plot

Run the code above in your browser using DataLab