0

I am in the process of computing an NDVI index for an image. The NDVI should give values between 0 and 1, however I get much larger values. My data is in a [5000,5000,3] array, and the important bands for the NDVI calculation are the second and the fourth. All entries in my arrays are positive. My code is:

nu=imarray[:,:,3]-imarray[:,:,1]
de=imarray[:,:,3]+imarray[:,:,1]
NDVI=div0(nu,de)

Where imarray is my array, nu is the numerator and de is the denominator. I use denis's excellent div0 function from this question, which divides the numerator by the denominator but sets the potential 0/0 to equal 0. Inserting the following print statements for debugging however:

print np.amax(NDVI)
print np.amin(NDVI)

Gives

38.1372781065
0.0

I.e. instead of being between -1 and 1, the index is between 0 and 38.1. Mathematically this appears to make little sense, so does anyone know what I am programming incorrectly here?

Thanks in advance :)

Community
  • 1
  • 1
G.Lang
  • 121
  • 1
  • 3
  • the problem has to be on the values contained within your arrays. I see no other explanation. – Ma0 Jul 04 '16 at 09:13
  • Thank you for your comment. Any suggestions what could be wrong? Running np.amin and np.amax on the arrays gives that the fourth band has a min of 0 and a max of 25697, whereas the second band has a min of 0 and a max of 16126. Running `np.isnan(imarray).any()` gives false... – G.Lang Jul 04 '16 at 09:18
  • Don't just look at the min/max values, look at the whole NDVI array. If like 98% of it is within [0,1] just ignore the rest as noise. Min and Max of an array with 25 million elements say nothing about the array. What is the mean/deviation? – Ma0 Jul 04 '16 at 09:18
  • For non-negative x and y, (x-y)/(x+y) is always between -1 and 1. Maybe you have negative values in your array? – Aguy Jul 04 '16 at 09:20
  • Thank you for pointing out that I could also get -1, which I completely forgot whilst asking the question, but as written earlier there does not seem to be any negative values... – G.Lang Jul 04 '16 at 09:22
  • 2
    Is your data dtype=uint8 by any chance? – Aguy Jul 04 '16 at 09:33
  • G.Lang Then you are perfectly fine. Having pixels (or array elements) with weird intensities/values happens. I would suggest putting a ``NDVI = min(div0(nu, de), 1)``. Don't sweat it that much. – Ma0 Jul 04 '16 at 09:37
  • 1
    @Theguy YES! It was dtype=uint8! Changing it to float immediately gave me values between -1 and 1! Thank you so much, you just saved me hours of confusion. I am new to stackoverflow - any way I can mark your comment as correct? :) – G.Lang Jul 04 '16 at 09:43
  • I've duplicated your problem with `imread`-ing an image. The data is uint8. Then the result of addition and subtraction are wrong. I guess the div0 does manage mask the fact that these are unit8. – Aguy Jul 04 '16 at 09:45
  • @G.Lang - happy to help. You can upvote the comment that helped you. – Aguy Jul 04 '16 at 09:50
  • how can it be uint8 and store values as big as 25697? – Ma0 Jul 04 '16 at 09:53
  • @Theguy alas I do not have enough reputation yet, however I will get back to it once I do. Thank you once again for your help! – G.Lang Jul 04 '16 at 09:53

0 Answers0