Public Lab Research note


How to calculate NDVI index in python ?

by it13 | November 15, 2019 12:01 15 Nov 12:01 | #21494 | #21494

Hello

Here i am trying to calculate ndvi index.

but in python if i read the image get band than after try to perform ndvi index it will give me numpy array.

so how to calculate ndvi index range between -1 to 1 ?


9 Comments

Hi, there are a couple of things to look out for: conversion of data types and not dividing by zero + contrast stretching and colormap to format the NDVI images .

Sample code is below and more detailed post is at: https://publiclab.org/notes/MaggPi/08-03-2018/ngb-ndvi-video-optimization-red-blue-manual-gain-control Code is a mix of python and opencv but you can also use other python routines.


# Get the individual colour components of the image b, g, r = cv2.split(image)

[#start](/tag/start) video capture ret, image = cap.read() # Calculate the NDVI # Bottom of fraction bottom = (r.astype(float) + g.astype(float)) bottom[bottom == 0] = 0.01 # Make sure we don't divide by zero! ndvi = (r.astype(float) - g) / bottom ndvi = contrast_stretch(ndvi) ndvi = ndvi.astype(np.uint8)

Hello MaggPi, Thanks For The Reply. We Are Using Beta sequncer library for ndvi image conversion. but there we are not getting ndvi index.So if You any idea about this how to getting ndvi index using beta sequncer.


Hi, can you share the image you're trying to process, and the output you're getting? Thanks!

Is this a question? Click here to post it to the Questions page.


Hello Warren, Thanks For Reply. This is my Orignal Image and ndvi image 2.png NDVI image

2.jpg Original Image

Is this a question? Click here to post it to the Questions page.


Hi! This looks pretty good, actually -- you're using a blue filter, i see; there's evidence that red filters are a bit better, but you're getting pretty good values for NDVI, it seems, just based on how vegetation is so clearly >0 here. What did you expect to get that you're not seeing? Thanks!

Is this a question? Click here to post it to the Questions page.


Hello Yes We are using Blue filter camera for capture images. but now we are getting ndvi image proper but not getting ndvi index (-1 to 1). so how to calculate ndvi index ???. Thanks!.

Is this a question? Click here to post it to the Questions page.


Ah, i see! So, when I drop your original image into https://publiclab.github.io/infragram/, and run the equation (R-B)/(R+B)+(1/3) (boosting the overall by 33%), i get this result; i've included a screenshot with the scale bar so you can see what NDVI value each color corresponds to. Does that help?

Screen_Shot_2019-11-26_at_10.57.56_AM.png

Note that to get an absolute NDVI value, you need to calibrate your intensities using known surfaces, like in this post: https://publiclab.org/questions/warren/10-27-2017/can-we-use-a-color-calibration-reference-card-to-calculate-absolute-values-for-diy-ndvi

That's why I'm able to just add 33% to the output. However, NDVI is still very valuable as a relative measure, so it's up to you if you want to take the extra steps to do an intensity calibration.

Is this a question? Click here to post it to the Questions page.


Hello, Thanks for your support. In this we have refer below link for ndvi calculation. http://ceholden.github.io/open-geo-tutorial/python/chapter_2_indices.html

So basiclly my code look like this in (Python Language)

re_img1 = cv2.imread(filename)
B, G, R = cv2.split(re_img1)
divisor = (R.astype(float) + B.astype(float))
divisor[divisor == 0] = 0.01 # Make sure we don't divide by zero!
ndvi = ((R.astype(float) - B.astype(float)) / divisor) + (1/3)
//(R-B)/(R+B)+(1/3)
print('NDVI INDEX',round(ndvi.max(),2))//NDVI INDEX MAX -1 to 1

Screenshot_(182).png

So see the image With diffrent formula and same image. So ndvi index must be (-1 to 1 ) between but in this i am getting max index from numpy array so index like (1.33,1.13,etc).

Is there any tool for ndvi index calculation.

Is this a question? Click here to post it to the Questions page.


Ah, in the infragram code, it clips anything above 1.0. But there's not a lot in the image that would be that high - just some artifacts in the lower left of the image - see the red pixels? But that's almost certainly not "real" data - just an artifact from very low dynamic range, i would guess.

Is this a question? Click here to post it to the Questions page.


Reply to this comment...


Login to comment.