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...