include/opencv2/flann/hdf5.h
Go to the documentation of this file.
00001 /***********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright 2008-2009  Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
00005  * Copyright 2008-2009  David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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     // Try block to detect exceptions raised by any of the calls inside it
00074     try
00075     {
00076         /*
00077          * Turn off the auto-printing when failure occurs so that we can
00078          * handle the errors appropriately
00079          */
00080         Exception::dontPrint();
00081 
00082         /*
00083          * Create a new file using H5F_ACC_TRUNC access,
00084          * default file creation properties, and default file
00085          * access properties.
00086          */
00087         H5File file( filename, H5F_ACC_TRUNC );
00088 
00089         /*
00090          * Define the size of the array and create the data space for fixed
00091          * size dataset.
00092          */
00093         hsize_t     dimsf[2];              // dataset dimensions
00094         dimsf[0] = flann_dataset.rows;
00095         dimsf[1] = flann_dataset.cols;
00096         DataSpace dataspace( 2, dimsf );
00097 
00098         /*
00099          * Create a new dataset within the file using defined dataspace and
00100          * datatype and default dataset creation properties.
00101          */
00102         DataSet dataset = file.createDataSet( name, get_hdf5_type<T>(), dataspace );
00103 
00104         /*
00105          * Write the data to the dataset using default memory space, file
00106          * space, and transfer properties.
00107          */
00108         dataset.write( flann_dataset.data, get_hdf5_type<T>() );
00109     }  // end of try block
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          * Check the type used by the dataset matches
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          * Get dataspace of the dataset.
00137          */
00138         DataSpace dataspace = dataset.getSpace();
00139 
00140         /*
00141          * Get the dimension size of each dimension in the dataspace and
00142          * display them.
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     }  // end of try block
00153     catch( H5::Exception &error )
00154     {
00155         error.printError();
00156         throw FLANNException(error.getDetailMsg());
00157     }
00158 }
00159 
00160 
00161 } // namespace cvflann
00162 
00163 #endif /* _OPENCV_HDF5_H_ */