-1

我有一个像下面这样的矩阵,从光栅文件中获得:

0   0   0   0   0   0   0   4   254 252
0   0   0   0   0   0   0   0   255 246
0   0   0   0   0   0   0   1   255 246
0   0   0   0   0   4   32  254 255 246
0   0   0   0   8   255 255 255 255 246
0   0   0   0   0   11 214 254 255 246
0   0   0   0   0   0   0   1   255 246
0   0   0   0   0   0   0   1   255 246
1   0   0   0   0   0   0   2   255 253
247 247 247 247 247 247 247 247 249 251

我想使用一个半径为“x”的高斯滤波器,它能够估计该半径内所考虑像素值的标准偏差和平均值。作为输出,我想得到一个“平均值”矩阵(通过使用过滤半径为每个像素估计)和一个“标准偏差”矩阵。

您对如何在 R 中执行此操作有任何建议吗?

4

1 回答 1

1

Given matrix m

m <- matrix(c(0,0,0,0,0,0,0,4,254,252,0,0,0,0,0,0,0,0,255,246,0,0,0,0,0,0,0,1,255,246,0,0,0,0,0,4,32,254,255,246,0,0,0,0,8,255,255,255,255,246,0,0,0,0,0,11,214,254,255,246,0,0,0,0,0,0,0,1,255,246,0,0,0,0,0,0,0,1,255,246,1,0,0,0,0,0,0,2,255,253,247,247,247,247,247,247,247,247,249,251), ncol=10, byrow=TRUE)

You can compute the (Gaussian) weighted mean like this

library(raster)
r <- raster(m)

# Gaussian filter
gf <- focalWeight(r, .2, "Gauss")
rg <- focal(r, w=gf, na.rm=TRUE, pad=TRUE)

# plot(rg)
# as.matrix(rg)

I don't know how you would compute a weighted standard deviation.

For a standard focal mean and sd

 fm <- focal(r, w=matrix(1,3,3), fun=mean, pad=TRUE, na.rm=TRUE) 
 fd <- focal(r, w=matrix(1,3,3), fun=sd, pad=TRUE, na.rm=TRUE) 
于 2018-02-25T19:19:23.390 回答