Learn R Programming

imager (version 0.31)

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

"as.raster"(x, frames, rescale = TRUE, colourscale = NULL, colorscale = NULL, ...)

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

Value

a raster object

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
try(as.raster(boats,rescale=FALSE))
#The above fails because the pixel values are in the wrong range
boats <- boats/255 #Rescale to 0..1
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