# subset.im

##### Extract Subset of Image

Extract a subset or subregion of a pixel image.

##### Usage

```
## S3 method for class 'im':
[(x, w)
## S3 method for class 'im':
[(x, w, drop=TRUE)
```

##### Arguments

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

. - w
- Object defining the subregion or subset to be extracted.
Either a spatial window (an object of class
`"owin"`

) or a point pattern (an object of class`"ppp"`

). - drop
- Logical value.
Locations in
`w`

that lie outside the spatial domain of the image`X`

return a pixel value of`NA`

if`drop=FALSE`

, and are omitted if`drop=TRUE`

.

##### Details

This function extracts a subset 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 extracted is determined by the argument `w`

.
If `w`

is a spatial window (an object of class `"owin"`

),
the values of the image inside this window are extracted
(after first clipping the window to the spatial domain of the image
if necessary). If `w`

is a point pattern (an object of class
`"ppp"`

), then the values of the pixel image at the points of
this pattern are extracted.
At locations outside the spatial domain of the image,
the pixel value is undefined, and is taken to be `NA`

.
The logical argument `drop`

determines whether such `NA`

values will be returned or omitted.
It also influences the format of the return value.

If `w`

is a point pattern,
`X[w, drop=FALSE]`

is a numeric vector
containing the pixel values at each of the points of the pattern.
Its length is equal to the number of points in the pattern `w`

.
It may contain `NA`

s corresponding to points which lie outside
the spatial domain of the image `X`

.
By contrast, `X[w]`

or `X[w, drop=TRUE]`

contains only those
pixel values which are not `NA`

. It may be shorter.

If `w`

is a spatial window
then `X[w, drop=FALSE]`

is another pixel image
of the same dimensions as `X`

obtained by
setting all pixels outside the window `w`

to have value
`NA`

. When the result is displayed by `plot.im`

the effect
is that the pixel image `X`

is clipped to the window `w`

.

If `w`

is a spatial window which is *not* a rectangle
(`w$type != "rectangle"`

) then `X[w, drop=TRUE]`

is a numeric vector containing the pixel values for all pixels
that lie inside the window `w`

.

If `w`

is a rectangle (a spatial window
with `w$type = "rectangle"`

) then `X[w, drop=TRUE]`

is a pixel image. The spatial domain of this image is the
intersection of `w`

with the spatial domain of `X`

.

##### Value

- Either a pixel image or a vector of pixel values. See Details.

##### synopsis

## S3 method for class 'im': [(x, i, j, drop=TRUE, \dots)

##### See Also

##### Examples

```
# make up an image
X <- setcov(unit.square())
plot(X)
# a rectangular subset
W <- owin(c(0,0.5),c(0.2,0.8))
Y <- X[W]
plot(Y)
# a polygonal subset
data(letterR)
R <- affine(letterR, diag(c(1,1)/2), c(-2,-0.7))
Y <- X[R, drop=FALSE]
plot(Y)
# a point pattern
P <- rpoispp(20)
Y <- X[P]
```

*Documentation reproduced from package spatstat, version 1.9-1, License: GPL version 2 or newer*