很多時候都需要改變已經影像的分辨率,這裏自己動手研究了一下相關原理,並進行了實現,以後可以很方便地改變影像的分辨率。
影像重採樣核心
重採樣的核心是影像的座標範圍不變,改變影像像元的大小,來實現像元個數的增減,即分辨率的改變。
像元的面積*像元個數=固定值
像元大小變爲原來的1/2時,影像的像元數量變爲原來的四倍。
代碼實現
from osgeo import gdal, gdalconst
import os
import numpy as np
def resampling(source_file, target_file, scale=1.0):
"""
影像重採樣
:param source_file: 源文件
:param target_file: 輸出影像
:param scale: 像元縮放比例
:return:
"""
dataset = gdal.Open(source_file, gdalconst.GA_ReadOnly)
band_count = dataset.RasterCount # 波段數
if band_count == 0 or not scale > 0:
print("參數異常")
return
cols = dataset.RasterXSize # 列數
rows = dataset.RasterYSize # 行數
cols = int(cols * scale) # 計算新的行列數
rows = int(rows * scale)
geotrans = list(dataset.GetGeoTransform())
print(dataset.GetGeoTransform())
print(geotrans)
geotrans[1] = geotrans[1] / scale # 像元寬度變爲原來的scale倍
geotrans[5] = geotrans[5] / scale # 像元高度變爲原來的scale倍
print(geotrans)
if os.path.exists(target_file) and os.path.isfile(target_file): # 如果已存在同名影像
os.remove(target_file) # 則刪除之
band1 = dataset.GetRasterBand(1)
data_type = band1.DataType
target = dataset.GetDriver().Create(target_file, xsize=cols, ysize=rows, bands=band_count,
eType=data_type)
target.SetProjection(dataset.GetProjection()) # 設置投影座標
target.SetGeoTransform(geotrans) # 設置地理變換參數
total = band_count + 1
for index in range(1, total):
# 讀取波段數據
print("正在寫入" + str(index) + "波段")
data = dataset.GetRasterBand(index).ReadAsArray(buf_xsize=cols, buf_ysize=rows)
out_band = target.GetRasterBand(index)
out_band.SetNoDataValue(dataset.GetRasterBand(index).GetNoDataValue())
out_band.WriteArray(data) # 寫入數據到新影像中
out_band.FlushCache()
out_band.ComputeBandStats(False) # 計算統計信息
print("正在寫入完成")
del dataset
del target
if __name__ == "__main__":
source_file = r"E:\商丘yx\相交4.tiff"
target_file = r"E:\商丘yx\相交411.tiff"
resampling(source_file, target_file, scale=1.1)
target_file = r"E:\商丘yx\相交05.tiff"
resampling(source_file, target_file, scale=0.5)
結果展示
改變前
增大分辨率後
減小分辨率後
不適用場景
經過多輪測試對比發現,在以下情況時使用GDAL進行重採樣會出現採樣後的影像旋轉、大小不一致的問題:
- 影像未定義座標系
- 影像大小爲0.0001這種特別小的情況