Products QGIS Scripts Canopy NDVI Median Calculator
Canopy NDVI Median Calculator

QGIS Script · Python

Canopy NDVI Median Calculator

Computes median NDVI per polygon using only canopy pixels — not bare soil, paths, or gaps within the plot boundary. Standard zonal statistics average everything inside the polygon and give misleading health readings wherever ground is visible. This script uses a Canopy Mask to sample only the tree pixels, giving you a figure you can actually act on.

$20 USD One-time purchase · Instant download
Buy on Gumroad

What you get

canopy_ndvi_median_calc.py
Free updates via Gumroad

What it does

Canopy-only NDVI sampling

Samples NDVI values from within each polygon but only at pixels where the canopy mask equals 1 — ignoring bare soil, paths, and open areas.

Median per polygon

Computes the median of the canopy-only NDVI values within each feature — more robust to outliers than mean.

New NDVI_median field

Writes the result directly as a new NDVI_median attribute on each polygon in the output GeoJSON.

Works with any polygon layer

Processes plot polygons, field boundaries, orchard blocks — any polygon layer that overlaps the NDVI and canopy mask rasters.

Requires: Canopy Mask Generator

This script needs a canopy_mask raster layer in your QGIS project before it will run. Generate it from your DSM and DTM using the Canopy Mask Generator — takes under a minute and only needs to be done once per survey.

Get the mask

How to use it

  1. 1

    Run generate_canopy_mask.py first to create the canopy_mask layer.

  2. 2

    Load your polygon plot layer and click it to make it the active layer.

  3. 3

    Check (tick) the canopy_mask raster and your NDVI raster in the Layers panel.

  4. 4

    Open Plugins → Python Console → Show Editor.

  5. 5

    Paste the script and click Run.

Requirements

  • QGIS 3.28 or newer
  • numpy, rasterio — bundled with QGIS
  • Active polygon layer (plot zones)
  • canopy_mask raster (from generate_canopy_mask.py)
  • NDVI raster — layer name contains "ndvi"

Also in the Vegetation & Field Analysis Pack

Get all 6 vegetation scripts for $40 — save $20 vs buying individually.

View bundle