您当前的位置: 首页 > 

惊鸿一博

暂无认证

  • 1浏览

    0关注

    535博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

将RGB图像和depth深度图像存入同一个HDF5文件中

惊鸿一博 发布时间:2020-10-02 17:41:51 ,浏览量:1

目标

代码
#include 
#include "hdf5.h"
#include 

#include 
using namespace std;

int  main(void)
{
  
    cv::Mat rgbImage = cv::imread("/home/john/projects/testhdf5/rgb_000131.png");
    cv::Mat depthImage = cv::imread("/home/john/projects/testhdf5/depth_000131.png"); 

    // define basic information for h5 file
    std::string fileName = "lhjpic.h5";
    std::string datasetDepthName = "depth";
    std::string datasetRGBName = "rgb";
    
    int height = rgbImage.rows;
    int width = rgbImage.cols;
    int channel = 3;

    hid_t       file, datasetDepth, datasetRGB;       
    hid_t       datatypeDepth, datatypeRGB, dataspaceDepth, dataspaceRGB; 
    hsize_t   dimsDepth[2], dimsRGB[3]; 
    herr_t     status;
    float       dataDepth[height][width];
    int		  dataRGB[channel][height][width];

    // set the value for depth dataset
    for(int h = 0; h < height; h++)
      for(int w = 0; w < width; w++)
	  dataDepth[h][w] =  depthImage.ptr(h)[w];
    // make sure the image is RGB/BRG  format. 
    assert(rgbImage.channels() == 3);

    // set the value for rgb dataset
    for(int h=0; h < height; h++)
    {
        cv::Vec3b *ptr = rgbImage.ptr(h);
	    for(int w=0; w < width; w++)
	    {
	      dataRGB[0][h][w] =  ptr[w][0];
	      dataRGB[1][h][w] =  ptr[w][1];
	      dataRGB[2][h][w] =  ptr[w][2]; 
	    }
    }

    // create h5 file
    file = H5Fcreate(fileName.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
    
    // set the dims, dataspace, and datatype for two datasets
    dimsDepth[0] = height;
    dimsDepth[1] = width;
    dimsRGB[0] = channel; 
    dimsRGB[1] = height;
    dimsRGB[2] = width;
    dataspaceDepth 	= H5Screate_simple(2, dimsDepth, NULL);
    dataspaceRGB 	= H5Screate_simple(3, dimsRGB, NULL);  
    
    datatypeDepth = H5Tcopy(H5T_NATIVE_FLOAT);
    status = H5Tset_order(datatypeDepth, H5T_ORDER_LE);
    
    datatypeRGB = H5Tcopy(H5T_NATIVE_INT);
    status = H5Tset_order(datatypeRGB, H5T_ORDER_LE);
    
    // create two specified datasets in the file
    datasetDepth = H5Dcreate2(file, datasetDepthName.c_str(), datatypeDepth, dataspaceDepth,
						H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    datasetRGB = H5Dcreate2(file, datasetRGBName.c_str(), datatypeRGB, dataspaceRGB,
						H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    
    // write the date to the respective dataset
    status = H5Dwrite(datasetDepth, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, dataDepth);
    status = H5Dwrite(datasetRGB, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, dataRGB);
    
    // close all handles
    H5Sclose(dataspaceDepth);
    H5Tclose(datatypeDepth);
    H5Dclose(datasetDepth);
    H5Sclose(dataspaceRGB);
    H5Tclose(datatypeRGB);
    H5Dclose(datasetRGB);
    H5Fclose(file);
    
    std::cout             
关注
打赏
1663399408
查看更多评论
0.0389s