Learn R Programming

spatstat (version 1.11-4)

im: Create a Pixel Image Object

Description

Creates an object of class "im" representing a two-dimensional pixel image.

Usage

im(mat, xcol=seq(ncol(mat)), yrow=seq(nrow(mat)), lev=levels(mat),
   units=NULL)

Arguments

mat
matrix or vector containing the pixel values of the image.
xcol
vector of $x$ coordinates for the pixel gid
yrow
vector of $y$ coordinates for the pixel grid
lev
possible factor levels, if mat should be interpreted as a factor.
units
Optional. Name of unit of length. Either a single character string, or a vector of two character strings giving the singular and plural forms, respectively.

code

as.im

Warnings

The internal representation of images is likely to change in future releases of spatstat. The safe way to extract pixel values from an image object is to use as.matrix.im or [.im.

Details

This function creates an object of class "im" representing a two-dimensional pixel image. See im.object for details of this class.

The matrix mat contains the greyscale values for a rectangular grid of pixels. Note carefully that the entry mat[i,j] gives the pixel value at the location (xcol[j],yrow[i]. That is, the row index of the matrix mat corresponds to increasing y coordinate, while the column index of mat corresponds to increasing x coordinate. Thus yrow has one entry for each row of mat and xcol has one entry for each column of mat. Under the usual convention in R, a correct display of the image would be obtained by transposing the matrix, e.g. image.default(xcol, yrow, t(mat)), if you wanted to do it by hand.

The entries of mat may be numeric (real or integer), complex, logical, character, or factor values. If mat is not a matrix, it will be converted into a matrix with nrow(mat) = length(yrow) and ncol(mat) = length(xcol).

R

See Also

im.object, as.im, as.matrix.im, [.im, eval.im

Examples

Run this code
vec <- rnorm(1200)
   mat <- matrix(vec, nrow=30, ncol=40)
   whitenoise <- im(mat)
   whitenoise <- im(mat, xcol=seq(0,1,length=40), yrow=seq(0,1,length=30))
   whitenoise <- im(vec, xcol=seq(0,1,length=40), yrow=seq(0,1,length=30))
   plot(whitenoise)

   # FACTOR-VALUED IMAGES:
   cutvec <- cut(mat, 3)
   # although mat was a matrix, cutvec is a vector, with factor values
   cutwhite <- im(cutvec, xcol=seq(0,1,length=40), yrow=seq(0,1,length=30))
   # cutwhite is a factor-valued image
   plot(cutwhite)

Run the code above in your browser using DataLab