EstervQrCode 1.1.1
Library for qr code manipulation
flann_base.hpp
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 
35 
36 #include <vector>
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 class FILEScopeGuard {
49 
50 public:
51  explicit FILEScopeGuard(FILE* file) {
52  file_ = file;
53  };
54 
55  ~FILEScopeGuard() {
56  fclose(file_);
57  };
58 
59 private:
60  FILE* file_;
61 };
62 
63 
68 inline void log_verbosity(int level)
69 {
70  if (level >= 0) {
71  Logger::setLevel(level);
72  }
73 }
74 
78 struct SavedIndexParams : public IndexParams
79 {
80  SavedIndexParams(cv::String filename)
81  {
82  (* this)["algorithm"] = FLANN_INDEX_SAVED;
83  (*this)["filename"] = filename;
84  }
85 };
86 
87 template<typename Distance>
88 NNIndex<Distance>* load_saved_index(const Matrix<typename Distance::ElementType>& dataset, const cv::String& filename, Distance distance)
89 {
90  typedef typename Distance::ElementType ElementType;
91 
92  FILE* fin = fopen(filename.c_str(), "rb");
93  if (fin == NULL) {
94  return NULL;
95  }
96  FILEScopeGuard fscgd(fin);
97 
98  IndexHeader header = load_header(fin);
99  if (header.data_type != Datatype<ElementType>::type()) {
100  FLANN_THROW(cv::Error::StsError, "Datatype of saved index is different than of the one to be created.");
101  }
102  if ((size_t(header.rows) != dataset.rows)||(size_t(header.cols) != dataset.cols)) {
103  FLANN_THROW(cv::Error::StsError, "The index saved belongs to a different dataset");
104  }
105 
106  IndexParams params;
107  params["algorithm"] = header.index_type;
108  NNIndex<Distance>* nnIndex = create_index_by_type<Distance>(dataset, params, distance);
109  nnIndex->loadIndex(fin);
110 
111  return nnIndex;
112 }
113 
114 
115 template<typename Distance>
116 class Index : public NNIndex<Distance>
117 {
118 public:
119  typedef typename Distance::ElementType ElementType;
120  typedef typename Distance::ResultType DistanceType;
121 
122  Index(const Matrix<ElementType>& features, const IndexParams& params, Distance distance = Distance() )
123  :index_params_(params)
124  {
125  flann_algorithm_t index_type = get_param<flann_algorithm_t>(params,"algorithm");
126  loaded_ = false;
127 
128  if (index_type == FLANN_INDEX_SAVED) {
129  nnIndex_ = load_saved_index<Distance>(features, get_param<cv::String>(params,"filename"), distance);
130  loaded_ = true;
131  }
132  else {
133  nnIndex_ = create_index_by_type<Distance>(features, params, distance);
134  }
135  }
136 
137  ~Index()
138  {
139  delete nnIndex_;
140  }
141 
145  void buildIndex() CV_OVERRIDE
146  {
147  if (!loaded_) {
148  nnIndex_->buildIndex();
149  }
150  }
151 
152  void save(cv::String filename)
153  {
154  FILE* fout = fopen(filename.c_str(), "wb");
155  if (fout == NULL) {
156  FLANN_THROW(cv::Error::StsError, "Cannot open file");
157  }
158  save_header(fout, *nnIndex_);
159  saveIndex(fout);
160  fclose(fout);
161  }
162 
167  virtual void saveIndex(FILE* stream) CV_OVERRIDE
168  {
169  nnIndex_->saveIndex(stream);
170  }
171 
176  virtual void loadIndex(FILE* stream) CV_OVERRIDE
177  {
178  nnIndex_->loadIndex(stream);
179  }
180 
184  size_t veclen() const CV_OVERRIDE
185  {
186  return nnIndex_->veclen();
187  }
188 
192  size_t size() const CV_OVERRIDE
193  {
194  return nnIndex_->size();
195  }
196 
200  flann_algorithm_t getType() const CV_OVERRIDE
201  {
202  return nnIndex_->getType();
203  }
204 
208  virtual int usedMemory() const CV_OVERRIDE
209  {
210  return nnIndex_->usedMemory();
211  }
212 
213 
217  IndexParams getParameters() const CV_OVERRIDE
218  {
219  return nnIndex_->getParameters();
220  }
221 
230  void knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, int knn, const SearchParams& params) CV_OVERRIDE
231  {
232  nnIndex_->knnSearch(queries, indices, dists, knn, params);
233  }
234 
244  int radiusSearch(const Matrix<ElementType>& query, Matrix<int>& indices, Matrix<DistanceType>& dists, float radius, const SearchParams& params) CV_OVERRIDE
245  {
246  return nnIndex_->radiusSearch(query, indices, dists, radius, params);
247  }
248 
252  void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) CV_OVERRIDE
253  {
254  nnIndex_->findNeighbors(result, vec, searchParams);
255  }
256 
260  CV_DEPRECATED NNIndex<Distance>* getIndex()
261  {
262  return nnIndex_;
263  }
264 
269  CV_DEPRECATED const IndexParams* getIndexParameters()
270  {
271  return &index_params_;
272  }
273 
274 private:
276  NNIndex<Distance>* nnIndex_;
278  bool loaded_;
280  IndexParams index_params_;
281 
282  Index(const Index &); // copy disabled
283  Index& operator=(const Index &); // assign disabled
284 };
285 
297 template <typename Distance>
298 int hierarchicalClustering(const Matrix<typename Distance::ElementType>& points, Matrix<typename Distance::CentersType>& centers,
299  const KMeansIndexParams& params, Distance d = Distance())
300 {
301  KMeansIndex<Distance> kmeans(points, params, d);
302  kmeans.buildIndex();
303 
304  int clusterNum = kmeans.getClusterCenters(centers);
305  return clusterNum;
306 }
307 
308 }
309 
311 
312 #endif /* OPENCV_FLANN_BASE_HPP_ */
T fclose(T... args)
T fopen(T... args)
CvMat * header
Definition: core_c.h:782
CvSize size
Definition: core_c.h:112
int int type
Definition: core_c.h:221
const char const char ** filename
Definition: core_c.h:2630
const CvArr const CvArr CvArr * result
Definition: core_c.h:1423
CV_EXPORTS_W double kmeans(InputArray data, int K, InputOutputArray bestLabels, TermCriteria criteria, int attempts, int flags, OutputArray centers=noArray())
Finds centers of clusters and groups input samples around the clusters.
#define CV_DEPRECATED
Definition: cvdef.h:450
#define CV_OVERRIDE
Definition: cvdef.h:792
int hierarchicalClustering(const Mat &features, Mat &centers, const ::cvflann::KMeansIndexParams &params, Distance d=Distance())
Clusters features using hierarchical k-means algorithm.
Definition: flann.hpp:584
CvPoint2D32f float * radius
Definition: imgproc_c.h:534
@ StsError
unknown /unspecified error
Definition: base.hpp:71
Definition: flann.hpp:60
int rows
Definition: types_c.h:485
int cols
Definition: types_c.h:486