简体   繁体   English

如何迭代加载 read_pixel 并写入到 vi 文件中; 蟒蛇3

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

I want to load hyperspectral data per pixel into an array and write this pixel out again using Python 3.5.我想将每个像素的高光谱数据加载到一个数组中,然后使用 Python 3.5 再次写出这个像素。 I want to calculate something with the spectral information of this Pixel.我想用这个 Pixel 的光谱信息计算一些东西。

I have tried two different ways and both don't work the way I want.我尝试了两种不同的方法,但都不能按我想要的方式工作。

First of all I have updated spectral package since the last version was stated not to work with iteratively envi.save_image but still my approach does not work.首先,我已经更新了光谱包,因为上一个版本被声明不能迭代地使用env.save_image,但我的方法仍然不起作用。 Second my approaches both are not very good with my double for loop - I know - If anyone could please help me on my problem.其次,我的方法对我的双循环都不是很好 - 我知道 - 如果有人可以帮助我解决我的问题。

1st:第一:

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)

1st example does not save the image rather gives me the error code第一个示例不保存图像而是给了我错误代码

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 

2nd:第二:

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

The second example saves the image but stores the Pixel in a different order and messes up my image.第二个示例保存图像,但以不同的顺序存储像素并弄乱了我的图像。

So this is the very short, fast and easy solution thanks to a colleague.多亏了一位同事,这是一个非常简短、快速和简单的解决方案。

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)

I have to say in advance that I am not familiar with the spectral package and envi and therefore unfortunately cannot offer a ready-to-use solution.我必须事先声明,我不熟悉光谱包和环境,因此很遗憾无法提供现成的解决方案。 Besides, I am not sure if I correctly understood what you are trying to do with your image.此外,我不确定我是否正确理解您要对图像进行的操作。

But just some thoughts: Could the write/save function inside the for loop cause your problem, because every pixel is treated in the exact same way and it gets overwritten?但只是一些想法:for 循环中的 write/save 函数是否会导致您的问题,因为每个像素都以完全相同的方式处理并被覆盖? I can not relate to the IndexError though.不过,我无法与 IndexError 联系起来。

Maybe you need a function where you can rather write a certain pixel to an empty image passing also i and j.也许您需要一个函数,您可以将某个像素写入空图像,同时传递 i 和 j。 A second option could be to save each pixel in an array and save it to an image at once after the for loop.第二种选择可能是将每个像素保存在一个数组中,并在 for 循环后立即将其保存到图像中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM