composite_index.h
Go to the documentation of this file.
1 /***********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
5  * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
6  *
7  * THE BSD LICENSE
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *************************************************************************/
30 
31 #ifndef OPENCV_FLANN_COMPOSITE_INDEX_H_
32 #define OPENCV_FLANN_COMPOSITE_INDEX_H_
33 
34 #include "general.h"
35 #include "nn_index.h"
36 #include "kdtree_index.h"
37 #include "kmeans_index.h"
38 
39 namespace cvflann
40 {
41 
46 {
47  CompositeIndexParams(int trees = 4, int branching = 32, int iterations = 11,
48  flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM, float cb_index = 0.2 )
49  {
50  (*this)["algorithm"] = FLANN_INDEX_KMEANS;
51  // number of randomized trees to use (for kdtree)
52  (*this)["trees"] = trees;
53  // branching factor
54  (*this)["branching"] = branching;
55  // max iterations to perform in one kmeans clustering (kmeans tree)
56  (*this)["iterations"] = iterations;
57  // algorithm used for picking the initial cluster centers for kmeans tree
58  (*this)["centers_init"] = centers_init;
59  // cluster boundary index. Used when searching the kmeans tree
60  (*this)["cb_index"] = cb_index;
61  }
62 };
63 
64 
70 template <typename Distance>
71 class CompositeIndex : public NNIndex<Distance>
72 {
73 public:
74  typedef typename Distance::ElementType ElementType;
75  typedef typename Distance::ResultType DistanceType;
76 
85  Distance d = Distance()) : index_params_(params)
86  {
87  kdtree_index_ = new KDTreeIndex<Distance>(inputData, params, d);
88  kmeans_index_ = new KMeansIndex<Distance>(inputData, params, d);
89 
90  }
91 
94 
95  virtual ~CompositeIndex()
96  {
97  delete kdtree_index_;
98  delete kmeans_index_;
99  }
100 
105  {
106  return FLANN_INDEX_COMPOSITE;
107  }
108 
112  size_t size() const
113  {
114  return kdtree_index_->size();
115  }
116 
120  size_t veclen() const
121  {
122  return kdtree_index_->veclen();
123  }
124 
128  int usedMemory() const
129  {
130  return kmeans_index_->usedMemory() + kdtree_index_->usedMemory();
131  }
132 
136  void buildIndex()
137  {
138  Logger::info("Building kmeans tree...\n");
139  kmeans_index_->buildIndex();
140  Logger::info("Building kdtree tree...\n");
141  kdtree_index_->buildIndex();
142  }
143 
148  void saveIndex(FILE* stream)
149  {
150  kmeans_index_->saveIndex(stream);
151  kdtree_index_->saveIndex(stream);
152  }
153 
158  void loadIndex(FILE* stream)
159  {
160  kmeans_index_->loadIndex(stream);
161  kdtree_index_->loadIndex(stream);
162  }
163 
168  {
169  return index_params_;
170  }
171 
175  void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams)
176  {
177  kmeans_index_->findNeighbors(result, vec, searchParams);
178  kdtree_index_->findNeighbors(result, vec, searchParams);
179  }
180 
181 private:
183  KMeansIndex<Distance>* kmeans_index_;
184 
186  KDTreeIndex<Distance>* kdtree_index_;
187 
189  const IndexParams index_params_;
190 };
191 
192 }
193 
194 #endif //OPENCV_FLANN_COMPOSITE_INDEX_H_
Definition: defines.h:86
const CvArr CvSeq CvSeq CvMemStorage CvSURFParams params
Definition: compat.hpp:647
flann_algorithm_t
Definition: defines.h:81
Definition: kmeans_index.h:81
Definition: composite_index.h:45
flann_algorithm_t getType() const
Definition: composite_index.h:104
int d
Definition: legacy.hpp:3064
CompositeIndex(const Matrix< ElementType > &inputData, const IndexParams &params=CompositeIndexParams(), Distance d=Distance())
Definition: composite_index.h:84
Definition: defines.h:85
const CvArr const CvArr CvArr * result
Definition: core_c.h:805
Definition: params.h:44
Distance::ElementType ElementType
Definition: composite_index.h:74
CompositeIndexParams(int trees=4, int branching=32, int iterations=11, flann_centers_init_t centers_init=FLANN_CENTERS_RANDOM, float cb_index=0.2)
Definition: composite_index.h:47
int usedMemory() const
Definition: composite_index.h:128
Definition: composite_index.h:71
size_t veclen() const
Definition: composite_index.h:120
const CvArr CvArr double int int int iterations
Definition: tracking.hpp:102
Distance::ResultType DistanceType
Definition: composite_index.h:75
flann_centers_init_t
Definition: defines.h:105
void buildIndex()
Builds the index.
Definition: composite_index.h:136
Definition: result_set.h:66
std::map< std::string, any > IndexParams
Definition: params.h:42
virtual ~CompositeIndex()
Definition: composite_index.h:95
GLenum const GLfloat * params
Definition: compat.hpp:688
Definition: kdtree_index.h:70
IndexParams getParameters() const
Definition: composite_index.h:167
Definition: nn_index.h:48
void loadIndex(FILE *stream)
Loads the index from a stream.
Definition: composite_index.h:158
CompositeIndex & operator=(const CompositeIndex &)
Definition: defines.h:107
void findNeighbors(ResultSet< DistanceType > &result, const ElementType *vec, const SearchParams &searchParams)
Method that searches for nearest-neighbours.
Definition: composite_index.h:175
size_t size() const
Definition: composite_index.h:112
void saveIndex(FILE *stream)
Saves the index to a stream.
Definition: composite_index.h:148