Python高光譜遙感影像處理問(wèn)題詳細(xì)分析講解
前言
在寫波段配準(zhǔn)相關(guān)代碼時(shí)經(jīng)常需要用到tif影像的波段合成和分解,雖然可以用ENVI才處理,但是每次都要打開再設(shè)置一些參數(shù)有些麻煩,所以本著“獨(dú)立自主、自力更生”的原則就寫了些腳本來(lái)處理這個(gè)需求。又寫了個(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打開并輸出為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ì)于所有其它類型的文件,如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打開并輸出為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ì)于所有其它類型的文件,如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等打開比較慢,還要各種設(shè)置,比較麻煩。所以直接寫了個(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打開并輸出為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ì)于所有其它類型的文件,如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打開并輸出為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ì)于所有其它類型的文件,如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)文章
Django項(xiàng)目的初步創(chuàng)建與簡(jiǎn)單配置
本文主要介紹了Django項(xiàng)目的初步創(chuàng)建與簡(jiǎn)單配置,詳細(xì)介紹了如何安裝和配置Django,包括創(chuàng)建項(xiàng)目、數(shù)據(jù)庫(kù)配置、路由等,通過(guò)本文可以了解如何使用Django創(chuàng)建自己的Web應(yīng)用程序2023-09-09
Pycharm遠(yuǎn)程連接服務(wù)器跑代碼的實(shí)現(xiàn)
本文主要介紹了Pycharm遠(yuǎn)程連接服務(wù)器跑代碼的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Python使用背景差分器實(shí)現(xiàn)運(yùn)動(dòng)物體檢測(cè)
目前,許多運(yùn)動(dòng)檢測(cè)技術(shù)都是基于簡(jiǎn)單的背景差分概念的,因此本文將基于背景差分器(MOG背景差分器和KNN背景差分器)來(lái)實(shí)現(xiàn)運(yùn)動(dòng)物體的檢測(cè),感興趣的可以了解一下2022-02-02
Python實(shí)現(xiàn)免費(fèi)音樂(lè)下載器
本文主要為大家介紹了通過(guò)Python實(shí)現(xiàn)的免費(fèi)音樂(lè)下載器,文中的示例代碼講解詳細(xì),對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的小伙伴可以學(xué)習(xí)一下2021-12-12
python正則表達(dá)式查找和替換內(nèi)容的實(shí)例詳解
在本篇文章里小編給大家整理的是一篇關(guān)于python正則表達(dá)式查找和替換內(nèi)容的實(shí)例詳解內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)參考下。2021-10-10
如何在 Matplotlib 中更改繪圖背景的實(shí)現(xiàn)
這篇文章主要介紹了如何在 Matplotlib 中更改繪圖背景的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Pandas之read_csv()讀取文件跳過(guò)報(bào)錯(cuò)行的解決
這篇文章主要介紹了Pandas之read_csv()讀取文件跳過(guò)報(bào)錯(cuò)行的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
pyppeteer執(zhí)行js繞過(guò)webdriver監(jiān)測(cè)方法下
這篇文章主要為大家介紹了pyppeteer上執(zhí)行js并繞過(guò)webdriver監(jiān)測(cè)常見方法的上篇,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04

