首页 > 编程语言 >Python|使用Python实现图像的重采样

Python|使用Python实现图像的重采样

时间:2022-08-18 20:12:45浏览次数:60  
标签:采样 Python referencefile 影像 图像 outputfilePath gdal

基础知识

图像重采样就是从高分辨率遥感影像中提取出低分辨率影像,或者从低分辨率影像中提取高分辨率影像的过程。常用的方法有最邻近内插法、双线性内插法、三次卷积法等。很多时候,我们下载到的数据精度是不一致,因而,将数据进行重采样就非常重要了。在ArcGIS中使用该工具,需要点击很多次,比较麻烦。因此使用Python+GDAL实现了该功能,后续也可以将其改为批量重采样。避免了繁琐的操作。

代码实现

代码注释比较详细,也比较简单,直接看代码就可以,代码如下:

from osgeo import gdal


def resample_images(referencefilePath, inputfilePath, outputfilePath):  # 影像重采样
   """
   :param referencefilePath: 重采样参考文件路径
   :param inputfilePath: 输入路径
   :param outputfilePath: 输出路径
   """
   # 获取参考影像信息, 其实可以自定义这些信息,有参考的话就不用查这些参数了
   referencefile = gdal.Open(referencefilePath, gdal.GA_ReadOnly)
   referencefileProj = referencefile.GetProjection()
   referencefiletrans = referencefile.GetGeoTransform()
   bandreferencefile = referencefile.GetRasterBand(1)
   width = referencefile.RasterXSize
   height = referencefile.RasterYSize
   bands = referencefile.RasterCount
   # 获取输入影像信息
   inputrasfile = gdal.Open(inputfilePath, gdal.GA_ReadOnly)  # 打开输入影像
   inputProj = inputrasfile.GetProjection()  # 获取输入影像的坐标系
   # 创建重采样输出文件(设置投影及六参数)
   driver = gdal.GetDriverByName('GTiff')  # 这里需要定义,如果不定义自己运算会大大增加运算时间
   output = driver.Create(outputfilePath, width, height, bands, bandreferencefile.DataType)  # 创建重采样影像
   output.SetGeoTransform(referencefiletrans)  # 设置重采样影像的仿射矩阵为参考面的仿射矩阵
   output.SetProjection(referencefileProj)  # 设置重采样影像的坐标系为参考面的坐标系
   # 参数说明 输入数据集、输出文件、输入投影、参考投影、重采样方法(最邻近内插\双线性内插\三次卷积等)、回调函数
   gdal.ReprojectImage(inputrasfile, output, inputProj, referencefileProj, gdal.GRA_Bilinear, 0.0, 0.0, )


if __name__ == "__main__":
   inputfilePath = 'D:\RS_Toolbox\DJKS_dem_12.5m.tif'  # 输入文件
   outputfilePath = 'D:\RS_Toolbox\DJKS_dem_2m.tif'  # 输出文件
   referencePath = 'D:\RS_Toolbox\image.tif'  # 重采样参考文件
   resample_images(referencePath, inputfilePath, outputfilePath)

代码运行结果

重采样前后对比如下:

         

标签:采样,Python,referencefile,影像,图像,outputfilePath,gdal
From: https://www.cnblogs.com/tangjielin/p/16599414.html

相关文章

  • MySQL多表查询与python操作MySQL
    一、navicateNavicate是一套可创建多个连接的数据库管理工具,用以方便管理 MySQL、Oracle、PostgreSQL、SQLite、SQLServer、MariaDB 和 MongoDB 等不同类型的数据库......
  • python 中实现DNA一致性序列计算
     001、root@PC1:/home/test#lsa.fastatest.pyroot@PC1:/home/test#cata.fasta##测试数据>Rosalind_1ATCCAGCT>Rosalind_2GGGCAACT>Rosalin......
  • Python list methods All In One
    PythonlistmethodsAllInOnePython3#!/usr/bin/envpython3#coding=utf-8__author__='xgqfrms'__editor__='vscode'__version__='1.0.1'__copyrigh......
  • python菜鸟学习: 9. 文件操作
    #-*-coding:utf-8-*-importsys,time#读文件:一次性读取所有内容#r=readf=open("singe.txt",'r',encoding="utf-8").read()print(f)#写文件,覆盖原来的文件#w=wr......
  • python 读取.pkl.gz文件
    1importpandasaspd2importsix.moves.cPickleascPickle3importgzip45filePath='./a/data.pkl.gz'6f=gzip.open(filePath,'rb')7df=pd.D......
  • Python进阶篇02-函数
    一、常规函数的定义函数函数就是一段组织好的、可以重复使用的代码块。比如在你写的某个.py文件中,需要频繁的计算面积,这样我们会有很多相似、重复的代码,在这种情况下,我......
  • Python 创建虚拟环境
    桌面建立一个文件夹,假设为tor打开cmdcddesktopcdtor假设python没有  pipenv包,则pip3installpipenv假设tor文件夹内没有虚拟环境,即没有pipfiel及p......
  • python中输出两条长度一致序列碱基不同的个数
     001、方法1root@PC1:/home/test#lstest.pyroot@PC1:/home/test#cattest.py##测试程序#!/usr/bin/pythonstr1="GAGCCTACTAACGGGAT"......
  • Python报错:ImportError cannot import name 'imresize'
    原文链接Python出现错误:ImportError:cannotimportname'imresize'解决方案首先安装pillow:pipinstallpillow然后安装scipy早期版本。高级版scipy本不再......
  • Python 数据类型及转换
    Python数据类型及转换说明:Python每一个变量在使用前都要赋值,每个变量都有一个数据类型,之前说的type()函数可以查看变量的类型。Python常见的数据类型有:Number(数字)、Strin......