flann_base.hpp
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_BASE_HPP_
32 #define OPENCV_FLANN_BASE_HPP_
33 
34 #include <vector>
35 #include <string>
36 #include <cassert>
37 #include <cstdio>
38 
39 #include "general.h"
40 #include "matrix.h"
41 #include "params.h"
42 #include "saving.h"
43 
44 #include "all_indices.h"
45 
46 namespace cvflann
47 {
48 
53 inline void log_verbosity(int level)
54 {
55  if (level >= 0) {
56  Logger::setLevel(level);
57  }
58 }
59 
64 {
66  {
67  (* this)["algorithm"] = FLANN_INDEX_SAVED;
68  (*this)["filename"] = filename;
69  }
70 };
71 
72 
73 template<typename Distance>
74 NNIndex<Distance>* load_saved_index(const Matrix<typename Distance::ElementType>& dataset, const std::string& filename, Distance distance)
75 {
76  typedef typename Distance::ElementType ElementType;
77 
78  FILE* fin = fopen(filename.c_str(), "rb");
79  if (fin == NULL) {
80  return NULL;
81  }
83  if (header.data_type != Datatype<ElementType>::type()) {
84  throw FLANNException("Datatype of saved index is different than of the one to be created.");
85  }
86  if ((size_t(header.rows) != dataset.rows)||(size_t(header.cols) != dataset.cols)) {
87  throw FLANNException("The index saved belongs to a different dataset");
88  }
89 
91  params["algorithm"] = header.index_type;
92  NNIndex<Distance>* nnIndex = create_index_by_type<Distance>(dataset, params, distance);
93  nnIndex->loadIndex(fin);
94  fclose(fin);
95 
96  return nnIndex;
97 }
98 
99 
100 template<typename Distance>
101 class Index : public NNIndex<Distance>
102 {
103 public:
104  typedef typename Distance::ElementType ElementType;
105  typedef typename Distance::ResultType DistanceType;
106 
107  Index(const Matrix<ElementType>& features, const IndexParams& params, Distance distance = Distance() )
108  : index_params_(params)
109  {
110  flann_algorithm_t index_type = get_param<flann_algorithm_t>(params,"algorithm");
111  loaded_ = false;
112 
113  if (index_type == FLANN_INDEX_SAVED) {
114  nnIndex_ = load_saved_index<Distance>(features, get_param<std::string>(params,"filename"), distance);
115  loaded_ = true;
116  }
117  else {
118  nnIndex_ = create_index_by_type<Distance>(features, params, distance);
119  }
120  }
121 
123  {
124  delete nnIndex_;
125  }
126 
130  void buildIndex()
131  {
132  if (!loaded_) {
133  nnIndex_->buildIndex();
134  }
135  }
136 
137  void save(std::string filename)
138  {
139  FILE* fout = fopen(filename.c_str(), "wb");
140  if (fout == NULL) {
141  throw FLANNException("Cannot open file");
142  }
143  save_header(fout, *nnIndex_);
144  saveIndex(fout);
145  fclose(fout);
146  }
147 
152  virtual void saveIndex(FILE* stream)
153  {
154  nnIndex_->saveIndex(stream);
155  }
156 
161  virtual void loadIndex(FILE* stream)
162  {
163  nnIndex_->loadIndex(stream);
164  }
165 
169  size_t veclen() const
170  {
171  return nnIndex_->veclen();
172  }
173 
177  size_t size() const
178  {
179  return nnIndex_->size();
180  }
181 
186  {
187  return nnIndex_->getType();
188  }
189 
193  virtual int usedMemory() const
194  {
195  return nnIndex_->usedMemory();
196  }
197 
198 
203  {
204  return nnIndex_->getParameters();
205  }
206 
215  void knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, int knn, const SearchParams& params)
216  {
217  nnIndex_->knnSearch(queries, indices, dists, knn, params);
218  }
219 
230  {
231  return nnIndex_->radiusSearch(query, indices, dists, radius, params);
232  }
233 
237  void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams)
238  {
239  nnIndex_->findNeighbors(result, vec, searchParams);
240  }
241 
245  FLANN_DEPRECATED NNIndex<Distance>* getIndex()
246  {
247  return nnIndex_;
248  }
249 
254  FLANN_DEPRECATED const IndexParams* getIndexParameters()
255  {
256  return &index_params_;
257  }
258 
259 private:
261  NNIndex<Distance>* nnIndex_;
263  bool loaded_;
265  IndexParams index_params_;
266 };
267 
279 template <typename Distance>
281  const KMeansIndexParams& params, Distance d = Distance())
282 {
283  KMeansIndex<Distance> kmeans(points, params, d);
284  kmeans.buildIndex();
285 
286  int clusterNum = kmeans.getClusterCenters(centers);
287  return clusterNum;
288 }
289 
290 }
291 #endif /* OPENCV_FLANN_BASE_HPP_ */
Definition: saving.h:69
void save_header(FILE *stream, const NNIndex< Distance > &index)
Definition: saving.h:86
void buildIndex()
Definition: kmeans_index.h:361
const CvArr CvSeq CvSeq CvMemStorage CvSURFParams params
Definition: compat.hpp:647
flann_algorithm_t
Definition: defines.h:81
flann_datatype_t data_type
Definition: saving.h:73
GLint level
Definition: tracking.hpp:88
static void setLevel(int level)
Definition: logger.h:85
const char const char ** filename
Definition: core_c.h:1750
flann_algorithm_t index_type
Definition: saving.h:74
size_t cols
Definition: matrix.h:52
Definition: kmeans_index.h:81
Definition: flann_base.hpp:63
size_t size() const
Definition: flann_base.hpp:177
int radiusSearch(const Matrix< ElementType > &query, Matrix< int > &indices, Matrix< DistanceType > &dists, float radius, const SearchParams &params)
Perform radius search.
Definition: flann_base.hpp:229
IndexParams getParameters() const
Definition: flann_base.hpp:202
void knnSearch(const Matrix< ElementType > &queries, Matrix< int > &indices, Matrix< DistanceType > &dists, int knn, const SearchParams &params)
Perform k-nearest neighbor search.
Definition: flann_base.hpp:215
virtual void loadIndex(FILE *stream)
Loads the index from a stream.
Definition: flann_base.hpp:161
Definition: kmeans_index.h:56
flann_algorithm_t getType() const
Definition: flann_base.hpp:185
void save(std::string filename)
Definition: flann_base.hpp:137
Index(const Matrix< ElementType > &features, const IndexParams &params, Distance distance=Distance())
Definition: flann_base.hpp:107
Definition: flann_base.hpp:101
Definition: general.h:41
int d
Definition: legacy.hpp:3064
Definition: matrix.h:46
CV_EXPORTS_W double kmeans(InputArray data, int K, CV_OUT InputOutputArray bestLabels, TermCriteria criteria, int attempts, int flags, OutputArray centers=noArray())
clusters the input data using k-Means algorithm
const CvArr const CvArr CvArr * result
Definition: core_c.h:805
Definition: params.h:44
SavedIndexParams(std::string filename)
Definition: flann_base.hpp:65
size_t cols
Definition: saving.h:76
GLuint GLuint GLsizei GLenum const GLvoid * indices
Definition: legacy.hpp:3084
int getClusterCenters(Matrix< DistanceType > &centers)
Definition: kmeans_index.h:459
IndexHeader load_header(FILE *stream)
Definition: saving.h:107
size_t veclen() const
Definition: flann_base.hpp:169
Definition: saving.h:47
Definition: result_set.h:66
std::map< std::string, any > IndexParams
Definition: params.h:42
virtual void loadIndex(FILE *stream)=0
Loads the index from a stream.
GLenum const GLfloat * params
Definition: compat.hpp:688
NNIndex< Distance > * load_saved_index(const Matrix< typename Distance::ElementType > &dataset, const std::string &filename, Distance distance)
Definition: flann_base.hpp:74
FLANN_DEPRECATED const IndexParams * getIndexParameters()
Returns index parameters.
Definition: flann_base.hpp:254
void findNeighbors(ResultSet< DistanceType > &result, const ElementType *vec, const SearchParams &searchParams)
Method that searches for nearest-neighbours.
Definition: flann_base.hpp:237
Distance::ElementType ElementType
Definition: flann_base.hpp:104
size_t rows
Definition: saving.h:75
virtual int usedMemory() const
Definition: flann_base.hpp:193
FLANN_DEPRECATED NNIndex< Distance > * getIndex()
Returns actual index.
Definition: flann_base.hpp:245
Definition: nn_index.h:48
size_t rows
Definition: matrix.h:51
GLsizei const GLfloat * points
CvMat * header
Definition: core_c.h:361
virtual void saveIndex(FILE *stream)
Saves the index to a stream.
Definition: flann_base.hpp:152
Definition: defines.h:90
void buildIndex()
Definition: flann_base.hpp:130
CvPoint int radius
Definition: core_c.h:1290
int hierarchicalClustering(const Matrix< typename Distance::ElementType > &points, Matrix< typename Distance::ResultType > &centers, const KMeansIndexParams &params, Distance d=Distance())
Definition: flann_base.hpp:280
Distance::ResultType DistanceType
Definition: flann_base.hpp:105
~Index()
Definition: flann_base.hpp:122
void log_verbosity(int level)
Definition: flann_base.hpp:53