Convert to Pixel Image

Converts various kinds of data to a pixel image

manip, spatial
Usage, W=as.mask(as.owin(X), dimyx=dimyx), ...,
        dimyx=NULL, na.replace=NULL)
Data to be converted to a pixel image.
Window object which determines the spatial domain of the image
Additional arguments passed to X when X is a function.
Optional. Dimensions for the pixel array. An integer, or vector of 2 integers c(ny, nx).
Optional value to replace NA entries in the output image.

This function converts the data X into a pixel image object of class "im" (see im.object).

Currently X may be any of the following:

  • a pixel image object, of class"im".
  • a window object, of class"owin"(seeowin.object). The result is an image with all pixel entries equal to1inside the windowX, andNAoutside.
  • a single number (or a single logical, complex, factor or character value). The result is an image with all pixel entries equal to this constant value inside the windowW(andNAoutside, unless the argumentna.replaceis given). ArgumentWis required.
  • a function of the formfunction(x, y, ...)which is to be evaluated to yield the image pixel values. In this case, the additional argumentWmust be present. This window will be converted to a binary image mask. Then the functionXwill be evaluated in the formX(x, y, ...)wherexandyarevectorscontaining the$x$and$y$coordinates of all the pixels in the image mask, and...are any extra arguments given. This function must return a vector or factor of the same length as the input vectors, giving the pixel values.
  • a list with entriesx, y, zin the format expected by the standardRfunctionsimage.defaultandcontour.default. That is,zis a matrix of pixel values,xandyare vectors of$x$and$y$coordinates respectively, andz[i,j]is the pixel value for the location(x[i],y[j]).

The spatial domain (enclosing rectangle) of the pixel image is determined by the argument W. If W is absent, the spatial domain is determined by X. When X is a function or a single numerical value, W is required. The pixel array dimensions of the final resulting image are determined by (in priority order)

  • the argumentdimyxif present;
  • the pixel dimensions of the windowW, if it is present and if it is a binary mask;
  • the pixel dimensions ofXif it is an image, a binary mask, or alist(x,y,z);
  • the default pixel dimensions, controlled byspatstat.options.

Note that if dimyx is given, this will override the pixel dimensions of X if it has them. Thus, can be used to change an image's pixel dimensions.

If the argument na.replace is given, then all NA entries in the image will be replaced by this value. The resulting image is then defined everwhere on the full rectangular domain, instead of a smaller window. Here na.replace should be a single value, of the same type as the other entries in the image.


  • An image object of class "im".

  # window object
  W <- demopat$window
  Z <-
  # function
  Z <-,y) {x^2 + y^2}, unit.square())
  # function with extra arguments
  f <- function(x, y, x0, y0) {
      sqrt((x - x0)^2 + (y-y0)^2)
  Z <-, unit.square(), x0=0.5, y0=0.5)
  # Revisit the Sixties
  Z <-, letterR, x0=2.5, y0=2)
  # usual convention in S
  stuff <- list(x=1:10, y=1:10, z=matrix(1:100, nrow=10))
  Z <-
  # convert to finer grid
  Z <-, dimyx=256)
Documentation reproduced from package spatstat, version 1.12-5, License: GPL (>= 2)

Community examples

Looks like there are no examples yet.