如何从 HDF5 文件中提取单个 JPEG 图像 [英] How to extract individual JPEG images from a HDF5 file

查看:58
本文介绍了如何从 HDF5 文件中提取单个 JPEG 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的 HDF5 文件,其中包含图像及其相应的地面实况密度图.我想将它们放入网络 CRSNet 中,它需要单独文件中的图像.我怎样才能做到这一点?非常感谢.

I have a big HDF5 file with the images and its corresponding ground truth density map. I want to put them into the network CRSNet and it requires the images in separate files. How can I achieve that? Thank you very much.

-- 基本信息 我有一个带有两个键图像"的 HDF5 文件;和密度地图".它们的形状是 (300, 380, 676, 1).300代表图片数量,380和676分别代表高度和宽度.

-- Basic info I have a HDF5 file with two keys "images" and "density_maps". Their shapes are (300, 380, 676, 1). 300 stands for the number of images, 380 and 676 refer to the height and width respectively.

-- 我需要放入 CRSNet 网络的是带有相应 HDF5 文件的图像 (jpg).它们的形状将是 (572, 945).

-- What I need to put into the CRSNet network are the images (jpg) with their corresponding HDF5 files. The shape of them would be (572, 945).

非常感谢您的任何评论和讨论!

Thanks a lot for any comment and discussion!

推荐答案

首先,快速说明 h5py 和 HDF5.h5py 是一个用于读取 HDF5 文件的 Python 包.您还可以使用 PyTables 包(以及其他语言:C、C++、FORTRAN)读取 HDF5 文件.

For starters, a quick clarification on h5py and HDF5. h5py is a Python package to read HDF5 files. You can also read HDF5 files with the PyTables package (and with other languages: C, C++, FORTRAN).

我不完全确定您所说的图像 (jpg) 及其相应的 h5py (HDF5) 文件"是什么意思?据我了解,您的所有数据都在 1 个 HDF5 文件中.另外,我不明白您的意思:它们的形状是 (572, 945)."这和图像数据不同吧?请更新您的帖子以澄清这些项目.

I'm not entirely sure what you mean by "the images (jpg) with their corresponding h5py (HDF5) files" As I understand all of your data is in 1 HDF5 file. Also, I don't understand what you mean by: "The shape of them would be (572, 945)." This is different from the image data, right? Please update your post to clarify these items.

从数据集中提取数据相对容易.这就是您如何获得图像"作为 NumPy 数组并使用 cv2 编写为单独的 jpg 文件.见下面的代码:

It's relatively easy to extract data from a dataset. This is how you can get the "images" as NumPy arrays and and use cv2 to write as individual jpg files. See code below:

with h5py.File('yourfile.h5','r') as h5f:
    for i in range(h5f['images'].shape[0]):
        img_arr = h5f['images'][i,:]   # slice notation gets [i,:,:,:]
        cv2.imwrite(f'test_img_{i:03}.jpg',img_arr)

在开始编码之前,您确定需要将图像作为单独的图像文件还是单独的图像数据(通常是 NumPy 数组)?我问是因为大多数 CNN 过程的第一步是读取图像并将它们转换为数组以进行下游处理.您已经在 HDF5 文件中有数组.您可能需要做的就是读取每个数组并将其保存到适当的数据结构中,以便 CRSNet 处理它们.例如,这里是创建数组列表的代码(由 TensorFlow 和 Keras 使用):

Before you start coding, are you sure you need the images as individual image files, or individual image data (usually NumPy arrays)? I ask because the first step in most CNN processes is reading the images and converting them to arrays for downstream processing. You already have the arrays in the HDF5 file. All you may need to do is read each array and save to the appropriate data structure for CRSNet to process them. For example, here is the code to create a list of arrays (used by TensorFlow and Keras):

image_list = []
with h5py.File('yourfile.h5','r') as h5f:
    for i in range(h5f['images'].shape[0]):
        image_list.append( h5f['images'][i,:] )  # gets slice [i,:,:,:]
        

这篇关于如何从 HDF5 文件中提取单个 JPEG 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