GDAL实现标准差拉伸渲染影像

2024-04-12 09:18

本文主要是介绍GDAL实现标准差拉伸渲染影像,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

import os
from sys import path
from osgeo import gdal
from osgeo import osr
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mpc
import argparsedef stdStretch(imgFile, colorList, flag, outPath):# imgFile 影像# colorList 色带参数# flag=True 将数据投影到3857坐标系进行显示# flag=False 将数据按原坐标系进行显示gdal.AllRegister()ds = gdal.Open(imgFile)if flag:dsrs = osr.SpatialReference()dsrs.ImportFromEPSG(3857)vrt_ds = gdal.AutoCreateWarpedVRT(ds, None, dsrs.ExportToWkt(), gdal.GRA_Bilinear)band = gdal.Dataset.GetRasterBand(vrt_ds, 1)else:band = gdal.Dataset.GetRasterBand(ds, 1)array = gdal.Band.ReadAsArray(band)avg = np.nanmean(array)std = np.nanstd(array)# 默认按2.5倍标准差进行拉伸v_max = avg+2.5*stdv_min = avg-2.5*stdnorm = plt.Normalize(v_min, v_max)colors = colorListcm = mpc.LinearSegmentedColormap.from_list('colormap', colors, 512)plt.imshow(array, cm, norm)plt.axis('off')plt.subplots_adjust(bottom=0, top=1, left=0, right=1, hspace=0, wspace=0)plt.colorbar()outname = os.path.basename(imgFile)outname = outname.replace(outname[outname.rindex('.'):], '.png')outname = os.path.join(outPath, outname)plt.savefig(outname, transparent=True, dpi=100)plt.close()# 封装为命令行工具 批量处理目录下的影像
if __name__ == '__main__':parser = argparse.ArgumentParser('标准差拉伸渲染影像')parser.add_argument('inputPath', type=str, help='The directory  stores the images or a image file to process!')parser.add_argument('colorList', type=str,help='Color map used in render!')parser.add_argument('outPath', type=str,help='The directory stores the result!')parser.add_argument('flag', type=bool, help='A flag which indicates whether the image should be prjected to epsg3857!')parser.add_argument('extension',type=str,help='The extension of image file to be processed!',default=None)args=parser.parse_args()inputPath=args.inputPathcolors=args.colorList.split(',')outPath=args.outPathflag=args.flagextension=args.extension# extension=None 则处理目录下的所有影像if os.path.isfile(inputPath):# 处理单张影像stdStretch(inputPath,colors,flag,outPath)else:# 处理整个目录imgFiles=os.listdir(inputPath)for imgFile in imgFiles:if not(extension) or imgFile.endswith(extension):stdStretch(os.path.join(inputPath,imgFile),colors,flag,outPath)# colors = '#0000FF, #3B7FFF, #00FFFF,#B4FF91, #FFFF00, #FF9100, #FF0000'print('finished!')
调用方式
user@DESKTOP-IFIB8V0 MINGW64 /i/毕业设计整理/LST
$ python color.py --help
usage: 标准差拉伸渲染影像 [-h] inputPath colorList outPath flag extensionpositional arguments:inputPath   The directory stores the images or a image file to process!colorList   Color map used in render!outPath     The directory stores the result!flag        A flag which indicates whether the image should be prjected to epsg3857!extension   The extension of image file to be processed!optional arguments:-h, --help  show this help message and exit
(base) 
渲染结果

在这里插入图片描述
在这里插入图片描述

这篇关于GDAL实现标准差拉伸渲染影像的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/896720

相关文章

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

Python与MySQL实现数据库实时同步的详细步骤

《Python与MySQL实现数据库实时同步的详细步骤》在日常开发中,数据同步是一项常见的需求,本篇文章将使用Python和MySQL来实现数据库实时同步,我们将围绕数据变更捕获、数据处理和数据写入这... 目录前言摘要概述:数据同步方案1. 基本思路2. mysql Binlog 简介实现步骤与代码示例1

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的

基于C#实现PDF转图片的详细教程

《基于C#实现PDF转图片的详细教程》在数字化办公场景中,PDF文件的可视化处理需求日益增长,本文将围绕Spire.PDFfor.NET这一工具,详解如何通过C#将PDF转换为JPG、PNG等主流图片... 目录引言一、组件部署二、快速入门:PDF 转图片的核心 C# 代码三、分辨率设置 - 清晰度的决定因

Java Kafka消费者实现过程

《JavaKafka消费者实现过程》Kafka消费者通过KafkaConsumer类实现,核心机制包括偏移量管理、消费者组协调、批量拉取消息及多线程处理,手动提交offset确保数据可靠性,自动提交... 目录基础KafkaConsumer类分析关键代码与核心算法2.1 订阅与分区分配2.2 拉取消息2.3

SpringBoot集成XXL-JOB实现任务管理全流程

《SpringBoot集成XXL-JOB实现任务管理全流程》XXL-JOB是一款轻量级分布式任务调度平台,功能丰富、界面简洁、易于扩展,本文介绍如何通过SpringBoot项目,使用RestTempl... 目录一、前言二、项目结构简述三、Maven 依赖四、Controller 代码详解五、Service

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

GO语言短变量声明的实现示例

《GO语言短变量声明的实现示例》在Go语言中,短变量声明是一种简洁的变量声明方式,使用:=运算符,可以自动推断变量类型,下面就来具体介绍一下如何使用,感兴趣的可以了解一下... 目录基本语法功能特点与var的区别适用场景注意事项基本语法variableName := value功能特点1、自动类型推

基于Python实现自动化邮件发送系统的完整指南

《基于Python实现自动化邮件发送系统的完整指南》在现代软件开发和自动化流程中,邮件通知是一个常见且实用的功能,无论是用于发送报告、告警信息还是用户提醒,通过Python实现自动化的邮件发送功能都能... 目录一、前言:二、项目概述三、配置文件 `.env` 解析四、代码结构解析1. 导入模块2. 加载环

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp