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

相关文章

Docker镜像修改hosts及dockerfile修改hosts文件的实现方式

《Docker镜像修改hosts及dockerfile修改hosts文件的实现方式》:本文主要介绍Docker镜像修改hosts及dockerfile修改hosts文件的实现方式,具有很好的参考价... 目录docker镜像修改hosts及dockerfile修改hosts文件准备 dockerfile 文

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定