The package mmand
provides functions for the calculation of Mathematical Morphologies for n-dimensional arrays. With a little workaround, these can also be calculated for raster images.
library(raster)
library(mmand)
r <- raster("C:/Program Files/R/R-3.2.3/doc/html/logo.jpg")
plot(r)
At first, a kernel (moving window) has to be set with a size (e.g. 9x9) and a shape type (e.g. disc
, box
or diamond
)
sk <- shapeKernel(c(9,9), type="disc")
Afterwards, the raster layer has to be converted into an array wich is used as input for the erode()
function.
rArr <- as.array(r, transpose = TRUE)
rErode <- erode(rArr, sk)
rErode <- setValues(r, as.vector(aperm(rErode)))
Besides erode()
, also the morphological functions dilate()
, opening()
and closing()
can be applied like this.
plot(rErode)