I want to know the basic stats (max, min, mean, std) of my raster file. I use two different methods and get similar, but different results. I assume the results should be exactly the same with any method, is that right? What am I doing wrong?
Let' use this single band raster as test file.
Method 1 (gdal on python)
from osgeo import gdal
myRaster = 'path/to/input.tiff'
gtif = gdal.Open(myRaster)
srcband = gtif.GetRasterBand(1)
stats = srcband.GetStatistics(True, True)
print ('Minimum: %.3f\nMaximum: %.3f\nMean: %.3f\nStDev: %.3f' % (stats[0], stats[1], stats[2], stats[3]))
With Method 1 I get the following values:
Minimum: -63.630
Maximum: 132.033
Mean: 37.191
StDev: 42.380
Method 2 (gdalinfo from command line)
I simply use the command
gdalinfo input.tiff -stats
I get the whole stats including:
Minimum=-63.630
Maximum=147.225
Mean=33.218
StdDev=37.375
This means that both methods result in the same Minimum value (-63.630 in both cases), but differ in the Maximum value (with concomitant differences in Mean and Standard Deviation). I read somewhere that there can be some problems with how to treat NaN values, but I am not sure this is the problem here.
Edit 00: I just tried the same using rasterio
and it gives exactly the same values (for all 4 stats) as the command-line gdalinfo
unsetstats
option. That way it is sure thatgdalinfo -stats
is computing the statistics from the pixel data. Maybe the gdalinfo option-mm
has the same effect but I am not sure gdal.org/programs/gdalinfo.html#cmdoption-gdalinfo-mm.