发布时间:2024-06-04 18:01
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 argparse
def 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*std
v_min = avg-2.5*std
norm = plt.Normalize(v_min, v_max)
colors = colorList
cm = 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.inputPath
colors=args.colorList.split(\',\')
outPath=args.outPath
flag=args.flag
extension=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 extension
positional 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)
pythonopencv检测行人_python+openCV利用摄像头实现人员活动检测
IDEA中web项目打成war包并在本地tomcat部署(超细版)
Flutter 开发一个 GitHub 客户端 | 掘金技术征文
Pycharm使用Database Navigator连接mysql数据库全过程
图解Transformer模型(Multi-Head Attention)
OpenCV学习笔记(二)——颜色空间(不同颜色空间转换函数cv.cvtColor()、多通道分离函数cv.split()、多通道合并函数cv.merge())