0

I have two ndvi images having values -1 to 1 as min/max....i want to subtract 2nd image from 1st or vice-versa but when i am doing that it is giving me raster with 0 value...which i think it should be like that as we subtract one band from other or one image from another....kindly help me where i am getting wrong.....here is the code which i am using to see the change....

    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

Thanks in advance

Angad
  • 31
  • 1
  • 5
  • 2
    Please make your example reproducible, without that we cannot help you effectively.. – Paul Hiemstra Feb 21 '13 at 12:34
  • 1
    actually i have a landsat image for a region so i am just stacking it and taking two bands for calculating indices and then displaying it after that i have done same thing for second image with same extent and area with different year and then subtracting second ndvi from first so that i could get the difference between that.....that is what i want.... – Angad Feb 21 '13 at 12:55
  • We can't help you if we don't see what's wrong. I suggest you make a small reproducible example. See this question for how to make a good one: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Feb 21 '13 at 13:02
  • 1
    dear i am not able to create reproducible of ndvi raster having value from -1 to 1....because when same thing i am using with other raster like any two bands of landsat it is giving me the answer but not in case of ndvi.....is it possible if i can get your e-mail ID so that i can send a subset of it with complete code... – Angad Feb 21 '13 at 13:16
  • Then post a link to the landsat images. Meanwhile, have you verified that your `b3` and `b4` have valid values, and that they are in fact different? – Carl Witthoft Feb 21 '13 at 14:17
  • This is my 1st raster values and the second raster is also having the similar extent, class, dimension, ref system and values also...but cells contains different values values which should be in floating values and has to between -1 to 1. class : RasterLayer dimensions : 7021, 7991, 56104811 (nrow, ncol, ncell) resolution : 30, 30 (x, y) extent : 673485, 913215, 3088785, 3299415 (xmin, xmax, ymin, ymax) coord. ref. : +proj=utm +zone=43 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 data source : names : layer values : -1, 1 (min, max) – Angad Feb 21 '13 at 16:52

1 Answers1

2

This is how you can make a reproducible example:

Generate some data

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))))

And then use it to illustrate the problem

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

But I do not see a problem here.

> 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)

> 
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63