Learn R Programming

imager (version 0.42.19)

as.cimg.function: Create an image by sampling a function

Description

Similar to as.im.function from the spatstat package, but simpler. Creates a grid of pixel coordinates x=1:width,y=1:height and (optional) z=1:depth, and evaluates the input function at these values.

Usage

# S3 method for `function`
as.cimg(
  obj,
  width,
  height,
  depth = 1,
  spectrum = 1,
  standardise = FALSE,
  dim = NULL,
  ...
)

Value

an object of class cimg

Arguments

obj

a function with arguments (x,y), or (x,y,cc), or (x,y,z), etc. Must be vectorised; see examples.

width

width of the image (in pixels)

height

height of the image (in pixels)

depth

depth of the image (in pixels). Default 1.

spectrum

number of colour channels. Defaut 1.

standardise

coordinates are scaled and centered (see doc for pixel.grid)

dim

a vector of image dimensions (can be used instead of width, height, etc.)

...

ignored

Author

Simon Barthelme

Examples

Run this code
im = as.cimg(function(x,y) cos(sin(x*y/100)),100,100)
plot(im)
#The following is just a rectangle at the center of the image 
im = as.cimg(function(x,y) (abs(x) < .1)*(abs(y) < .1) ,100,100,standardise=TRUE)
plot(im)
#Since coordinates are standardised the rectangle scales with the size of the image
im = as.cimg(function(x,y) (abs(x) < .1)*(abs(y) < .1) ,200,200,standardise=TRUE)
plot(im)
#A Gaussian mask around the center
im = as.cimg(function(x,y) dnorm(x,sd=.1)*dnorm(y,sd=.3) ,dim=dim(boats),standardise=TRUE)
im = im/max(im)

plot(im*boats)
#A Gaussian mask for just the red channel
fun = function(x,y,cc) ifelse(cc==1,dnorm(x,sd=.1)*dnorm(y,sd=.3),0)
im = as.cimg(fun,dim=dim(boats),standardise=TRUE)
plot(im*boats)

Run the code above in your browser using DataLab