列堆栈和行堆栈,H5py到现有数据集

2024-03-29 14:32:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python将HDF5文件中的列堆栈和行堆栈数据与其他数据一起使用。我从一个相机记录图像,并保存到单独的文件。然后我希望能够生成一个文件,将所有的图像拼接在一起。因此,我希望能够在一个新文件中创建一个数据集,并将每个图像文件中的所有数组堆叠到一个文件中。在

我知道h5py允许我像numPy数组一样使用数据集,但是我不知道如何告诉h5py再次将数据保存到文件中。下面我有一个非常简单的例子。在

我的问题是如何将HDF5文件中的数据与第二个数组(arr2)进行列堆栈,以便arr2保存到文件中?

(注意:在我的实际应用程序中,文件中的数据将比示例中的数据大得多。因此,将数据导入内存、列堆叠,然后将其重写到文件中是不可能的。)

import h5py
import numpy

arr1 = numpy.random.random((2000,2000))

with h5py.File("Plot0.h5", "w") as f:
    dset = f.create_dataset("Plot", data = arr1)

arr2 = numpy.random.random((2000,2000))

with h5py.File("Plot0.h5", "r+") as f:
    dset = f["Plot"]
    dset = numpy.column_stack((dset, arr2))

这似乎是个小问题,但我所有的搜索都没有成功。提前谢谢。在


Tags: 文件数据图像importnumpy堆栈withrandom
1条回答
网友
1楼 · 发布于 2024-03-29 14:32:50

在重读了一些关于H5py的文档之后,我意识到了我的错误。下面是我的新脚本结构,它允许我在HDF5文件中堆叠数组:

import h5py
import numpy

arr1 = numpy.random.random((2000,2000))

with h5py.File("Plot0.h5", "w") as f:
    dset = f.create_dataset("Plot", data = arr1, maxshape=(None,None))

dsetX, dsetY = 2000,2000
go = ""
while go == "":
    go = raw_input("Current Size: " + str(dsetX) + "  " + str(dsetY) + "  Continue?")
    arr2 = numpy.random.random((2000,2000))

    with h5py.File("Plot0.h5", "r+") as f:
        dset = f["Plot"]
        print len(arr2[:])
        print len(arr2[0][:])
        change = "column"

        dsetX, dsetY = dset.shape

        if change == "column":

            x1 = dsetX
            x2 = len(arr2[:]) + dsetX

            y1 = 0
            y2 = len(arr2[0][:])

            dset.shape = (x2, y2)
        else:
            x1 = 0
            x2 = len(arr2[:])

            y1 = dsetY
            y2 = len(arr2[0][:]) + dsetY

            dset.shape = (x2, y2)
        print "x1", x1
        print "x2", x2
        print "y1", y1
        print "y2", y2

        print dset.shape

        dset[x1:x2,y1:y2] = arr2

        print arr2
        print "\n"
        print dset[x1:x2,y1:y2]

        dsetX, dsetY = dset.shape

我希望这能帮助其他人。当然,更好地解决这个问题是值得欢迎的。在

相关问题 更多 >