A program compute NDVI from MODIS/HDF4 file

Just finished a c++ program to compute NDVI from MODIS/HDF4 file

Written in c++, depends on GDAL(Geospatial Data Abstraction Library)

Source code can be downloaded HERE


Before compiling, make sure you have make, g++, gdal installed.

go to the hdf2ndvi directory, type:


you’ll see

g++  -c hdf2ndvi.cpp
g++  -c enhance_gdal.cpp
g++  -o hdf2ndiv hdf2ndvi.o enhance_gdal.o -lgdal

and then it’s successfully complied.


##dealing with a single file

./hdf2ndiv filename.hdf

and you’ll see:

computing filename.hdf...

after finished, you’ll get filename.hdf_NDVI.tiff in the same folder

dealing with multiple files

If the files to be computed in the same folder.
You can type:

./hdf2ndvi *.hdf

If you want to compute all the files in the folders and subfolders,
you can use xargs:

find . -name ".hdf" -print0 | xargs -0 ./hdf2ndvi

change band used

This program defaultly regard band0(band is counted from 0) as Red Band, and regard band1 as NIR(near infrared red) band. If you want to change the default value, for instance, set band-4 as NIR band and band-0 as Red Band, you can do like this:

./hdf2ndvi filename.hdf -red 0 -nir 4

and you’ll get file: filename.hdf_NDVI.tiff computed from band4 and band0


The NDVI is computed as following:

but some pixel value of MODIS data is negative,
so when meeting negative value in

ifthe program set NDVI = 1,
else ifthe program set NDVI = -1.


邮箱地址不会被公开。 必填项已用*标注