[GIS] Permanent Histogram Stretch with QGIS or FWTools

fwtoolshistogramqgis

Is there a way to permanently stretch a histogram of a raster within QGIS or gdal? I would like to stretch a satellite image with a Standard Deviation 2.5 stretch. I would prefer to use QGIS or FWTools, but I am open to other open source tools.

Thanks…

Best Answer

A great and easy way to stretch the histogram of a raster seems to be gdal_contrast_stretch:

https://github.com/gina-alaska/dans-gdal-scripts/wiki/Gdal-contrast-stretch

Installation guide: https://github.com/gina-alaska/dans-gdal-scripts

Then you can use it as

gdal_contrast_stretch -linear-stretch <target_average value> 2.5
 <input-raster.tif> <stretched-raster.tif>

That is, you also have to indicate the target average value. Supposing you only have one band, a (not so elegant but working) way on Unix to find out the mean (saved in a variable $mean) is:

mean=$(gdalinfo -stats <input-raster.tif> |grep Mean|cut -f4 -d "="|sed 's/, StdDev//g')

Hope this helps!