栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

(Python)使用Gdal进行批量的影像RPC正射校正

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

(Python)使用Gdal进行批量的影像RPC正射校正

使用Python+Gdal进行批量的影像RPC正射校正

文章目录
  • 使用Python+Gdal进行批量的影像RPC正射校正
    • 摘要
    • 方法
      • 实验数据
    • 实现
      • 代码介绍
      • 完整代码
    • 实验结果
      • 代码运行结果
      • 正射校正结果
      • 性能

摘要

遥感影像经常会遇到大批量数据做统一处理的情况,而其影像命名由于其一级产品的生产过程有很强的规律性,天然具备批量处理的潜质。

本次针对带RPC的遥感影像批量处理,借助了Python脚本语言,使用re库(正则表达式)来处理字符串与名称搜索、os库来处理路径和文件遍历、GDAL库来进行RPC模型的正射校正,最终实现了批量正射的效果。

使用的Python及其处理库具有在window和linux等各种平台下具备易部署、易调试、易编译、易移植的特点。

方法 实验数据

数据组织如下,常见的一级带rpc的产品(rpb亦可),注意一定遥感影像要带有同名“_RPC”或者“_rpc”的有理多项式模型文件:

    目录: D:欧比特2single6


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2021/11/6     23:10                ortho
-a----         2021/11/4     18:43            777 imagelist.txt
-a----         2021/11/5     16:00       51176986 HHZ3_20201114005237_0007_L1B_B06_CMOS2.tif
-a----         2021/11/5     16:00       51176986 HHZ3_20201114005237_0008_L1B_B06_CMOS2.tif
-a----         2021/11/5     16:00       51176986 HHZ3_20201114005237_0009_L1B_B06_CMOS2.tif
-a----         2021/11/5     16:00       51176986 HHZ3_20201114005237_0010_L1B_B06_CMOS1.tif
-a----         2021/11/5     16:01       51176986 HHZ3_20201114005237_0010_L1B_B06_CMOS3.tif
-a----         2021/11/5     16:01       51176986 HHZ3_20201114005237_0011_L1B_B06_CMOS1.tif
-a----         2021/11/5     16:01       51176986 HHZ3_20201114005237_0011_L1B_B06_CMOS3.tif
-a----         2021/11/3     16:15           6036 HHZ3_20201114005237_0007_L1B_B06_CMOS2_rpc.txt
-a----         2021/11/3     16:15           6036 HHZ3_20201114005237_0008_L1B_B06_CMOS2_rpc.txt
-a----         2021/11/3     16:15           6036 HHZ3_20201114005237_0009_L1B_B06_CMOS2_rpc.txt
-a----         2021/11/3     16:15           6035 HHZ3_20201114005237_0010_L1B_B06_CMOS1_rpc.txt
-a----         2021/11/3     16:15           6037 HHZ3_20201114005237_0010_L1B_B06_CMOS3_rpc.txt
-a----         2021/11/3     16:15           6035 HHZ3_20201114005237_0011_L1B_B06_CMOS1_rpc.txt
-a----         2021/11/3     16:15           6037 HHZ3_20201114005237_0011_L1B_B06_CMOS3_rpc.txt

实现 代码介绍

主要分为三个函数:

  1. 遍历文件夹的遥感影像并生成待处理图像列表和正射之后存放的位置。
  2. 进行批量的正射的组织。
  3. 使用GDAL进行RPC矫正的封装。
完整代码
import os
import re
import numpy as np
from osgeo import gdal
import time

def RPCrect(input,output):
    """
    使用
    :param input:输入原始影像
    :param output:输出正射影像
    """
    dataset = gdal.Open(input, gdal.GA_Update)#读入影像
    rpc = dataset.Getmetadata("RPC")#读入影像,rpc
    dst_ds = gdal.Warp(output, dataset, dstSRS='EPSG:4326',
                       xRes=9.0909090909090909090909090909091e-5,#x方向正射影像分辨率
                       yRes=09.0909090909090909090909090909091e-5, #y方向正射影像分辨率
                       resampleAlg=gdal.GRIORA_Bilinear,#插值方式,默认为最最邻近,我们一般使用双线性
                       rpc=True, #使用RPC模型进行校正
                       transformerOptions=[r'RPC_DEM=E:全球SRTM_90mglobal']#参考DEM
                       )

def PatchGeoRectRPC(dst_list,ortho_list):
    """
    批量的进行正射
    :param dst_list: 待处理的影像列表
    :param ortho_list: 输出正射影像的正射影像列表
    """
    image_dst_list = np.loadtxt(dst_list,'str')
    image_ortho_list = np.loadtxt(ortho_list, 'str')
    tif_tile = re.compile(r'.tif')
    for dst,ortho in zip(image_dst_list,image_ortho_list):
        dst = dst.replace('n', '')
        ortho = ortho.replace('n', '')
        RPCrect(dst,ortho)
        print("orthoed the input image.")
    print("Patch ortho finished.")

def GetOrthoImageList(image_dir):
    """
    自动读取影像,获取原始影像列表,建立正射影像的输出位置
    :param image_dir: 待处理影像的文件夹
    """
    image_dir_ortho = image_dir + r'ortho'
    isExists = os.path.exists(image_dir_ortho)
    # 判断结果
    if not isExists:
        os.makedirs(image_dir_ortho)
        print(image_dir_ortho+' 创建成功')
    else:
        # 如果目录存在则不创建,并提示目录已存在
        print(image_dir_ortho + ' existed')
    tif_tile = re.compile(r'.tif')
    dst_file = image_dir_ortho+ r"image_list.txt"
    ortho_file =  image_dir_ortho + r"image_ortho_list.txt"
    f_dst = open(dst_file, 'w')
    f_ortho = open(ortho_file, 'w')
    gDirPath = os.walk(image_dir)  # 文件名迭代器
    for root, dirs, files in gDirPath:
        if files:
            for file in files:
                mo =  tif_tile.search(file)
                if mo:
                    image_dst = root + '\' + file
                    img_ortho = tif_tile.sub(r'_ortho.tif',file)
                    image_ortho = root + '\ortho\' + img_ortho
                    f_dst.write(image_dst+'n')
                    f_ortho.write(image_ortho + 'n')
    print("Image list created.")

if __name__ == '__main__':

    image_dir = r"D:欧比特2single28"
    GetOrthoImageList(image_dir)
    dst_list = image_dir + r"orthoimage_list.txt"
    ortho_list = image_dir + r"orthoimage_ortho_list.txt"

    start = time.time()#统计运行时间
    PatchGeoRectRPC(dst_list, ortho_list)
    end = time.time()
    print("正射校正运行时间:%.2f秒" % (end - start))
实验结果 代码运行结果
Image list created.
orthoed the input image.
orthoed the input image.
orthoed the input image.
orthoed the input image.
orthoed the input image.
orthoed the input image.
orthoed the input image.
Patch ortho finished.
正射校正运行时间:55.08秒

正射校正结果

性能

在商务本Thinkpad上,校正一张在数秒到十几秒不等,考虑到易用性,性能的要求可以略微宽容;在主机或者性能本上应该具有更好的性能表现。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/460949.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号