Python高光谱遥感影像处理问题详细分析讲解
作者:倾城一少 发布时间:2023-10-04 04:03:37
标签:Python,高光谱,遥感影像
前言
在写波段配准相关代码时经常需要用到tif影像的波段合成和分解,虽然可以用ENVI才处理,但是每次都要打开再设置一些参数有些麻烦,所以本着“独立自主、自力更生”的原则就写了些脚本来处理这个需求。又写了个批量裁剪影像的脚本。这里简单总结归纳一下。
1.波段合并
# coding=utf-8
import sys
import cv2
import functions as fun
import os
if __name__ == '__main__':
if sys.argv.__len__() >= 2:
if sys.argv[1] == 'help' or sys.argv[1] == 'HELP':
print("Function description:")
print("Join several bands into one file.")
print("\nUsage instruction:")
print("example.exe [img_dir] [img_type] [out_path]")
print("[img_dir]:The input dir that contains band data.")
print("[img_type]:The file type of band data,tif or png etc.")
print("[out_path]:The filename of joined image.")
print("Please note that these band data should have same height and width.")
print("\nUsage example:")
print("Tool_JoinBands.exe C:\\tif tif C:\\tifout\\joined.tif")
os.system('pause')
else:
img_dir = sys.argv[1]
img_type = sys.argv[2]
out_path = sys.argv[3]
paths, names, files = fun.findAllFiles(img_dir, img_type)
bands_data = []
# 对于tif文件,统一用gdal打开并输出为tif文件
if img_type.endswith('tif') or img_type.endswith('TIF') or img_type.endswith('TIFF') or img_type.endswith(
'tiff'):
for i in range(files.__len__()):
band_data = fun.readTifImage(files[i])
bands_data.extend(band_data)
print("joined " + (i + 1).__str__() + " bands.")
print(bands_data.__len__().__str__() + " bands in total.")
fun.writeTif(bands_data, out_path)
# 对于所有其它类型的文件,如jpg、png等,统一用OpenCV处理
else:
for i in range(files.__len__()):
band_data = cv2.imread(files[i], cv2.IMREAD_GRAYSCALE)
bands_data.append(band_data)
print("Open image success.")
data = cv2.merge((bands_data[0], bands_data[1], bands_data[2]))
cv2.imwrite(out_path, data)
print("Save image success.")
else:
print("Unknown mode, input 'yourExeName.exe help' to get help information.")
这里简单介绍下代码。经过波段配准后,不同波段的影像已经实现了对齐,所以通过读取各波段影像然后利用GDAL叠加即可。
2.波段拆分
# coding=utf-8
import sys
import os
import cv2
import functions as fun
if __name__ == '__main__':
if sys.argv.__len__() >= 2:
if sys.argv[1] == 'help' or sys.argv[1] == 'HELP':
print("Function description:")
print("Separate and save different band data in one image file.")
print("\nUsage instruction:")
print("example.exe [img_path] [out_dir]")
print("[img_path]:The filename of input image.")
print("[output_dir]:The output dir for different band images.")
print("\nUsage example:")
print("Tool_SeparateBands.exe C:\\tif\\input.tif C:\\tifout")
os.system('pause')
else:
img_path = sys.argv[1]
output_dir = sys.argv[2]
# 对于tif文件,统一用gdal打开并输出为tif文件
if img_path.endswith('tif') or img_path.endswith('TIF') or img_path.endswith('TIFF') or img_path.endswith(
'tiff'):
bands_data = fun.readTifImage(img_path)
for i in range(bands_data.__len__()):
fun.writeTif([bands_data[i]], output_dir + os.path.sep + "band_" + i.__str__().zfill(2) + ".tif")
print("saved " + (i + 1).__str__() + "/" + bands_data.__len__().__str__())
# 对于所有其它类型的文件,如jpg、png等,统一用OpenCV处理
else:
img = cv2.imread(img_path)
print("Open image success.")
band_b, band_g, band_r = cv2.split(img)
cv2.imwrite(output_dir + os.path.sep + "band_b.png", band_b)
cv2.imwrite(output_dir + os.path.sep + "band_g.png", band_g)
cv2.imwrite(output_dir + os.path.sep + "band_r.png", band_r)
print("Save image success.")
else:
print("Unknown mode, input 'yourExeName.exe help' to get help information.")
波段拆分与波段合并相反,直接读取一个多波段的tif影像,然后依次保存各波段数据为单独文件即可。
3.影像裁剪
在之前,要想实现影像裁剪的功能需要借助ENVI等软件,但是ENVI等打开比较慢,还要各种设置,比较麻烦。所以直接写了个脚本来方便地实现功能
# coding=utf-8
import sys
import cv2
import functions as fun
import os
if __name__ == '__main__':
if sys.argv.__len__() >= 2:
if sys.argv[1] == 'help' or sys.argv[1] == 'HELP':
print("Function description:")
print("Select and cut the ROI(region of interest) in a big image file.")
print("\nUsage instruction:")
print("example.exe [img_path] [out_path] [start_x] [start_y] [x_range] [y_range]")
print("[img_path]:The filename of input image.")
print("[out_path]:The filename of output image.")
print("[start_x]:The x coordinate of ROI's left-top point in big image.")
print("[start_y]:The y coordinate of ROI's left-top point in big image.")
print("[x_range]:The range of ROI in x direction(width).")
print("[y_range]:The range of ROI in y direction(height).")
print("\nUsage example:")
print("Tool_ResizeIMG.exe C:\\tif\\input.tif C:\\tifout\\roi.tif 100 200 3000 4000")
os.system('pause')
else:
img_path = sys.argv[1]
out_path = sys.argv[2]
start_x = int(sys.argv[3])
start_y = int(sys.argv[4])
x_range = int(sys.argv[5])
y_range = int(sys.argv[6])
# 对于tif文件,统一用gdal打开并输出为tif文件
if img_path.endswith('tif') or img_path.endswith('TIF') or img_path.endswith('TIFF') or img_path.endswith(
'tiff'):
bands_data = fun.readTifImageWithWindow(img_path, start_x, start_y, x_range, y_range)
fun.writeTif(bands_data, out_path)
# 对于所有其它类型的文件,如jpg、png等,统一用OpenCV处理
else:
bands_data = cv2.imread(img_path)
print("Open image success.")
bands_data_roi = bands_data[start_y:start_y + y_range, start_x:start_x + x_range, :]
cv2.imwrite(out_path, bands_data_roi)
print("Save image success.")
else:
print("Unknown mode, input 'yourExeName.exe help' to get help information.")
影像裁剪实现也相对简单,就是通过设置读取影像范围即可实现对指定区域的裁剪。
4.批量影像裁剪
# coding=utf-8
import sys
import cv2
import functions as fun
import os
if __name__ == '__main__':
if sys.argv.__len__() >= 2:
if sys.argv[1] == 'help' or sys.argv[1] == 'HELP':
print("Function description:")
print("Select and cut the ROI(region of interest) in big image files(Batch mode).")
print("\nUsage instruction:")
print("example.exe [img_dir] [img_type] [output_dir] [start_x] [start_y] [x_range] [y_range]")
print("[img_dir]:The input dir that contains band data.")
print("[img_type]:The file type of band data,tif or png etc.")
print("[output_dir]:The output dir for ROI images.")
print("[start_x]:The x coordinate of ROI's left-top point in big image.")
print("[start_y]:The y coordinate of ROI's left-top point in big image.")
print("[x_range]:The range of ROI in x direction(width).")
print("[y_range]:The range of ROI in y direction(height).")
print("\nUsage example:")
print("Tool_ResizeIMG_Batch.exe C:\\tif tif C:\\tifout 100 200 3000 4000")
os.system('pause')
else:
img_dir = sys.argv[1]
img_type = sys.argv[2]
out_dir = sys.argv[3]
start_x = int(sys.argv[4])
start_y = int(sys.argv[5])
x_range = int(sys.argv[6])
y_range = int(sys.argv[7])
paths, names, files = fun.findAllFiles(img_dir, img_type)
# 对于tif文件,统一用gdal打开并输出为tif文件
if img_type.endswith('tif') or img_type.endswith('TIF') or img_type.endswith('TIFF') or img_type.endswith(
'tiff'):
for i in range(files.__len__()):
bands_data = fun.readTifImageWithWindow(files[i], start_x, start_y, x_range, y_range)
fun.writeTif(bands_data, out_dir + os.path.sep + names[i][:names[i].rfind('.')] + "_cut.tif")
print("cutting " + (i + 1).__str__() + "/" + files.__len__().__str__())
print('cut finished.')
# 对于所有其它类型的文件,如jpg、png等,统一用OpenCV处理
else:
for i in range(files.__len__()):
bands_data = cv2.imread(files[i])
bands_data_roi = bands_data[start_y:start_y + y_range, start_x:start_x + x_range, :]
cv2.imwrite(out_dir + os.path.sep + "band_" + (i + 1).__str__().zfill(2) + ".jpg", bands_data_roi)
print("cutting " + (i + 1).__str__() + "/" + files.__len__())
print('cut finished.')
else:
print("Unknown mode, input 'yourExeName.exe help' to get help information.")
相比于单张影像裁剪,批量裁剪就是多加了个循环,实现了批量操作,也比较简单。
来源:https://blog.csdn.net/u010329292/article/details/128712909


