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.
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
rArr <- as.array(r, transpose = TRUE) rErode <- erode(rArr, sk) rErode <- setValues(r, as.vector(aperm(rErode)))
erode(), also the morphological functions
closing() can be applied like this.