I'd like to vary the size of my window on a moving window filter. The size
of the radius is set by a function of the center pixel (the current pixel
processed). I picked the raster package, but as focal() is using a fix
radius for the analysis, I thought I could use a large radius and apply a
function according to the value of the center pixel inside the raster.
Now,
when reaching the borders of the raster, it's hard to tell where's the
center pixel or what's the value of the current pixel processed.
I did a little code to illustrate the problem of finding the center :
library(raster)
r <- raster(matrix(1:25, nrow=5))
vf <- function(x, ...){
x[length(x)/2+.5] # that' how I (wrongly) find the center pixel
}
r2 <- focal(r, nb=2, fun=vf)
# result :
as.matrix(r2-r)
# [,1] [,2] [,3] [,4] [,5]
#[1,] 1 0 0 0 -4
#[2,] 1 0 0 0 -4
#[3,] 1 0 0 0 -4
#[4,] 1 0 0 0 -4
#[5,] 0 -1 -1 -1 -5
Do you know any way I could implement my function or if such a
functionality
is already implemented in another package ?
Thanks,
Etienne