Recommended Posts


I am an engineering student working on a project based on NDVI calculation to monitor the crop health. I used the PiNoIR camera with blue filter for my experiment in order to obtain the values of NIR and Red region. I used the following code to extract the required values and to calculate the NDVI. But in the output image, the empty regions (area where no leaves are present as shown in the below figure) and ground have higher NDVI values. The shadowed regions are shown in the range 0.5 to 0.6. I wanted to know whether the output is correct and what corrections can be done in the -code in order to correct the error. The code is given below.

from PIL import Image

import numpy as np

import cv2

from cv2 import imread

from matplotlib import cm

rgb_matrix =cv2.imread('inputimg.jpg')

w=rgb_matrix.shape[1]    #columns

h=rgb_matrix.shape[0]    #rows



#Compute ndvi values for each pixel



for i in range(h):


 for j in range(w):






if(den == 0):







#based on NDVI values, give different colors for easier identification

for i in range(h):

    for j in range(w):

        if(res[j] >=-1 and res[j] <0):

            rgb_matrix[j]=[128,128,128]   #grey

        elif(res[j]>=0 and res[j]<0.2):

            rgb_matrix[j]=[64,255,0]    #parrot green

        elif(res[j]>=0.2 and res[j]<0.3):

            rgb_matrix[j]=[125,255,255]    #yellow

        elif(res[j]>=0.3 and res[j]<0.4):

            rgb_matrix[j]=[0,128,128]   #dark green

        elif(res[j]>=0.4 and res[j]<0.5):

            rgb_matrix[j]=[255,255,0]     #sky blue

        elif(res[j]>=0.5 and res[j]<0.6):

            rgb_matrix[j]=[255,51,153]    #purple

        elif(res[j]>=0.6 and res[j]<0.7):

            rgb_matrix[j]=[0,128,255]    #orange

        elif(res[j]>=0.7 and res[j]<0.8):

            rgb_matrix[j]=[255,43,255]   #pink

        elif(res[j]>=0.8 and res[j]<0.9):

            rgb_matrix[j]=[40,40,255]      #red


            rgb_matrix[j]=[255,0,0]       #dark blue



(Ignore the indentation errors)












Edited by Harshitha G S
add images
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.