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

标签 python image-processing file-io python-imaging-library satellite-image

我有一张以波段交错像素 (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/

相关文章:

android - 在react-native中裁剪后的图像非常小

c++ - 在 linux 中使用两个进程读取文件?

android - 在 Android 中加载自定义 .so

php - 无法写入文件,即使 is_writable 返回 true

python - 查找字母的所有组合,从字典中的不同键中选择每个字母

python - 如果将 QThread 创建为局部变量,为什么 QThread 的行为会有所不同

c++ - opencv中自适应阈值和正常阈值之间的区别

java - 我想用Java将4张图片合并在一起

用于打印 n 行模式 1 121 12321 12 1 的 Python 程序

python - 当我尝试使用 winrt 发出通知时,它会出错