【问题标题】:How to iteratively load read_pixel and write to envi file; python3如何迭代加载read_pixel并写入env文件;蟒蛇3
【发布时间】:2016-08-13 01:29:55
【问题描述】:

我想将每个像素的高光谱数据加载到一个数组中,并使用 Python 3.5 再次写出这个像素。我想用这个像素的光谱信息计算一些东西。

我尝试了两种不同的方法,但都没有按照我想要的方式工作。

首先,我已经更新了光谱包,因为上一个版本被声明不能迭代地使用 envi.save_image,但我的方法仍然不起作用。 其次,我的方法对于我的双 for 循环都不是很好 - 我知道 - 如果有人可以帮助我解决我的问题。

第一个:

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()

第二个示例保存了图像,但以不同的顺序存储像素并弄乱了我的图像。

【问题讨论】:

    标签: python spectral envi spectral-python


    【解决方案1】:

    我必须提前说我不熟悉光谱包和环境,因此很遗憾无法提供现成的解决方案。此外,我不确定我是否正确理解了您要对图像执行的操作。

    但只是一些想法:for 循环中的写入/保存函数是否会导致您的问题,因为每个像素都以完全相同的方式处理并被覆盖?我无法与 IndexError 联系起来。

    也许你需要一个函数,你可以将某个像素写入一个空图像,同时传递 i 和 j。第二种选择可能是将每个像素保存在一个数组中,并在 for 循环之后立即将其保存到图像中。

    【讨论】:

    • 感谢您的回答。我已经尝试过了,也从同事那里得到了这个提示。这奏效了,最终速度更快。仍然没有给我预期的结果。输出是只有零数据的图像。
    【解决方案2】:

    感谢一位同事,这是一个非常简短、快速且简单的解决方案。

    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)
    

    【讨论】:

      猜你喜欢
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 1970-01-01
      • 1970-01-01
      • 2016-11-02
      • 2019-04-18
      • 1970-01-01
      相关资源
      最近更新 更多