简体   繁体   English

如何以HDF5格式存储非常大的3维矩阵?

[英]How to store very large 3 dimensional matrix in HDF5 format?

I have a very large matrix which is a video file as an array of frames, each around 350x250 resolution. 我有一个非常大的矩阵,它是一个视频文件,是一个帧数组,每个帧的分辨率约为350x250。 I have around 8,000-10,000 such frames in a single video file, which is around 1-1.5GB in size. 我在单个视频文件中有大约8,000-10,000个此类帧,大小约为1-1.5GB。 I have figured that HDF5 is a good file format for my use as I have to perform a lot of mathematical operations on the file (across the entire depth column). 我已经发现HDF5是一种很好的文件格式,因为我必须对文件执行很多数学运算(在整个深度列中)。 My problem is that I am unable to store this 3D matrix in HDF5. 我的问题是我无法在HDF5中存储此3D矩阵。 Can someone suggest me how to store these frames in an incremental fashion (adding frame by frame to the hdf5 file) as a 3D matrix in hdf5 format? 有人可以建议我如何以增量格式(将帧逐帧添加到hdf5文件中)作为hdf5格式的3D矩阵存储这些帧吗? I am using h5py python package. 我正在使用h5py python软件包。

As an example, let's assume your video has 10 frames with a resolution of 200x200 pixels. 举例来说,假设您的视频有10帧,分辨率为200x200像素。 Therefore, you would have to create a dataset with dimensions 10 x 200 x 200 x 3 with data type uint8 (each RGB component uses 8 unsigned bits). 因此,您将必须创建尺寸为10 x 200 x 200 x 3且数据类型为uint8的数据集(每个RGB组件使用8个无符号位)。 Here's how this transfers to the h5py api. 这是如何转移到h5py api的方法。 Check the docs for details. 检查文档以获取详细信息。

import h5py
import numpy as np

# create an hdf5 file
with h5py.File("/tmp/videos.h5") as f:
    # create a dataset for your movie
    dst = f.create_dataset("myvideo", shape=(10, 200, 200, 3),
                           dtype=np.uint8)
    # fill the 10 frames with a random image
    for frame in range(10):
        dst[frame] = np.random.randint(255, size=(200, 200, 3))

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

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