目标
#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
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?