NDVI差异

时间:2013-02-21 12:30:33

标签: r raster

我有两个ndvi图像的值为-1到1作为min / max ....我想从1st减去第二个图像,反之亦然但是当我这样做时它给了我带有0值的光栅.. 。我认为它应该是这样的,因为我们从另一个或一个图像中减去一个乐队从另一个...请帮助我在哪里出错...这是我用来看变化的代码....

    b3<-raster(stackIm1,3) #band 3 of first input image
    b4<-raster(stackIm1,4) #band 4 of first input image
    ndvi1<-(b4-b3)/(b4+b3) #calculating NDVI for first image

    b3<-raster(stackIm2,3) #band 3 of second image
    b4<-raster(stackIm2,4) #band 4 of second image
    ndvi2<-(b4-b3)/(b4+b3) #calculating NDVI for second image

    ndvi <- ndvi1-ndvi2  #subtracting second ndvi image from first

提前致谢

1 个答案:

答案 0 :(得分:2)

这就是你如何制作一个可重复的例子:

生成一些数据

library(raster)
r <- raster(nc=10, nr=10)
set.seed(2212013)
stackIm1 <- stack(lapply(1:5, function(x) setValues(r, round(runif(ncell(r))* 255))))
stackIm2 <- stack(lapply(1:5, function(x) setValues(r, round(runif(ncell(r))* 255))))

然后用它来说明问题

b3<-raster(stackIm1,3) #band 3 of first input image
b4<-raster(stackIm1,4) #band 4 of first input image
ndvi1<-(b4-b3)/(b4+b3) #calculating NDVI for first image

b3<-raster(stackIm2,3) #band 3 of second image
b4<-raster(stackIm2,4) #band 4 of second image
ndvi2<-(b4-b3)/(b4+b3) #calculating NDVI for second image

ndvi <- ndvi1-ndvi2  #subtracting second ndvi image from first

但我不认为这里有问题。

> ndvi
class       : RasterLayer 
dimensions  : 10, 10, 100  (nrow, ncol, ncell)
resolution  : 36, 18  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 
data source : in memory
names       : layer 
values      : -1.461194, 1.590255  (min, max)

>