This function extracts a subset of the pixel values in a
pixel image. (To reassign the pixel values, see [<-.im
). 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 i
.
If i
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 i
is a pixel image with logical values,
it is interpreted as a spatial window (with TRUE
values
inside the window and FALSE
outside).
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 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 i
is a point pattern (or something that can be
converted to a point pattern by as.ppp
such as a
list of x,y
coordinates), then
X[i, 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 i
.
It may contain NA
s corresponding to points which lie outside
the spatial domain of the image x
.
By contrast, X[i]
or X[i, drop=TRUE]
contains only those
pixel values which are not NA
. It may be shorter.
If i
is a spatial window
then X[i, drop=FALSE]
is another pixel image
of the same dimensions as x
obtained by
setting all pixels outside the window i
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 i
.
If i
is a spatial window which is not a rectangle
(i$type != "rectangle"
) then X[i, drop=TRUE]
is a numeric vector containing the pixel values for all pixels
that lie inside the window i
.
If i
is a rectangle (a spatial window
with i$type = "rectangle"
) then X[i, drop=TRUE]
is a pixel image. The spatial domain of this image is the
intersection of i
with the spatial domain of x
.
If the optional argument raster
is given, then it should
be a binary image mask or a pixel image. Then
x
will first be converted to an image defined on the
pixel grid implied by raster
, before the subset operation
is carried out.
In particular, x[i, raster=i, drop=FALSE]
will return
an image defined on the same pixel array as the object i
.
If i
does not satisfy any of the conditions above,
then it is assumed to be a valid index for
the matrix as.matrix(x)
. The result is a vector or matrix of
pixel values.