Unit 05 - Perform computation

Computation of NDVI is a well known “discipline” in remote sensing. It’s simple task which requires basic knowledge of map algebra. The key module in GRASS related to map algebra is r.mapcalc.

NDVI = (NIR - VIS) / (NIR + VIS)

To compute NDVI, the red (VIS) and near-infrared (NIR) channels are required. In the case of Sentinel-2, it refers to 4th and 8th band.

The GUI tool for map algebra can be launched from Raster ‣ Raster map calculator or by grass-raster-calculator Raster map calculator in a main toolbar.


Be aware that r.mapcalc as other raster processing modules are sensitive to computational region which needs to be set before any computation, see Computational region section for details.


Fig. 40 Compute NDVI using GUI raster map calculator by Expression. The result is defined by Output section.

Corresponding command for CLI (map names shorten):

r.mapcalc expression="ndvi = float(B08_10m - B04_10m) / ( B08_10m + B04_10m )"


Because all input raster maps are CELL type (integer) at least one sub-result must be converted to floating-point (here by float() function). Otherwise result would be also CELL map (integer: -1, 0, 1).


Fig. 41 NDVI computed by map algebra module (Jena city in red color).

Result is not perfect:

  • default color table viridis is not suitable for NDVI values
  • NDVI has been computed also in cloudy areas

The first issue can be easily fixed by r.colors (Raster ‣ Manage colors ‣ Color tables), see Color table section. The second issue requires setting a mask to ignore cloudy areas in computation. This operation can be done by r.mask (Raster ‣ Mask).

There is also specialized module for computing various vegetation indices including NDVI - i.vi. This module makes NDVI computing even simpler, and it also solves first issue since the module sets ndvi color table automatically.

At first let’s define a mask based on cloud mask vector map (note that mask is created only within current computation region).

r.mask -i vector=MaskFeature


Cloud mask provided by Sentinel products is not perfect, but for us it is a good starting point.

Then let’s recompute ndvi map with i.vi (map names shorten):

i.vi --overwrite red=B04_10m output=ndvi viname=ndvi nir=B08_10m

We can also add simple legend to map display by grass-overlay-add Add map elements (Add raster legend) from Map Display toolbar.


Fig. 42 Final NDVI result with cloud mask and simple legend.