spatstat.geom (version 3.2-9)

Replace.im: Reset Values in Subset of Image

Description

Reset the values in a subset of a pixel image.

Usage

# S3 method for im
[(x, i, j, ..., drop=TRUE) <- value

Value

The image x with the values replaced.

Arguments

x

A two-dimensional pixel image. An object of class "im".

i

Object defining the subregion or subset to be replaced. Either a spatial window (an object of class "owin"), or a pixel image with logical values, or a point pattern (an object of class "ppp"), or any type of index that applies to a matrix, or something that can be converted to a point pattern by as.ppp (using the window of x).

j

An integer or logical vector serving as the column index if matrix indexing is being used. Ignored if i is appropriate to some sort of replacement other than matrix indexing.

...

Ignored.

drop

Logical value specifying what happens when i and j are both missing. See Details.

value

Vector, matrix, factor or pixel image containing the replacement values. Short vectors will be recycled.

Warning

If you have a 2-column matrix containing the \(x,y\) coordinates of point locations, then to prevent this being interpreted as an array index, you should convert it to a data.frame or to a point pattern.

Author

Adrian Baddeley Adrian.Baddeley@curtin.edu.au, Rolf Turner rolfturner@posteo.net and Ege Rubak rubak@math.aau.dk.

Details

This function changes some of the pixel values in a pixel image. The image x must be an object of class "im" representing a pixel image defined inside a rectangle in two-dimensional space (see im.object).

The subset to be changed is determined by the arguments i,j according to the following rules (which are checked in this order):

  1. i is a spatial object such as a window, a pixel image with logical values, or a point pattern; or

  2. i,j are indices for the matrix as.matrix(x); or

  3. i can be converted to a point pattern by as.ppp(i, W=Window(x)), and i is not a matrix.

If i is a spatial window (an object of class "owin"), the values of the image inside this window are changed.

If i is a point pattern (an object of class "ppp"), then the values of the pixel image at the points of this pattern are changed.

If i does not satisfy any of the conditions above, then the algorithm tries to interpret i,j as indices for the matrix as.matrix(x). Either i or j may be missing or blank.

If none of the conditions above are met, and if i is not a matrix, then i is converted into a point pattern by as.ppp(i, W=Window(x)). Again the values of the pixel image at the points of this pattern are changed.

If i and j are both missing, as in the call x[] <- value, then all pixel values in x are replaced by value:

  • If drop=TRUE (the default), then this replacement applies only to pixels whose values are currently defined (i.e. where the current pixel value is not NA). If value is a vector, then its length must equal the number of pixels whose values are currently defined.

  • If drop=FALSE then the replacement applies to all pixels inside the rectangle Frame(x). If value is a vector, then its length must equal the number of pixels in the entire rectangle.

See Also

im.object, [.im, [, ppp.object, as.ppp, owin.object

Examples

Run this code
 # make up an image
 X <- setcov(unit.square())
 plot(X)

 # a rectangular subset
 W <- owin(c(0,0.5),c(0.2,0.8))
 X[W] <- 2
 plot(X)

 # a polygonal subset
 R <- affine(letterR, diag(c(1,1)/2), c(-2,-0.7))
 X[R] <- 3
 plot(X)

 # a point pattern
 X[cells] <- 10
 plot(X)

 # change pixel value at a specific location
 X[list(x=0.1,y=0.2)] <- 7

 # matrix indexing --- single vector index
 X[1:2570] <- 10
 plot(X)

 # matrix indexing using double indices
 X[1:257,1:10] <- 5
 plot(X)

 # matrix indexing using a matrix of indices
 X[cbind(1:257,1:257)] <- 10
 X[cbind(257:1,1:257)] <- 10
 plot(X)

 # Blank indices
 Y <- as.im(letterR)
 plot(Y)
 Y[] <- 42  # replace values only inside the window 'R'
 plot(Y)
 Y[drop=FALSE] <- 7 # replace all values in the rectangle
 plot(Y)

 Z <- as.im(letterR)
 Z[] <- raster.x(Z, drop=TRUE) # excludes NA
 plot(Z)
 Z[drop=FALSE] <- raster.y(Z, drop=FALSE) # includes NA
 plot(Z)

Run the code above in your browser using DataCamp Workspace