There is a couple of things wrong with this:
mat <- matrix(1/25,ncol=5, nrow=5)
raster_res <- focal(raster1, mat, FUN="mean", na.rm=T)
The argument to supply the function is called fun
, not FUN
. You give each value a weight of 1/25, and then you want to use "mean". However, you should use "sum" in that case! (which is the default, and because you were using FUN
instead of fun
, that is what happened anyway.) However, since you have missing values and you are using na.rm=TRUE
, you really need to give each value a weight of 1 and then use mean
.
Also, as you want to fill in missing values, not change existing values, I would use NAonly = TRUE
, together with pad=TRUE
(to pad virtual rows and columns with NA
s outside of the raster).
Here is an example:
library(raster)
# example data
logo <- raster(system.file("external/rlogo.grd", package="raster"))
set.seed(0)
i <- sample(ncell(logo), 200)
logo[i] <- NA
plot(logo)
m <- matrix(1, ncol=5, nrow=5)
r <- focal(logo, m, fun="mean", na.rm=TRUE, NAonly=TRUE, pad=TRUE)
plot(r)
To see for which cells the values were estimated
plot(as(reclassify(is.na(logo), cbind(0, NA)), 'SpatialPolygons'), add=TRUE)
Also, if you want to write the raster to a file, instead of using writeRaster
you should do that in one step:
r <- focal(logo, m, fun="mean", na.rm=TRUE, NAonly=TRUE, pad=TRUE, filename="smooth.tif", overwrite=TRUE)
Best Answer
The Edge Extraction tool may have been moved to the Orfeo Toolbox as this focuses on image analysis. The Orfeo Toolbox contains a number of edge extraction tools using various methods such as: