Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef _OPENCV_HDF5_H_
00031 #define _OPENCV_HDF5_H_
00032
00033 #include <H5Cpp.h>
00034
00035 #include "opencv2/flann/matrix.h"
00036
00037
00038
00039 #ifndef H5_NO_NAMESPACE
00040 using namespace H5;
00041 #endif
00042
00043 namespace cvflann
00044 {
00045
00046
00047 namespace {
00048
00049 template<typename T>
00050 PredType get_hdf5_type()
00051 {
00052 throw FLANNException("Unsupported type for IO operations");
00053 }
00054
00055 template<> PredType get_hdf5_type<char>() { return PredType::NATIVE_CHAR; }
00056 template<> PredType get_hdf5_type<unsigned char>() { return PredType::NATIVE_UCHAR; }
00057 template<> PredType get_hdf5_type<short int>() { return PredType::NATIVE_SHORT; }
00058 template<> PredType get_hdf5_type<unsigned short int>() { return PredType::NATIVE_USHORT; }
00059 template<> PredType get_hdf5_type<int>() { return PredType::NATIVE_INT; }
00060 template<> PredType get_hdf5_type<unsigned int>() { return PredType::NATIVE_UINT; }
00061 template<> PredType get_hdf5_type<long>() { return PredType::NATIVE_LONG; }
00062 template<> PredType get_hdf5_type<unsigned long>() { return PredType::NATIVE_ULONG; }
00063 template<> PredType get_hdf5_type<float>() { return PredType::NATIVE_FLOAT; }
00064 template<> PredType get_hdf5_type<double>() { return PredType::NATIVE_DOUBLE; }
00065 template<> PredType get_hdf5_type<long double>() { return PredType::NATIVE_LDOUBLE; }
00066
00067 }
00068
00069
00070 template<typename T>
00071 void save_to_file(const cvflann::Matrix<T>& flann_dataset, const std::string& filename, const std::string& name)
00072 {
00073
00074 try
00075 {
00076
00077
00078
00079
00080 Exception::dontPrint();
00081
00082
00083
00084
00085
00086
00087 H5File file( filename, H5F_ACC_TRUNC );
00088
00089
00090
00091
00092
00093 hsize_t dimsf[2];
00094 dimsf[0] = flann_dataset.rows;
00095 dimsf[1] = flann_dataset.cols;
00096 DataSpace dataspace( 2, dimsf );
00097
00098
00099
00100
00101
00102 DataSet dataset = file.createDataSet( name, get_hdf5_type<T>(), dataspace );
00103
00104
00105
00106
00107
00108 dataset.write( flann_dataset.data, get_hdf5_type<T>() );
00109 }
00110 catch( H5::Exception& error )
00111 {
00112 error.printError();
00113 throw FLANNException(error.getDetailMsg());
00114 }
00115 }
00116
00117
00118 template<typename T>
00119 void load_from_file(cvflann::Matrix<T>& flann_dataset, const std::string& filename, const std::string& name)
00120 {
00121 try
00122 {
00123 Exception::dontPrint();
00124
00125 H5File file( filename, H5F_ACC_RDONLY );
00126 DataSet dataset = file.openDataSet( name );
00127
00128
00129
00130
00131 if ( !(dataset.getDataType()==get_hdf5_type<T>())) {
00132 throw FLANNException("Dataset matrix type does not match the type to be read.");
00133 }
00134
00135
00136
00137
00138 DataSpace dataspace = dataset.getSpace();
00139
00140
00141
00142
00143
00144 hsize_t dims_out[2];
00145 dataspace.getSimpleExtentDims( dims_out, NULL);
00146
00147 flann_dataset.rows = dims_out[0];
00148 flann_dataset.cols = dims_out[1];
00149 flann_dataset.data = new T[flann_dataset.rows*flann_dataset.cols];
00150
00151 dataset.read( flann_dataset.data, get_hdf5_type<T>() );
00152 }
00153 catch( H5::Exception &error )
00154 {
00155 error.printError();
00156 throw FLANNException(error.getDetailMsg());
00157 }
00158 }
00159
00160
00161 }
00162
00163 #endif