ComputeBandStats是否不考虑任何数据? [英] Does ComputeBandStats take nodata into account?

查看:113
本文介绍了ComputeBandStats是否不考虑任何数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算仅部分被数据覆盖的图像的统计信息.我想知道ComputeBandStats是否忽略与文件nodata值相同的像素.

I am trying to compute the stats for an image which is only partly covered by data. I would like to know if ComputeBandStats ignores the pixels with the same value as the files nodata.

这是我的代码:

inIMG = gdal.Open(infile)

# getting stats for the first 3 bands
# Using ComputeBandStats insted of stats array has min, max, mean and sd values
print "Computing band statistics"
bandas = [inIMG.GetRasterBand(b+1) for b in range(3)]
minMax = [b.ComputeRasterMinMax() for b in bandas]
meanSD = [b.ComputeBandStats(1) for b in bandas]
print minMax
print meanSD

对于没有nodata属性的图像,输出为:

For the image without the nodata attribute the output is:

Computing band statistics
[(0.0, 26046.0), (0.0, 24439.0), (0.0, 22856.0)]
[(762.9534697777777, 647.9056493556284), (767.642869, 516.0531530834181), (818.0449643333334, 511.5360132592902)]

对于nodata = 0的图像,输出为:

For the image with nodata = 0 the output is:

Computing band statistics
[(121.0, 26046.0), (202.0, 24439.0), (79.0, 22856.0)]
[(762.9534697777777, 647.9056493556284), (767.642869, 516.0531530834181), (818.0449643333334, 511.5360132592902)]

最小值和最大值已更改,不再是0,这很有意义,因为在第二个版本中,它是无数据的,因此ComputeRasterMinMax()不会考虑.但是,均值和标准差没有改变.

The min and max values have changed such that 0 is no longer min, which makes sense, because in the second version it is nodata and therefore not regarded by ComputeRasterMinMax(). However, the mean and standard deviation has not changed.

这是否意味着ComputeBandStats不会忽略nodata值?
有什么方法可以强制ComputeBandStats忽略nodata值?

Does this mean that ComputeBandStats doesn't disregard the nodata values?
Is there any way to force ComputeBandStats to disregard the nodata values?

推荐答案

设置NoData值对数据本身没有影响.您可以这样尝试:

Setting the NoData value has no effect on the data itself. You can try it this way:

# First image, all valid data
data = numpy.random.randint(1,10,(10,10))
driver = gdal.GetDriverByName('GTIFF')
ds = driver.Create("stats1.tif", 10, 10, 1, gdal.GDT_Byte)
ds.GetRasterBand(1).WriteArray(data)
print ds.GetRasterBand(1).ComputeBandStats(1)
print ds.GetRasterBand(1).ComputeStatistics(False)
ds = None

# Second image, values of "1" set to no data
driver = gdal.GetDriverByName('GTIFF')
ds = driver.Create("stats2.tif", 10, 10, 1, gdal.GDT_Byte)
ds.GetRasterBand(1).SetNoDataValue(1)
ds.GetRasterBand(1).WriteArray(data)
print ds.GetRasterBand(1).ComputeBandStats(1)
print ds.GetRasterBand(1).ComputeStatistics(False)
ds = None

请注意,ComputeBandStats返回的统计信息不变,但ComputeStatistics返回的统计信息是:

Note that the stats returned by ComputeBandStats are unchanged, but that those returned by ComputeStatistics are:

>>> (4.97, 2.451346568725035)
>>> [1.0, 9.0, 4.970000000000001, 2.4513465687250346]

>>> (4.97, 2.451346568725035)
>>> [2.0, 9.0, 5.411111111111111, 2.1750833672117]

您可以手动确认统计信息正确无误:

You can confirm manually that the stats are correct:

numpy.mean(data)
numpy.mean(data[data != 1])
numpy.std(data)
numpy.std(data[data != 1])

>>> 4.9699999999999998
>>> 5.4111111111111114
>>> 2.4513465687250346
>>> 2.1750833672117

这篇关于ComputeBandStats是否不考虑任何数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