The morph
function applies a kernel to a target array. Optionally,
applying the kernel to a particular array element can be made conditional on
its value, or the number of nonzero immediate neighbours that it has. The
morph
function is (S3) generic.
morph(x, kernel, ...)# S3 method for default
morph(x, kernel, operator = c("+", "-", "*", "i", "1", "0",
"=="), merge = c("sum", "min", "max", "mean", "median", "all", "any"),
value = NULL, valueNot = NULL, nNeighbours = NULL,
nNeighboursNot = NULL, renormalise = TRUE, ...)
A morphed array with the same dimensions as the original array.
Any object. For the default method, this must be coercible to an array.
An object representing the kernel to be applied, which must be
coercible to an array. It must have odd width in all dimensions, but does
not have to be isotropic in size. The kernel's dimensionality may be less
than that of the target array, x
. See kernels
for
kernel-generating functions.
Additional arguments to methods.
The operator applied elementwise within the kernel, as a
function of the original image value and the kernel value. Arithmetic
operators are as usual; "i"
is the identity operator, where every
value within the kernel will be included as-is; "1"
and "0"
include a 1 or 0 for each element within the kernel's nonzero region;
"=="
produces a 1 where the image matches the kernel, and 0
elsewhere.
The operator applied to combine the elements into a final value for the centre pixel. All have their usual meanings.
An optional vector of values in the target array for which to
apply the kernel. Takes priority over valueNot
if both are
specified.
An optional vector of values in the target array for which not to apply the kernel.
An optional numeric vector giving allowable numbers of
nonzero neighbours (including diagonal neighbours) for array elements
where the kernel will be applied. Takes priority over
nNeighboursNot
if both are specified.
An optional numeric vector giving nonallowable numbers of nonzero neighbours (including diagonal neighbours) for array elements where the kernel will be applied.
If TRUE
, the default, and merge
is
"sum"
, the sum will be renormalised relative to the sum over the
visited part of the kernel. This avoids low-intensity bands around the
edges of a morphed image.
Jon Clayden <code@clayden.org>
kernels
for kernel-generating functions, and
morphology
for more specific mathematical morphology
functions. gameOfLife
shows how this function can be used
for non-morphological purposes, in that case to power a cellular
automaton. See also the kernel
and kernapply
functions in
the stats
package, particularly if you want to smooth time series.