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
00031 #ifndef OPENCV_FLANN_COMPOSITE_INDEX_H_
00032 #define OPENCV_FLANN_COMPOSITE_INDEX_H_
00033
00034 #include "general.h"
00035 #include "nn_index.h"
00036 #include "kdtree_index.h"
00037 #include "kmeans_index.h"
00038
00039 namespace cvflann
00040 {
00041
00045 struct CompositeIndexParams : public IndexParams
00046 {
00047 CompositeIndexParams(int trees = 4, int branching = 32, int iterations = 11,
00048 flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM, float cb_index = 0.2 )
00049 {
00050 (*this)["algorithm"] = FLANN_INDEX_KMEANS;
00051
00052 (*this)["trees"] = trees;
00053
00054 (*this)["branching"] = branching;
00055
00056 (*this)["iterations"] = iterations;
00057
00058 (*this)["centers_init"] = centers_init;
00059
00060 (*this)["cb_index"] = cb_index;
00061 }
00062 };
00063
00064
00070 template <typename Distance>
00071 class CompositeIndex : public NNIndex<Distance>
00072 {
00073 public:
00074 typedef typename Distance::ElementType ElementType;
00075 typedef typename Distance::ResultType DistanceType;
00076
00084 CompositeIndex(const Matrix<ElementType>& inputData, const IndexParams& params = CompositeIndexParams(),
00085 Distance d = Distance()) : index_params_(params)
00086 {
00087 kdtree_index_ = new KDTreeIndex<Distance>(inputData, params, d);
00088 kmeans_index_ = new KMeansIndex<Distance>(inputData, params, d);
00089
00090 }
00091
00092 CompositeIndex(const CompositeIndex&);
00093 CompositeIndex& operator=(const CompositeIndex&);
00094
00095 virtual ~CompositeIndex()
00096 {
00097 delete kdtree_index_;
00098 delete kmeans_index_;
00099 }
00100
00104 flann_algorithm_t getType() const
00105 {
00106 return FLANN_INDEX_COMPOSITE;
00107 }
00108
00112 size_t size() const
00113 {
00114 return kdtree_index_->size();
00115 }
00116
00120 size_t veclen() const
00121 {
00122 return kdtree_index_->veclen();
00123 }
00124
00128 int usedMemory() const
00129 {
00130 return kmeans_index_->usedMemory() + kdtree_index_->usedMemory();
00131 }
00132
00136 void buildIndex()
00137 {
00138 Logger::info("Building kmeans tree...\n");
00139 kmeans_index_->buildIndex();
00140 Logger::info("Building kdtree tree...\n");
00141 kdtree_index_->buildIndex();
00142 }
00143
00148 void saveIndex(FILE* stream)
00149 {
00150 kmeans_index_->saveIndex(stream);
00151 kdtree_index_->saveIndex(stream);
00152 }
00153
00158 void loadIndex(FILE* stream)
00159 {
00160 kmeans_index_->loadIndex(stream);
00161 kdtree_index_->loadIndex(stream);
00162 }
00163
00167 IndexParams getParameters() const
00168 {
00169 return index_params_;
00170 }
00171
00175 void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams)
00176 {
00177 kmeans_index_->findNeighbors(result, vec, searchParams);
00178 kdtree_index_->findNeighbors(result, vec, searchParams);
00179 }
00180
00181 private:
00183 KMeansIndex<Distance>* kmeans_index_;
00184
00186 KDTreeIndex<Distance>* kdtree_index_;
00187
00189 const IndexParams index_params_;
00190 };
00191
00192 }
00193
00194 #endif //OPENCV_FLANN_COMPOSITE_INDEX_H_