Python高光譜遙感影像處理問(wèn)題詳細(xì)分析講解
前言
在寫(xiě)波段配準(zhǔn)相關(guān)代碼時(shí)經(jīng)常需要用到tif影像的波段合成和分解,雖然可以用ENVI才處理,但是每次都要打開(kāi)再設(shè)置一些參數(shù)有些麻煩,所以本著“獨(dú)立自主、自力更生”的原則就寫(xiě)了些腳本來(lái)處理這個(gè)需求。又寫(xiě)了個(gè)批量裁剪影像的腳本。這里簡(jiǎn)單總結(jié)歸納一下。
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 = [] # 對(duì)于tif文件,統(tǒng)一用gdal打開(kāi)并輸出為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) # 對(duì)于所有其它類(lèi)型的文件,如jpg、png等,統(tǒng)一用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.")
這里簡(jiǎn)單介紹下代碼。經(jīng)過(guò)波段配準(zhǔn)后,不同波段的影像已經(jīng)實(shí)現(xiàn)了對(duì)齊,所以通過(guò)讀取各波段影像然后利用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] # 對(duì)于tif文件,統(tǒng)一用gdal打開(kāi)并輸出為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__()) # 對(duì)于所有其它類(lèi)型的文件,如jpg、png等,統(tǒng)一用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.")
波段拆分與波段合并相反,直接讀取一個(gè)多波段的tif影像,然后依次保存各波段數(shù)據(jù)為單獨(dú)文件即可。
3.影像裁剪
在之前,要想實(shí)現(xiàn)影像裁剪的功能需要借助ENVI等軟件,但是ENVI等打開(kāi)比較慢,還要各種設(shè)置,比較麻煩。所以直接寫(xiě)了個(gè)腳本來(lái)方便地實(shí)現(xiàn)功能
# 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]) # 對(duì)于tif文件,統(tǒng)一用gdal打開(kāi)并輸出為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) # 對(duì)于所有其它類(lèi)型的文件,如jpg、png等,統(tǒng)一用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.")
影像裁剪實(shí)現(xiàn)也相對(duì)簡(jiǎn)單,就是通過(guò)設(shè)置讀取影像范圍即可實(shí)現(xiàn)對(duì)指定區(qū)域的裁剪。
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) # 對(duì)于tif文件,統(tǒng)一用gdal打開(kāi)并輸出為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.') # 對(duì)于所有其它類(lèi)型的文件,如jpg、png等,統(tǒng)一用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.")
相比于單張影像裁剪,批量裁剪就是多加了個(gè)循環(huán),實(shí)現(xiàn)了批量操作,也比較簡(jiǎn)單。
到此這篇關(guān)于Python高光譜遙感影像處理問(wèn)題詳細(xì)分析講解的文章就介紹到這了,更多相關(guān)Python高光譜遙感影像處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中request庫(kù)的各種用法詳細(xì)解析
本文詳細(xì)介紹了Python的requests庫(kù)的安裝與使用,包括HTTP請(qǐng)求方法、請(qǐng)求頭、請(qǐng)求體的基本概念,以及發(fā)送GET和POST請(qǐng)求的基本用法,同時(shí),探討了會(huì)話(huà)對(duì)象、處理重定向、超時(shí)設(shè)置、代理支持等高級(jí)功能,幫助讀者更高效地處理復(fù)雜的HTTP請(qǐng)求場(chǎng)景,需要的朋友可以參考下2024-10-10python 讀取攝像頭數(shù)據(jù)并保存的實(shí)例
今天小編就為大家分享一篇python 讀取攝像頭數(shù)據(jù)并保存的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08如何解決vscode下powershell終端進(jìn)入python虛擬環(huán)境venv問(wèn)題
這篇文章主要介紹了如何解決vscode下powershell終端進(jìn)入python虛擬環(huán)境venv問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05Python內(nèi)置函數(shù)——__import__ 的使用方法
本篇文章主要介紹了Python內(nèi)置函數(shù)——__import__ 的使用方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11使用python采集Excel表中某一格數(shù)據(jù)
這篇文章主要介紹了使用python采集Excel表中某一格數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05tensorflow 大于某個(gè)值為1,小于為0的實(shí)例
這篇文章主要介紹了tensorflow 大于某個(gè)值為1,小于為0的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06解決torch.autograd.backward中的參數(shù)問(wèn)題
今天小編就為大家分享一篇解決torch.autograd.backward中的參數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01