R中的滑动窗口功能

Ser*_*jev 2 r image-processing median

有人知道R中是否存在2d矩阵的滑动窗口方法,而不仅仅是向量.我需要将中值函数应用于存储在矩阵中的图像

Jos*_*ien 8

focal()优秀的光栅包中的功能对此有好处.除了以下示例中显示的参数之外,它还需要几个参数,如果需要,可以用于指定非矩形滑动窗口.

library(raster)

## Create some example data
m <- matrix(1, ncol=10, nrow=10)
diag(m) <- 2
r <- as(m, "RasterLayer") # Coerce matrix to RasterLayer object

## Apply a function that returns a single value when passed values of cells
## in a 3-by-3 window surrounding each focal cell 
rmean <- focal(r, w=matrix(1/9, ncol=3, nrow=3), fun=mean)
rmedian <- focal(r, w=matrix(1/9, ncol=3, nrow=3), fun=median)

## Plot the results to confirm that this behaves as you'd expect
par(mfcol=c(1,3))
plot(r)
plot(rmean)
plot(rmedian)

## Coerce results back to a matrix, if you so desire
mmean <- as(rmean, "matrix")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述