[an error occurred while processing this directive]
Fields: Web Manual
Table of Contents
1. Introduction

2. Thin Plate Splines: Tps 3. Spatial Process Models: Krig 4. Simulating Random Fields (sim.rf)

5. Spatial Predictions for Large Data Sets
6. Other Fields Functions References

Printable version of Fields Manual

Back to Fields Home Page
Working with Images

In the course of spatial analysis of 2-d fields we found need for some simple functions to create, plot, and smooth rectangular images. Here we mention two, as.image and image.plot.
By a surface or image object we mean a list with components x, y and z Where x and y are equally spaced grids and z is a matrix of values. This is the same format as used by the S functions contour, image, and persp

as.image creates an image object from irregular x,y,z by discretizing the 2-d locations to a grid and then producing an image object with the z values in the right cells. The arguments necessary for as.image to work are:

Z: Values of image.

The rest of the arguments are optional. They are:

  • ind = NULL, A matrix giving the row and column subscripts for each image value in Z. (Not needed if x is specified)

  • grid = NULL, A list with components x and y of equally spaced values describing the centers of the grid points. The default is to use nrow and ncol and the ranges of the data locations (x) to construct a grid.

  • x = NULL, Locations of image values. Not needed if ind is specified.

  • nrow = 64, Number of rows in image matrix (x-axis direction).

  • ncol = 64, Number of columns in image matrix (y-axis direction)

  • weights = rep( 1, length(Z))
Weights used to form mean values. If ind is passed in, as.image will use ind for x. If x and grid are both missing, as.image will use 1:nrow and 1:ncol for the grid. If x is passed in, but grid is not, then as.image will use a nrow x ncol discretized x for the grid. For fine grids each Z value will occupy its own grid square and so the conversion from irregular data to an image is unambiguous. If two or more Z values are mapped to same grid box then the returned value is their average. (It is easy to modify this function to produce other summaries but this is left to the user.)
For an example, we consider the function, image.plot, is also a fields function which simply draws the image plot with a color legend along side.

look <- as.image( coalash$y, x = coalash$x)
image.plot (look)

In this conversion grid boxes without Z value are coded as NA and so appear as the background color in an image plot. The function image.plot has the same functionality as image but adds a legend strip. It can be used to add a legend strip to an existing plot or create a new image and legend. Its chief benefit is that this function resides the plot region automatically to make room for the legend strip. We have found a function with reasonable defaults has been quite useful.


This is software for statistical research and not for commercial uses. The authors do not guarantee the correctness of any function or program in this package. Any changes to the software should not be made without the authors permission.

Last modified: Dec 21 2005   by thoar@ucar.edu