基于MATLAB与Python计算长时间遥感栅格图像的像元值变异系数(CV)
生活随笔
收集整理的這篇文章主要介紹了
基于MATLAB与Python计算长时间遥感栅格图像的像元值变异系数(CV)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Matlab代碼如下:
更改路徑以及年份即可。
?Python代碼:
from osgeo import gdal import numpy as np import copy # 計算變異系數 images = [r'E:\test\pre\2001year_mean.tif',r'E:\test\pre\2002year_mean.tif',r'E:\test\pre\2003year_mean.tif',r'E:\test\pre\2004year_mean.tif',r'E:\test\pre\2005year_mean.tif', ] def coefficient_of_variation(data): # 變異系數mean = np.mean(data) # 計算平均值std = np.std(data, ddof=0) # 計算標準差cv = std/meanreturn cv # 柵格圖像組計算變異系數 def CV(images, outpath):images_pixels = [] # 存放多個圖像像元矩陣的空數組for image in images:tif = str(image)open_tif = gdal.Open(tif) # 打開柵格圖像band = open_tif.ReadAsArray() # 獲取波段的矩陣images_pixels.append(band) # 把圖像的矩陣加入到數組中CV = copy.deepcopy(images_pixels[0]) # 獲取一個矩陣作為要寫入的模板# 讀取像元,并計算變異系數for i in range(len(CV)):for j in range(len(CV[1])):CV_data = [] # 存放ij坐標下像元值的數組,以計算變異系數for px in range(len(images)): # 遍歷多個圖像下ij坐標的像元值CV_data.append(images_pixels[px][i][j]) # 同一坐標的多點加入數組,以計算變異系數CV_value = coefficient_of_variation(CV_data) # 變異系數計算CV[i][j] = CV_value # 寫入該坐標下的變異系數# 保存柵格圖像gtiff_driver = gdal.GetDriverByName('GTiff')out_tif = gtiff_driver.Create(outpath, CV.shape[1], CV.shape[0], 1, gdal.GDT_Float32)# 將數據坐標投影設置為原始坐標投影out_tif.SetProjection(open_tif.GetProjection())out_tif.SetGeoTransform(open_tif.GetGeoTransform())out_band = out_tif.GetRasterBand(1)out_band.WriteArray(CV)out_band.FlushCache()print('柵格圖像組變異系數計算完成') CV(images, r'E:\test\pre\cv.tif')總結
以上是生活随笔為你收集整理的基于MATLAB与Python计算长时间遥感栅格图像的像元值变异系数(CV)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c# 使用谷歌身份验证GoogleAut
- 下一篇: 2022-2027年中国环保领域集团级s