Python实现多幅图像栅格值均值计算
本文介绍使用Python和GDAL库批量计算多幅TIFF图像对应栅格平均值的实现方法。通过遍历指定文件夹内的图像序列,读取栅格数据并累加求和,最终生成包含每年平均值的单幅TIFF文件。代码示例展示了文件读写、数组运算及投影信息保留等关键步骤,适用于遥感影像的年度合成处理。
Python实现多幅图像栅格值均值计算
本文分享一个使用Python计算多幅图像栅格值平均值的实例,供读者参考。
需要说明的是,该方法并非最优解,因为ARCGIS等软件已提供现成的相关函数。本程序仅作为技术参考。
以下是程序的具体说明。
数据源位于文件夹E://work//EVI_Data_tif,包含2000年至2010年某地区的EVI图像数据,每年包含13幅图像。处理目标是将每年13幅图像的对应栅格值相加后求平均,生成对应年份的TIFF文件。例如,将2000年的13幅图像处理生成2000.tif,其栅格值为原13幅图像对应位置栅格值的平均值。结果保存至E:\work\result目录。源文件命名规则以2000年为例,依次为20006.tif、20007.tif、20008.tif直至200018.tif。
import os
import os.path
import gdal
import sys
from gdalconst import *
from osgeo import gdal
import osr
import numpy as np
#coding=utf-8
def WriteGTiffFile(filename, nRows, nCols, data,geotrans,proj, noDataValue, gdalType):#向磁盘写入结果文件
format = "GTiff"
driver = gdal.GetDriverByName(format)
ds = driver.Create(filename, nCols, nRows, 1, gdalType)
ds.SetGeoTransform(geotrans)
ds.SetProjection(proj)
ds.GetRasterBand(1).SetNoDataValue(noDataValue)
ds.GetRasterBand(1).WriteArray(data)
ds = None
def File():#遍历文件,读取数据,算出均值
rows,cols,geotransform,projection,noDataValue = Readxy('E://work//EVI_Data_tif//20006.tif')
#获取源文件的行,列,投影等信息,所有的源文件这些信息都是一致的
print 'rows and cols is ',rows,cols
filesum = [[0.0]*cols]*rows #栅格值和,二维数组
average= [[0.0]*cols]*rows# 存放平均值,二维数组
filesum=np.array(filesum)#转换类型为np.array
average = np.array(average)
print 'the type of filesum',type(filesum)
count=0
rootdir = 'E:\work\EVI_Data_tif'
for dirpath,filename,filenames in os.walk(rootdir):#遍历源文件
for filename in filenames:
if os.path.splitext(filename)[1] == '.tif':#判断是否为tif格式
filepath = os.path.join(dirpath,filename)
purename = filename.replace('.tif','') #获得除去扩展名的文件名,比如201013.tif,purename为201013
if purename[:4] == '2010': #判断年份
filedata = [[0.0]*cols]*rows
filedata = np.array(filedata)
filedata = Read(filepath) #将2010年的13幅图像数据存入filedata中
count+=1
np.add(filesum,filedata,filesum) #求13幅图像相应栅格值的和
#print str(count)+'this is filedata',filedata
print 'count is ',count
for i in range(0,rows):
for j in range(0,cols):
if(filesum[i,j]==noDataValue*count): #处理图像中的noData
average[i,j]=-9999
else:
average[i,j]=filesum[i,j]*1.0/count #求平均
WriteGTiffFile("E:\\work\\result\\2010.tif", rows, cols, average,geotransform,projection, -9999, GDT_Float32) #写入结果文件
def Readxy(RasterFile): #读取每个图像的信息
ds = gdal.Open(RasterFile,GA_ReadOnly)
if ds is None:
print 'Cannot open ',RasterFile
sys.exit(1)
cols = ds.RasterXSize
rows = ds.RasterYSize
band = ds.GetRasterBand(1)
data = band.ReadAsArray(0,0,cols,rows)
noDataValue = band.GetNoDataValue()
projection=ds.GetProjection()
geotransform = ds.GetGeoTransform()
return rows,cols,geotransform,projection,noDataValue
def Read(RasterFile):#读取每个图像的信息
ds = gdal.Open(RasterFile,GA_ReadOnly)
if ds is None:
print 'Cannot open ',RasterFile
sys.exit(1)
cols = ds.RasterXSize
rows = ds.RasterYSize
band = ds.GetRasterBand(1)
data = band.ReadAsArray(0,0,cols,rows)
return data
if __name__ == "__main__":
print"ok1"
File()
print"ok2"
以上就是本文的全部内容,希望能为读者提供有益的帮助,欢迎大家继续支持本站。
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









