使用GDAL和python计算像素值的统计信息

时间:2018-06-21 11:58:50

标签: percentage static-analysis gdal

我有一个使用GDAL创建的Geotiff图像。 我想知道是否有可能使用GDAL和Python(或仅其中之一)提取特定值的像素百分比。

特别是,如果我这样做:

gdalinfo -hist input.tif

我获得了所有元数据信息,尤其是

Size is 4901, 2867
...
Band 1 Block=4901x1 Type=Byte, ColorInterp=Palette
  Minimum=0.000, Maximum=5.000, Mean=2.263, StdDev=1.135
0...10...20...30...40...50...60...70...80...90...100 - done.
  256 buckets from -0.5 to 255.5:
  1740973 365790 6385650 3688110 1757506 113138 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

有没有一种方法可以计算直方图中定义的6个值的像素百分比?

.tif文件的大小为4901x2867,因此,如果我可以使用GDAL和/或Python提取每个字段,那么我可以计算出以下内容:

pixel_value_0 = 1740973/(4901x2867)

并获得像素值0的百分比

1 个答案:

答案 0 :(得分:1)

您可以将栅格图像转换为Numpy数组,然后使用Python进行计算

from collections import Counter
from osgeo import gdal_array

# Read raster data as numeric array from file
rasterArray = gdal_array.LoadFile('RGB.byte.tif')

# The 3rd band
band3 = rasterArray[2]
# Flatten the 2D array to 1D and count occurrences of each values
# Then simple to get the stat for a pixel value in particular
print(Counter(band3.flatten()))