猜你喜欢
- 1、块级作用域想想此时运行下面的程序会有输出吗?执行会成功吗?#块级作用域if 1 == 1: name = "lzl"
- 目录Python的安装VS Code配置Hello World测试调用Python函数string.split()第一次尝试第二次尝试第三次
- 目录结构:testtest/index.phptest/test_zip.ziptest/test_zip<span style=&q
- 本文实例讲述了Python基于正则表达式实现文件内容替换的方法。分享给大家供大家参考,具体如下:最近因为有一个项目需要从普通的服务器移植到S
- 代码之余,将代码过程重要的一些代码段备份一下,如下的代码内容是关于Python从ftp服务器下载文件的的代码,希望能对小伙伴有用途。#cod
- 编写思路:把本地文件在客户端通过base64编码以后发送目的地.测试过程中,上传文件过大,导致超时不成功,后来经过改善.把编码分
- 本文只是几年前学习的tkinter的时候写的测试程序,十分之简陋,只是学习用,没什么其他用处。学习一下莫烦Python的tkinter教程,
- 本节笔者主将要介绍Active Server Page的另一对象Response,Response对象的作用是在向浏览器发送数据。将Resp
- 工欲善其事,必先利其器。作为更专业的前端工程师,我们需要强劲的IDE协助我们写出规范、美观、漂亮的JavaScript代码,首先要作的就是对
- 最近折腾索引引擎以及数据统计方面的工作比较多, 与 Python 字典频繁打交道, 至此整理一份此方面 API 的用法与坑法备案. 
- 本文实例为大家分享了opencv实现回形遍历像素算法的具体代码,供大家参考,具体内容如下代码实现# -*- coding:utf-8 -*-
- 本文实例为大家分享了python实现抽奖小程序的具体代码,供大家参考,具体内容如下设计一个抽奖服务 背景:有x个奖品,要求在y天内
- 今天谈一下关于python中input的一些基本用法(写给新手入门之用,故只谈比较实用的部分)。首先,我们可以看一下官方文档给我们的解释(在
- 放大镜并不是一个难以实现的效果, 只是因为牵涉到一些精确的数值计算, 显得比较繁琐. 在未来的一段日子, 我会不定期地写关于 JavaScr
- 目录uni-app 介绍html部分js部分创建实例开始录音结束录音播放录音暂停播放提交录音到后端重新录制onLoad部分计时器数据部分un
- python 使用get_argument获取url query参数ornado的每个请求处理程序,我们叫做handler,handler里
- EasyUI 简介easyui是一种基于jQuery的用户界面插件集合。easyui为创建现代化,互动,JavaScript应用程序,提供必
- root账户为MySQL的超级管理员用户,拥有MySQL提供的所有权限。我们登录了root账户可以重置其它创建的所有用户的密码,那么root
- 于是我登陆主机的管理面板,重启了网站服务,ldb文件就消失了,网站打开也正常了,但过了数小时后,网站又再次出现以上情况,此种情况持续了好一段
- 今天在运行登录远程桌面的时候发现了这个提示关于 “ MySQL Installer is running in Community mode