Python高光谱遥感影像处理问题详细分析讲解
倾城一少 人气:0前言
在写波段配准相关代码时经常需要用到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.")
相比于单张影像裁剪,批量裁剪就是多加了个循环,实现了批量操作,也比较简单。
加载全部内容