gpt4 book ai didi

python - 在 python 中读取卫星图像文件时优化性能

转载 作者:太空狗 更新时间:2023-10-30 02:34:37 24 4
gpt4 key购买 nike

我有一张以波段交错像素 (BIP) 格式存储的多波段卫星图像以及一个单独的头文件。头文件提供了图像中的行数和列数、波段数(可以超过标准的 3 个)等详细信息。

图像本身是这样存储的(假设是 5 波段图像):

[B1][B2][B3][B4][B5][B1][B2][B3][B4][B5] ... 等等(基本上是 5 个字节 - 每个波段一个 - 对于每个像素从图像的左上角开始)。

我需要在 Python 3.2(在 Windows 7 64 位上)中将这些波段中的每一个分离为 PIL 图像,目前我认为我正在错误地处理这个问题。我目前的代码如下:

def OpenBIPImage(file, width, height, numberOfBands):
"""
Opens a raw image file in the BIP format and returns a list
comprising each band as a separate PIL image.
"""
bandArrays = []
with open(file, 'rb') as imageFile:
data = imageFile.read()
currentPosition = 0
for i in range(height * width):
for j in range(numberOfBands):
if i == 0:
bandArrays.append(bytearray(data[currentPosition : currentPosition + 1]))
else:
bandArrays[j].extend(data[currentPosition : currentPosition + 1])
currentPosition += 1
bands = [Image.frombytes('L', (width, height), bytes(bandArray)) for bandArray in bandArrays]
return bands

此代码打开 BIP 文件的时间太长,肯定有更好的方法来执行此操作。我也有 numpy 和 scipy 库,但我不确定如何使用它们,或者它们是否能以任何方式提供帮助。

由于图像中波段的数量也是可变的,我发现很难想出一种方法来快速读取文件并将图像分成其组成波段。

只是为了记录,我已经尝试在循环中弄乱列表方法(使用切片,不使用切片,仅使用追加,仅使用扩展等),它并没有特别重要的时间由于涉及的迭代次数 - (width * height * numberOfBands) 而丢失。

任何建议或建议都会非常有帮助。谢谢。

最佳答案

如果你能找到一个快速函数来将二进制数据加载到一个大的 python 列表(或 numpy 数组)中,你可以使用切片符号来解交错数据:

band0 = biglist[::nbands]
band1 = biglist[1::nbands]
....

这有帮助吗?

关于python - 在 python 中读取卫星图像文件时优化性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7279409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com