【只谈干货】读取ITOP数据集中的HDF5文件

ITOP是斯坦福大学开源的人体关节检测数据集,包含RGB图、深度图和点云,下载链接如下:
https://www.alberthaque.com/projects/viewpoint_3d_pose/
关于读取HDF5文件所需库的下载安装可以参考这篇文章:
https://blog.csdn.net/ys578676728/article/details/104732140

1. ReadHDF5

我写了一个class用来读取ITOP数据集中的各种格式文件,直接上代码:

read_hdf5.h

#ifndef READ_HDF5_H
#define READ_HDF5_H

#include <iostream>
#include <vector>
#include "H5Cpp.h"
#include "params.h"

using namespace H5;
using namespace std;

const int BATCH = 1;   // how many frames of data

class ReadHDF5
{
    public:
        ReadHDF5(H5File, H5File, int);
        ~ReadHDF5();

        int readValidIndex();  // return num of valid data
        void readDepthMap(vector<vector<float>>&, int);
        void read2DCoord(vector<vector<int>>&, int);
        void read3DCoord(vector<vector<float>>&, int);

        int *is_valid_;
        int num_;

    private:
        DataSet dataset1_0_;     // depth_maps
        DataSet dataset2_0_;     // is_valid
        DataSet dataset2_1_;     // image_coordinates (2D)
        DataSet dataset2_2_;     // real_world_coordinates (3D)
};

#endif

params.h (相关参数)

#ifndef PARAMS_H
#define PARAMS_H

const int HEIGHT = 240;
const int WIDTH = 320;
const int NJOINTS = 15;

#endif

read_hdf5.cpp

构造参数n:全部图片/坐标的数量(39795 for side training data)
函数形参idx: 从第idx个图片/坐标开始读取
全局变量BATCH: 一次读取BATCH个图片、坐标

#include "include/read_hdf5.h"

ReadHDF5::ReadHDF5(H5File file_1, H5File file_2, int n) {
    dataset1_0_ = file_1.openDataSet("data");
    dataset2_0_ = file_2.openDataSet("is_valid");
    dataset2_1_ = file_2.openDataSet("image_coordinates");
    dataset2_2_ = file_2.openDataSet("real_world_coordinates");
    num_ = n;
    is_valid_ = new int[num_];
}

ReadHDF5::~ReadHDF5() {

}

int ReadHDF5::readValidIndex() {
    // Get dataspace of the dataset.
    DataSpace dataspace = dataset2_0_.getSpace();

    // Define hyperslab in the dataset; implicitly giving strike and block NULL.
    hsize_t offset[1];   // hyperslab offset in the file
    hsize_t count[1];    // size of the hyperslab in the file
    offset[0] = 0;
    count[0]  = num_;
    dataspace.selectHyperslab(H5S_SELECT_SET, count, offset);

    // Define the memory dataspace.
    hsize_t dimsm[1];      /* memory space dimensions */
    dimsm[0] = num_;
    DataSpace memspace(1, dimsm);

    // Define memory hyperslab.
    hsize_t offset_out[1];   // hyperslab offset in memory
    hsize_t count_out[1];    // size of the hyperslab in memory
    offset_out[0] = 0;
    count_out[0]  = num_;
    memspace.selectHyperslab(H5S_SELECT_SET, count_out, offset_out);

    // Read data from hyperslab in the file into the hyperslab in memory and display the data.
    dataset2_0_.read(is_valid_, PredType::NATIVE_INT, memspace, dataspace);

    // Count valid data
    int num_valid = 0;
    for(int i = 0; i < num_; i++){
        if(is_valid_[i] == 1) num_valid++;
    }

    return num_valid;
}

