使用GDAL和python计算像素值统计

Using GDAL and python to calculate statistics of pixel value

我有一个使用 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 and/or Python 提取每个字段,那么我可以这样计算:

pixel_value_0 = 1740973/(4901x2867)

并得到像素值0​​的百分比

如果使用 Python

,您可以将光栅图像转换为 Numpy 数组,然后进行计算
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()))