欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python高光譜遙感影像處理問(wèn)題詳細(xì)分析講解

 更新時(shí)間:2023年01月28日 09:28:07   作者:傾城一少  
這篇文章主要介紹了Python高光譜遙感影像處理問(wèn)題,總的來(lái)說(shuō)這并不是一道難題,那為什么要拿出這道題介紹?拿出這道題真正想要傳達(dá)的是解題的思路,以及不斷優(yōu)化探尋最優(yōu)解的過(guò)程。希望通過(guò)這道題能給你帶來(lái)一種解題優(yōu)化的思路

前言

在寫(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ì)解析

    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-10
  • python 讀取攝像頭數(shù)據(jù)并保存的實(shí)例

    python 讀取攝像頭數(shù)據(jù)并保存的實(shí)例

    今天小編就為大家分享一篇python 讀取攝像頭數(shù)據(jù)并保存的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • Python?的賦值,淺拷貝和深拷貝詳解

    Python?的賦值,淺拷貝和深拷貝詳解

    這篇文章主要為大家介紹了Python?的賦值,淺拷貝和深拷貝,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助<BR>
    2021-12-12
  • 利用Python Pygame放個(gè)煙花

    利用Python Pygame放個(gè)煙花

    Pygame是一款專(zhuān)門(mén)為開(kāi)發(fā)和設(shè)計(jì) 2D 電子游戲而生的軟件包,它支 Windows、Linux、Mac OS 等操作系統(tǒng),具有良好的跨平臺(tái)性。本文將用它放個(gè)煙花,快來(lái)一起動(dòng)手嘗試一下吧
    2022-01-01
  • 如何解決vscode下powershell終端進(jìn)入python虛擬環(huán)境venv問(wèn)題

    如何解決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-05
  • 如何一鍵升級(jí)Python所有包

    如何一鍵升級(jí)Python所有包

    這篇文章主要介紹了如何一鍵升級(jí)Python所有包,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-11-11
  • Python內(nèi)置函數(shù)——__import__ 的使用方法

    Python內(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ù)

    這篇文章主要介紹了使用python采集Excel表中某一格數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • tensorflow 大于某個(gè)值為1,小于為0的實(shí)例

    tensorflow 大于某個(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)題

    今天小編就為大家分享一篇解決torch.autograd.backward中的參數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01

最新評(píng)論