void ReadHDF5::readDepthMap(vector<vector<float>>& depth_map, int idx) {
    float dataout[BATCH][HEIGHT][WIDTH];
    
    // Get dataspace of the dataset.
    DataSpace dataspace = dataset1_0_.getSpace();

    // Define hyperslab in the dataset; implicitly giving strike and block NULL.
    hsize_t offset[3];   // hyperslab offset in the file
    hsize_t count[3];    // size of the hyperslab in the file
    offset[0] = idx; offset[1] = 0; offset[2] = 0;
    count[0]  = BATCH; count[1]  = HEIGHT; count[2]  = WIDTH;
    dataspace.selectHyperslab(H5S_SELECT_SET, count, offset);

    // Define the memory dataspace.
    hsize_t dimsm[3];      /* memory space dimensions */
    dimsm[0] = BATCH; dimsm[1] = HEIGHT; dimsm[2] = WIDTH;
    DataSpace memspace(3, dimsm);

    // Define memory hyperslab.
    hsize_t offset_out[3];   // hyperslab offset in memory
    hsize_t count_out[3];    // size of the hyperslab in memory
    offset_out[0] = 0; offset_out[1] = 0; offset_out[2] = 0;
    count_out[0]  = BATCH; count_out[1]  = HEIGHT; count_out[2]  = WIDTH;
    memspace.selectHyperslab(H5S_SELECT_SET, count_out, offset_out);

    // Read data from hyperslab in the file into the hyperslab in memory and display the data.
    dataset1_0_.read(dataout, PredType::NATIVE_FLOAT, memspace, dataspace);

    // Copy the image data to vector
    for(int i = 0; i < HEIGHT; i++) {
        for(int j = 0; j < WIDTH; j++) {
            depth_map[i][j] = dataout[0][i][j];
        }
    }
}

void ReadHDF5::read2DCoord(vector<vector<int>>& coords, int idx) {
    int dataout[BATCH][NJOINTS][2];

    // Get dataspace of the dataset.
    DataSpace dataspace = dataset2_1_.getSpace();

    // Define hyperslab in the dataset; implicitly giving strike and block NULL.
    hsize_t offset[3];   // hyperslab offset in the file
    hsize_t count[3];        // size of the hyperslab in the file
    offset[0] = idx; offset[1] = 0; offset[2] = 0;
    count[0]  = BATCH; count[1]  = NJOINTS; count[2]  = 2;
    dataspace.selectHyperslab(H5S_SELECT_SET, count, offset);

    // Define the memory dataspace.
    hsize_t dimsm[3];      /* memory space dimensions */
    dimsm[0] = BATCH; dimsm[1] = NJOINTS; dimsm[2] = 2;
    DataSpace memspace(3, dimsm);

    // Define memory hyperslab.
    hsize_t offset_out[3];   // hyperslab offset in memory
    hsize_t count_out[3];    // size of the hyperslab in memory
    offset_out[0] = 0; offset_out[1] = 0; offset_out[2] = 0;
    count_out[0]  = BATCH; count_out[1]  = NJOINTS; count_out[2]  = 2;
    memspace.selectHyperslab(H5S_SELECT_SET, count_out, offset_out);

    // Read data from hyperslab in the file into the hyperslab in memory and display the data.
    dataset2_1_.read(dataout, PredType::NATIVE_INT, memspace, dataspace);

    // Copy 2D coords to vector
    for(int i = 0; i < NJOINTS; i++) {
        for(int j = 0; j < 2; j++) {
            coords[i][j] = dataout[0][i][j];
        }
    }
}

void ReadHDF5::read3DCoord(vector<vector<float>>& coords, int idx) {
    float dataout[BATCH][NJOINTS][3];

    // Get dataspace of the dataset.
    DataSpace dataspace = dataset2_2_.getSpace();

    // Define hyperslab in the dataset; implicitly giving strike and block NULL.
    hsize_t offset[3];   // hyperslab offset in the file
    hsize_t count[3];        // size of the hyperslab in the file
    offset[0] = idx; offset[1] = 0; offset[2] = 0;
    count[0]  = BATCH; count[1]  = NJOINTS; count[2]  = 3;
    dataspace.selectHyperslab(H5S_SELECT_SET, count, offset);

    // Define the memory dataspace.
    hsize_t dimsm[3];      /* memory space dimensions */
    dimsm[0] = BATCH; dimsm[1] = NJOINTS; dimsm[2] = 3;
    DataSpace memspace(3, dimsm);

    // Define memory hyperslab.
    hsize_t offset_out[3];   // hyperslab offset in memory
    hsize_t count_out[3];    // size of the hyperslab in memory
    offset_out[0] = 0; offset_out[1] = 0; offset_out[2] = 0;
    count_out[0]  = BATCH; count_out[1]  = NJOINTS; count_out[2]  = 3;
    memspace.selectHyperslab(H5S_SELECT_SET, count_out, offset_out);

    // Read data from hyperslab in the file into the hyperslab in memory and display the data.
    dataset2_2_.read(dataout, PredType::NATIVE_FLOAT, memspace, dataspace);

    // Copy 3D coords to vector
    for(int i = 0; i < NJOINTS; i++) {
        for(int j = 0; j < 3; j++) {
            coords[i][j] = dataout[0][i][j];
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值