简体   繁体   English

高效R栅格代数

[英]Efficient R Raster algebra

In order to calculate reflectance for a multi-spectral radiance image acquired with a linear scanner, I have a 1-row array of white reference values: multi-spectral image A: x rows, y cols, z bands multi-spectral reference image B: 1 row, y cols, z bands 为了计算使用线性扫描仪获取的多光谱辐射图像的反射率,我有一个白色参考值的1行数组:多光谱图像A:x行,y cols,z波段多光谱参考图像B :1行,y列,z波段

In a simple R code in array form we can set the following example: 在简单的数组形式的R代码中,我们可以设置以下示例:

a <- array(runif(3*5*6),dim=c(3,5,6)) 
b <- matrix(runif(5*6),nrow=5)

In this example I would just repeat each row of b to make an array as a and then calculate a/b, for which I do: 在此示例中,我将重复b的每一行以将数组制作为a,然后计算a / b,为此:

b2 <- rep(b,nrow(a))
dim(b2) <- c(ncol(a),dim(a)[3],nrow(a))
b3 <- aperm(b2,c(3,1,2))
res <- a/b3

Or using a loop: 或使用循环:

res <- a
for(i in 1:nrow(a)){
  res[i,,] <- a[i,,]/b
}

Now the real images are relatively large, x=969, y=640, z=224 and would like to know if there is a relatiely efficient way of doing this using package raster. 现在,实际图像相对较大,x = 969,y = 640,z = 224,并且想知道是否有使用包栅格进行此操作的相对有效的方法。 What I have tried is very slow: 我试过的很慢:

*a and b are read as a raster bricks from envi files * a和b从envi文件中读取为光栅块
*I calculate b3 as above *我如上所述计算b3
*I convert b3 to raster brick, but it is very slow and close to memory problems: *我将b3转换为光栅块,但是它非常慢并且接近内存问题:

x=969; y=640; z=224
b <- matrix(runif(y*z),nrow=y)
b2 <- rep(b,x)
dim(b2) <- c(y,z,x)
b3 <- aperm(b2,c(3,1,2))
b4 <- brick(b3)
extent(b4) <- c(0,y,0,x)
b4
writeRaster(b4,"b4",overwrite=TRUE)
rm(b2,b3,b4)
b4 <- brick("b4.gri")
b4
res <- overlay(x=RadIma,y=b4,fun=function(x,y){x/y}, filename="res",overwrite=TRUE)

There is probably a way operating with b directly thus avoiding b2,b3 and b4... 可能有一种直接与b运算的方式,从而避免了b2,b3和b4 ...

I've tried with merge(), but while it works for the reproducible example, it is impractically slow for the real case: 我已经尝试过merge(),但是虽然它适用于可重现的示例,但在实际情况下实在是太慢了:

a <- array(runif(3*5*6),dim=c(3,5,6)) 
b <- matrix(runif(5*6),nrow=5)
dim(b) <- c(1,dim(a)[2:3])

a <- brick(a)
extent(a) <- c(0,ncol(a),0,nrow(a))
res(a) <- 1
b <- brick(b)
extent(b) <- c(0,dim(b)[2],0,1)
res(b) <- 1
a
b

v <- vector(length=nrow(a),mode="list")
v[1:nrow(a)] <- list(b)
for(i in 1:nrow(a)) extent(v[[i]]) <- c(0,ncol(a),i-1,i)
b4 <- do.call(merge,args=c(v,filename="b4",format="GTiff",overwrite=TRUE))
b4
dim(a)
dim(b4)
res <- overlay(x=a,y=b4,fun=function(x,y){x/y}, filename="res",overwrite=TRUE)

Therefore this is not really a solution. 因此,这实际上不是解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM