GDAL+Python實現柵格影像處理之重採樣

GDAL+Python實現柵格影像處理之重採樣


由於項目需要,所以使用到了GDAL框架,項目中未使用到GDAL關於圖像處理部分的算法接口,所以近期學習總結一下。GDAL支持Python、c++、c、c# 、java。其中接口大同小異,主要是學習其中思路和方法,此處採用Python編寫代碼實現該功能。

重採樣概念

重採樣是從高分辨率遙感影像中提取出低分辨率影像的過程。
常用的方法有最鄰近內插法、雙線性內插法、三次卷積法等

使用方法

  • 方法1
    python中我們可以使用gdal.ReprojectImage()進行重採樣。其中官網地址如下,如圖所示:
    在這裏插入圖片描述
    可以查看其具體用法,也可以在pycharm中查看具體參數設置。
    參數說明(未列完):
參數 說明
Dataset src_ds 輸入數據集
Dataset dst_ds 輸出文件
GDALResampleAlg eResampleAlg 重採樣方法(最鄰近內插\雙線性內插\三次卷積等)
GDALProgressFunc 回調函數
char const * src_wkt=None 輸入投影
char const * dst_wkt=None 參考投影

代碼實現

outputfilePath = 'G:/studyprojects/gdal/GdalStudy/Files/images/ReprojectImage.tif'
inputfilePath='G:/studyprojects/gdal/GdalStudy/Files/images/2016CHA.tif'
referencefilefilePath='G:/studyprojects/gdal/GdalStudy/Files/images/2018CHA.tif'
def ReprojectImages():
    # 獲取輸出影像信息
    inputrasfile = gdal.Open(inputfilePath, gdal.GA_ReadOnly)
    inputProj = inputrasfile.GetProjection()
    # 獲取參考影像信息
    referencefile = gdal.Open(referencefilefilePath, gdal.GA_ReadOnly)
    referencefileProj = referencefile.GetProjection()
    referencefileTrans = referencefile.GetGeoTransform()
    bandreferencefile = referencefile.GetRasterBand(1)
    Width= referencefile.RasterXSize
    Height = referencefile.RasterYSize
    nbands = referencefile.RasterCount
    # 創建重採樣輸出文件(設置投影及六參數)
    driver = gdal.GetDriverByName('GTiff')
    output = driver.Create(outputfilePath, Width,Height, nbands, bandreferencefile.DataType)
    output.SetGeoTransform(referencefileTrans)
    output.SetProjection(referencefileProj)
    # 參數說明 輸入數據集、輸出文件、輸入投影、參考投影、重採樣方法(最鄰近內插\雙線性內插\三次卷積等)、回調函數
    gdal.ReprojectImage(inputrasfile, output, inputProj, referencefileProj, gdalconst.GRA_Bilinear,0.0,0.0,)
  • 方法2
    同時也可以使用gdal.Warp()方法進行重採樣。
    參數詳解(未列完):
參數 說明
srcSRS 源座標系統
dstSRS 目標座標系統
resampleAllg 重採樣方法
multeThread 多線程
cutLineDSname 裁剪mask矢量數據集名字
format 輸出格式 eg GTIFF
cutLineLayername 裁剪mask圖層名
cutLinewhere 裁剪where語句
def ReprojectImages2():
    # 若採用gdal.Warp()方法進行重採樣
    # 獲取輸出影像信息
    inputrasfile = gdal.Open(inputfilePath, gdal.GA_ReadOnly)
    inputProj = inputrasfile.GetProjection()
    # 獲取參考影像信息
    referencefile = gdal.Open(referencefilefilePath, gdal.GA_ReadOnly)
    referencefileProj = referencefile.GetProjection()
    referencefileTrans = referencefile.GetGeoTransform()
    bandreferencefile = referencefile.GetRasterBand(1)
    x = referencefile.RasterXSize
    y = referencefile.RasterYSize
    nbands = referencefile.RasterCount
    # 創建重採樣輸出文件(設置投影及六參數)
    driver = gdal.GetDriverByName('GTiff')
    output = driver.Create(outputfilePath, x, y, nbands, bandreferencefile.DataType)
    output.SetGeoTransform(referencefileTrans)
    output.SetProjection(referencefileProj)
    options = gdal.WarpOptions(srcSRS=inputProj, dstSRS=referencefileProj, resampleAlg=gdalconst.GRA_Bilinear)
    gdal.Warp(output, inputfilePath, options=options)

效果展示

圖中可以看出我將16年分類影像經過重採樣後跟18年影像一致。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章