I am trying to calculate NDVI using python and landsat imagery. I am able to calculate ndvi using the following equation:

def ndvi(sat, fp):
    raster = rasterio.open(fp)
    if sat.lower() == 'l8':
       red = raster.read(4)
       nir = raster.read(5)
    if sat.lower() == 'l5':
       red = raster.read(3)
       nir = raster.read(4)
    red = red.astype('float64')
    nir = nir.astype('float64')
    ndvi = (nir - red) /(nir + red)
    display(ndvi, 'Greens')
def display(img, ramp):
    plt.figure(figsize=(200, 200), dpi=80, facecolor='w', edgecolor='k')
    plt.imshow(img, cmap=ramp)

This produce an image that looks like: enter image description here The image appears to show the inverse of what should be shown. The areas in the rectangles are golf courses that are shown with negative ndvi and the highways which are circled are displayed with a high ndvi. I know I could multiple everything by -1 and get the correct image but I would like to know why this happens. Here is the corresponding image processed in GEE for reference: enter image description here

  • 1
    Works as expected for me with a random Landsat 8 image. However, I get similar to your results if I accidentally on purpose use Landsat 8 B3 & B4 as the R & NIR bands. Perhaps somewhere in your logic you've specified the satellite ("sat.lower()" as "l5" instead of "l8". ? Can you edit your question with more details about your script. – user2856 Dec 2 '18 at 22:51
  • @Luke I updated the code above with the full functions. The script takes a file path to a tiff and you specify whether your using landsat five or landsat 8 and it display the image using matplotlib – Taylor Dec 2 '18 at 23:40
  • Can you also provide more info about how you specify in your code what the satellite is? And some details about the data (i.e, where you got it from, processing level, filename, datatype etc. perhaps you could provide the output of a rio info or gdalinfo report on your raster). – user2856 Dec 2 '18 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.