Cast Sentinel-2 Uint16 datatype before NDVI calculation (Beginners guide #5)
whisperingpixel opened this issue · 0 comments
whisperingpixel commented
The Beginners Guide Notebook 5 calculates NDVI using the Sentinel-2 Uint16 data type that does not support negative values: https://github.com/digitalearthafrica/deafrica-sandbox-notebooks/blob/main/Beginners_guide/05_Basic_analysis.ipynb
Therefore, we don't obtain the negative NDVI output values; instead, they become positive and have very high NDVI values, e.g., the water areas:
If we cast the values to float (before the subtraction and division), we obtain the negative values in the NDVI array.
Just as an example:
# Calculate the components that make up the NDVI calculation
nir = dataset.nir.astype(float)
red = dataset.red.astype(float)
band_diff = nir - red
band_sum = nir + red
# Calculate NDVI and store it as a measurement in the original dataset
ndvi = band_diff / band_sum