首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何迭代加载read_pixel和写入envi文件;python3

如何迭代加载read_pixel和写入envi文件;python3
EN

Stack Overflow用户
提问于 2016-04-20 19:37:55
回答 2查看 925关注 0票数 2

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

我尝试了两种不同的方法,但都不能以我想要的方式工作。

首先,我已经更新了spectral包,因为上一个版本声明不能迭代地使用envi.save_image,但我的方法仍然不起作用。其次,我的两种方法都不太适合我的double for循环--我知道--如果有人能帮我解决我的问题的话。

第一个:

代码语言:javascript
复制
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)

第一个示例没有保存图像,而是给出了错误代码

代码语言:javascript
复制
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 

第二个:

代码语言:javascript
复制
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()

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

EN

回答 2

Stack Overflow用户

发布于 2016-06-21 14:38:34

这是一个非常简短、快速和简单的解决方案,这要归功于一位同事。

代码语言:javascript
复制
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)
票数 1
EN

Stack Overflow用户

发布于 2016-04-22 17:17:12

我必须提前说一句,我不熟悉spectral软件包和envi,因此不幸的是无法提供现成的解决方案。此外,我不确定我是否正确地理解了你试图用你的形象做什么。

但是只有一些想法: for循环中的write/save函数是否会导致您的问题,因为每个像素都被以完全相同的方式处理并被覆盖?不过,我不能与IndexError产生共鸣。

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

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36742625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档