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年影像一致。

tiff 影像拼接 python 代码 python影像处理_tiff 影像拼接 python 代码