簡體   English   中英

如何迭代加載 read_pixel 並寫入到 vi 文件中; 蟒蛇3

[英]How to iteratively load read_pixel and write to envi file; python3

我想將每個像素的高光譜數據加載到一個數組中,然后使用 Python 3.5 再次寫出這個像素。 我想用這個 Pixel 的光譜信息計算一些東西。

我嘗試了兩種不同的方法,但都不能按我想要的方式工作。

首先,我已經更新了光譜包,因為上一個版本被聲明不能迭代地使用env.save_image,但我的方法仍然不起作用。 其次,我的方法對我的雙循環都不是很好 - 我知道 - 如果有人可以幫助我解決我的問題。

第一:

myfile=open_image('input.hdr')
    for i in range(0,myfile.shape[0]):
        for j in range(0,myfile.shape[1]):
            mypixel = (myfile.read_pixel(i,j))
            envi.save_image('output.hdr', mypixel, dtype=np.int16)

第一個示例不保存圖像而是給了我錯誤代碼

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "/usr/local/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 88, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "/dtc/Python/Masking.py", line 132, in <module>
envi.save_image('test.hdr', mypixel, dtype=np.int16)#, metadata=myfile.metadata)
File "/usr/local/lib/python3.5/site-packages/spectral/io/envi.py", line 415, in save_image
data, metadata = _prepared_data_and_metadata(hdr_file, image, **kwargs)
File "/usr/local/lib/python3.5/site-packages/spectral/io/envi.py", line 568, in _prepared_data_and_metadata
add_image_info_to_metadata(image, metadata)
File "/usr/local/lib/python3.5/site-packages/spectral/io/envi.py", line 613, in add_image_info_to_metadata
metadata['samples'] = image.shape[1]
IndexError: tuple index out of range 

第二:

myfile=open_image('input.hdr')
envi.create_image('test.hdr',ext='.bip', interleave='bip',dtype='h',force=True,metadata=myfile.metadata)
open('test.bip', 'w').close() # empties the  created file

file = open('test.bip', 'ab')#ab #opens the created file for appending the new bands

for i in range(0,myfile.shape[0]):
    for j in range(0,myfile.shape[1]):
        mypixel = (myfile.read_pixel(i,j))
        file.write(mypixel) 
file.close()
myfile.close()

第二個示例保存圖像,但以不同的順序存儲像素並弄亂了我的圖像。

多虧了一位同事,這是一個非常簡短、快速和簡單的解決方案。

myfile=envi.open('input.hdr') #opens image for calculating with it

    imageArray = 10000*myfile[:,:,:] #do some math with it; 

    #10000* is needed because input data are thresholded between {0;10000} 
    #and during processing get thresholded between {0;1}. 
    #For preventing 0 in the output with datatype int the thresholding to {0;10000} is necessary again

envi.save_image('test.hdr',imageArray,dtype=np.int16,metadata=myfile.metadata,force=True)

我必須事先聲明,我不熟悉光譜包和環境,因此很遺憾無法提供現成的解決方案。 此外,我不確定我是否正確理解您要對圖像進行的操作。

但只是一些想法:for 循環中的 write/save 函數是否會導致您的問題,因為每個像素都以完全相同的方式處理並被覆蓋? 不過,我無法與 IndexError 聯系起來。

也許您需要一個函數,您可以將某個像素寫入空圖像,同時傳遞 i 和 j。 第二種選擇可能是將每個像素保存在一個數組中,並在 for 循環后立即將其保存到圖像中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM