In a previous post, I thought I'd cracked a problem running a script with a batch NDVI calculation on some 16bit unsigned ENVI .hdr data cubes:

from qgis.analysis import QgsRasterCalculatorEntry, QgsRasterCalculator
from qgis.core import QgsProject
lddLrs = lddLrs = [l for l in QgsProject().instance().mapLayers().values() if isinstance(l, QgsRasterLayer)]
    path = "/desired/output/location"
    for lyr in lddLrs:
        entries = []
        r = QgsRasterCalculatorEntry()
        ir = QgsRasterCalculatorEntry()
        r.ref = 'r@121'
        ir.ref = 'ir@192'
        r.raster = lyr
        ir.raster = lyr
        r.bandNumber = 121
        ir.bandnumber = 192
        entries.append( r )
        entries.append( ir )
        calc = QgsRasterCalculator( '(ir@192 - r@121) / (ir@192 + r@121)', path + lyr.name() + "_c3_ndvi.tif", 'GTiff', lyr.extent(), lyr.width(), lyr.height(), entries )

However, the outputs are all dark with low resolution and the statistics are off. I'm thinking there some settings in the calculation that need instruction but lost as to what it might be?

If I run the same NDVI calculation in the QGIS calculator GUI I get a clear image. No adjustments in the symbology seem to help.

  • Are your input layers of the same bit depth? What are the minimum and maximum values in your output raster?
    – Techie_Gus
    Jul 14, 2020 at 3:17
  • Hi, thanks... yeah important detail. AFIK they're all 16 bit unsigned going in. They're ENVI .bil .hdr hyperspectral cubes. But got 32bit floating point in my outputs?
    – Ricardo
    Jul 14, 2020 at 10:08
  • Try setting your output to 8 bit
    – Techie_Gus
    Jul 14, 2020 at 10:18


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.