本文主要是介绍C++ 读取 hdf5 格式文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
编译条件:
CMakeLists.txt 中添加
#findinclude
find_package(HDF5 REQUIRED COMPONENTS C CXX)
INCLUDE_DIRECTORIES(${HDF5_INCLUDE_DIRS})# linktarget
TARGET_LINK_LIBRARIES(test -lstdc++fs ${HDF5_CXX_LIBRARIES} ${HDF5_LIBRARIES})
完整 CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(testcpp)
# CUDA package
FIND_PACKAGE(CUDA REQUIRED)
INCLUDE(FindCUDA)
# CUDA include directories
INCLUDE_DIRECTORIES(/usr/local/cuda/include)
# OpenCV package
FIND_PACKAGE(OpenCV REQUIRED)
# OpenCV include directories
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
find_package(HDF5 REQUIRED COMPONENTS C CXX)
INCLUDE_DIRECTORIES(${HDF5_INCLUDE_DIRS})
find_package(Eigen3 REQUIRED)
INCLUDE_DIRECTORIES(${EIGEN3_INCLUDE_DIR})
# file name
FILE(GLOB SOURCES "*.cu" "*.cpp" "*.c" "*.h")# CUDA execu
CUDA_ADD_EXECUTABLE(test ${SOURCES})
# C++ list
LIST(APPEND CMAKE_CXX_FLAGS "-std=c++11 -O3 -ffast-math -Wall")
# CUDA list
LIST(APPEND CUDA_NVCC_FLAGS --compiler-options -fno-strict-aliasing -lineinfo -use_fast_math -Xptxas -dlcm=cg)
LIST(APPEND CUDA_NVCC_FLAGS -gencode arch=compute_20,code=sm_20)
LIST(APPEND CUDA_NVCC_FLAGS -gencode arch=compute_30,code=sm_30)
LIST(APPEND CUDA_NVCC_FLAGS -gencode arch=compute_35,code=sm_35)# target
TARGET_LINK_LIBRARIES(test -lstdc++fs ${OpenCV_LIBS} ${HDF5_CXX_LIBRARIES} ${HDF5_LIBRARIES})
读取文件函数:
main.cpp
#include <stdio.h>
#include <string>
#include <iostream>
#include <opencv2/opencv.hpp>
#include "kernel.h"
#include<Eigen/Eigen>
#include <Eigen/Core>
#include <Eigen/Dense>
#include<H5Cpp.h>using namespace Eigen;
using namespace std;
using namespace H5;const H5std_string FILE_NAME("../D006630_2-760-30_000.upah5");
const H5std_string DATASET_NAME("data3");
int main(){H5::H5File file(FILE_NAME, H5F_ACC_RDONLY);H5::DataSet dataset = file.openDataSet(DATASET_NAME);H5::DataSpace dataspace =dataset.getSpace();int ndims =dataspace.getSimpleExtentNdims();std::cout << " ndims = " << ndims << std::endl;hsize_t dims_out[ndims];dataspace.getSimpleExtentDims(dims_out, NULL);std::cout << " size = " << dims_out[0] << " " << dims_out[1] << " " << dims_out[2]<< std::endl;double ***data = new double**[dims_out[0]];for(int i = 0; i < dims_out[0]; i++){data[i] = new double*[dims_out[1]];for(int j = 0; j< dims_out[1]; j++){data[i][j] = new double[dims_out[2]];}}std::cout << "start read data ..." << std::endl;dataset.read(data, H5::PredType::NATIVE_DOUBLE, dataspace);dataset.close();file.close();return 0;
}
这篇关于C++ 读取 hdf5 格式文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!