EstervQrCode 1.1.1
Library for qr code manipulation
flann.hpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
8 //
9 //
10 // License Agreement
11 // For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
26 //
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42 
43 #ifndef OPENCV_FLANN_HPP
44 #define OPENCV_FLANN_HPP
45 
46 #include "opencv2/core.hpp"
47 #include "opencv2/flann/miniflann.hpp"
48 #include "opencv2/flann/flann_base.hpp"
49 
59 namespace cvflann
60 {
61  CV_EXPORTS flann_distance_t flann_distance_type();
63 }
64 
65 
66 namespace cv
67 {
68 namespace flann
69 {
70 
71 
74 
75 template <typename T> struct CvType {};
76 template <> struct CvType<unsigned char> { static int type() { return CV_8U; } };
77 template <> struct CvType<char> { static int type() { return CV_8S; } };
78 template <> struct CvType<unsigned short> { static int type() { return CV_16U; } };
79 template <> struct CvType<short> { static int type() { return CV_16S; } };
80 template <> struct CvType<int> { static int type() { return CV_32S; } };
81 template <> struct CvType<float> { static int type() { return CV_32F; } };
82 template <> struct CvType<double> { static int type() { return CV_64F; } };
83 
84 
85 // bring the flann parameters into this namespace
86 using ::cvflann::get_param;
87 using ::cvflann::print_params;
88 
89 // bring the flann distances into this namespace
90 using ::cvflann::L2_Simple;
91 using ::cvflann::L2;
92 using ::cvflann::L1;
93 using ::cvflann::MinkowskiDistance;
94 using ::cvflann::MaxDistance;
96 using ::cvflann::Hamming;
97 using ::cvflann::Hamming2;
98 using ::cvflann::DNAmmingLUT;
99 using ::cvflann::DNAmming2;
100 using ::cvflann::HistIntersectionDistance;
101 using ::cvflann::HellingerDistance;
102 using ::cvflann::ChiSquareDistance;
103 using ::cvflann::KL_Divergence;
104 
105 
169 template <typename Distance>
171 {
172 public:
173  typedef typename Distance::ElementType ElementType;
174  typedef typename Distance::ResultType DistanceType;
175 
286  GenericIndex(const Mat& features, const ::cvflann::IndexParams& params, Distance distance = Distance());
287 
289 
301  std::vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& params);
302  void knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& params);
303 
316  std::vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& params);
317  int radiusSearch(const Mat& query, Mat& indices, Mat& dists,
318  DistanceType radius, const ::cvflann::SearchParams& params);
319 
320  void save(String filename) { nnIndex->save(filename); }
321 
322  int veclen() const { return nnIndex->veclen(); }
323 
324  int size() const { return (int)nnIndex->size(); }
325 
326  ::cvflann::IndexParams getParameters() { return nnIndex->getParameters(); }
327 
328  CV_DEPRECATED const ::cvflann::IndexParams* getIndexParameters() { return nnIndex->getIndexParameters(); }
329 
330 private:
331  ::cvflann::Index<Distance>* nnIndex;
332  Mat _dataset;
333 };
334 
336 
337 #define FLANN_DISTANCE_CHECK \
338  if ( ::cvflann::flann_distance_type() != cvflann::FLANN_DIST_L2) { \
339  printf("[WARNING] You are using cv::flann::Index (or cv::flann::GenericIndex) and have also changed "\
340  "the distance using cvflann::set_distance_type. This is no longer working as expected "\
341  "(cv::flann::Index always uses L2). You should create the index templated on the distance, "\
342  "for example for L1 distance use: GenericIndex< L1<float> > \n"); \
343  }
344 
345 
346 template <typename Distance>
347 GenericIndex<Distance>::GenericIndex(const Mat& dataset, const ::cvflann::IndexParams& params, Distance distance)
348 : _dataset(dataset)
349 {
350  CV_Assert(dataset.type() == CvType<ElementType>::type());
351  CV_Assert(dataset.isContinuous());
352  ::cvflann::Matrix<ElementType> m_dataset((ElementType*)_dataset.ptr<ElementType>(0), _dataset.rows, _dataset.cols);
353 
354  nnIndex = new ::cvflann::Index<Distance>(m_dataset, params, distance);
355 
356  FLANN_DISTANCE_CHECK
357 
358  nnIndex->buildIndex();
359 }
360 
361 template <typename Distance>
363 {
364  delete nnIndex;
365 }
366 
367 template <typename Distance>
368 void GenericIndex<Distance>::knnSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& searchParams)
369 {
370  ::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
371  ::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
372  ::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
373 
374  FLANN_DISTANCE_CHECK
375 
376  nnIndex->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
377 }
378 
379 
380 template <typename Distance>
381 void GenericIndex<Distance>::knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& searchParams)
382 {
383  CV_Assert(queries.type() == CvType<ElementType>::type());
384  CV_Assert(queries.isContinuous());
385  ::cvflann::Matrix<ElementType> m_queries((ElementType*)queries.ptr<ElementType>(0), queries.rows, queries.cols);
386 
387  CV_Assert(indices.type() == CV_32S);
388  CV_Assert(indices.isContinuous());
389  ::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
390 
391  CV_Assert(dists.type() == CvType<DistanceType>::type());
392  CV_Assert(dists.isContinuous());
393  ::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
394 
395  FLANN_DISTANCE_CHECK
396 
397  nnIndex->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
398 }
399 
400 template <typename Distance>
401 int GenericIndex<Distance>::radiusSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
402 {
403  ::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
404  ::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
405  ::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
406 
407  FLANN_DISTANCE_CHECK
408 
409  return nnIndex->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
410 }
411 
412 template <typename Distance>
413 int GenericIndex<Distance>::radiusSearch(const Mat& query, Mat& indices, Mat& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
414 {
415  CV_Assert(query.type() == CvType<ElementType>::type());
416  CV_Assert(query.isContinuous());
417  ::cvflann::Matrix<ElementType> m_query((ElementType*)query.ptr<ElementType>(0), query.rows, query.cols);
418 
419  CV_Assert(indices.type() == CV_32S);
420  CV_Assert(indices.isContinuous());
421  ::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
422 
423  CV_Assert(dists.type() == CvType<DistanceType>::type());
424  CV_Assert(dists.isContinuous());
425  ::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
426 
427  FLANN_DISTANCE_CHECK
428 
429  return nnIndex->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
430 }
431 
435 template <typename T>
436 class Index_
437 {
438 public:
439  typedef typename L2<T>::ElementType ElementType;
440  typedef typename L2<T>::ResultType DistanceType;
441 
442  CV_DEPRECATED Index_(const Mat& dataset, const ::cvflann::IndexParams& params)
443  {
444  printf("[WARNING] The cv::flann::Index_<T> class is deperecated, use cv::flann::GenericIndex<Distance> instead\n");
445 
446  CV_Assert(dataset.type() == CvType<ElementType>::type());
447  CV_Assert(dataset.isContinuous());
448  ::cvflann::Matrix<ElementType> m_dataset((ElementType*)dataset.ptr<ElementType>(0), dataset.rows, dataset.cols);
449 
450  if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L2 ) {
451  nnIndex_L1 = NULL;
452  nnIndex_L2 = new ::cvflann::Index< L2<ElementType> >(m_dataset, params);
453  }
454  else if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L1 ) {
455  nnIndex_L1 = new ::cvflann::Index< L1<ElementType> >(m_dataset, params);
456  nnIndex_L2 = NULL;
457  }
458  else {
459  printf("[ERROR] cv::flann::Index_<T> only provides backwards compatibility for the L1 and L2 distances. "
460  "For other distance types you must use cv::flann::GenericIndex<Distance>\n");
461  CV_Assert(0);
462  }
463  if (nnIndex_L1) nnIndex_L1->buildIndex();
464  if (nnIndex_L2) nnIndex_L2->buildIndex();
465  }
466  CV_DEPRECATED ~Index_()
467  {
468  if (nnIndex_L1) delete nnIndex_L1;
469  if (nnIndex_L2) delete nnIndex_L2;
470  }
471 
472  CV_DEPRECATED void knnSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& searchParams)
473  {
474  ::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
475  ::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
476  ::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
477 
478  if (nnIndex_L1) nnIndex_L1->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
479  if (nnIndex_L2) nnIndex_L2->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
480  }
481  CV_DEPRECATED void knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& searchParams)
482  {
483  CV_Assert(queries.type() == CvType<ElementType>::type());
484  CV_Assert(queries.isContinuous());
485  ::cvflann::Matrix<ElementType> m_queries((ElementType*)queries.ptr<ElementType>(0), queries.rows, queries.cols);
486 
487  CV_Assert(indices.type() == CV_32S);
488  CV_Assert(indices.isContinuous());
489  ::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
490 
491  CV_Assert(dists.type() == CvType<DistanceType>::type());
492  CV_Assert(dists.isContinuous());
493  ::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
494 
495  if (nnIndex_L1) nnIndex_L1->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
496  if (nnIndex_L2) nnIndex_L2->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
497  }
498 
499  CV_DEPRECATED int radiusSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
500  {
501  ::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
502  ::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
503  ::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
504 
505  if (nnIndex_L1) return nnIndex_L1->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
506  if (nnIndex_L2) return nnIndex_L2->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
507  }
508 
509  CV_DEPRECATED int radiusSearch(const Mat& query, Mat& indices, Mat& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
510  {
511  CV_Assert(query.type() == CvType<ElementType>::type());
512  CV_Assert(query.isContinuous());
513  ::cvflann::Matrix<ElementType> m_query((ElementType*)query.ptr<ElementType>(0), query.rows, query.cols);
514 
515  CV_Assert(indices.type() == CV_32S);
516  CV_Assert(indices.isContinuous());
517  ::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
518 
519  CV_Assert(dists.type() == CvType<DistanceType>::type());
520  CV_Assert(dists.isContinuous());
521  ::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
522 
523  if (nnIndex_L1) return nnIndex_L1->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
524  if (nnIndex_L2) return nnIndex_L2->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
525  }
526 
527  CV_DEPRECATED void save(String filename)
528  {
529  if (nnIndex_L1) nnIndex_L1->save(filename);
530  if (nnIndex_L2) nnIndex_L2->save(filename);
531  }
532 
533  CV_DEPRECATED int veclen() const
534  {
535  if (nnIndex_L1) return nnIndex_L1->veclen();
536  if (nnIndex_L2) return nnIndex_L2->veclen();
537  }
538 
539  CV_DEPRECATED int size() const
540  {
541  if (nnIndex_L1) return nnIndex_L1->size();
542  if (nnIndex_L2) return nnIndex_L2->size();
543  }
544 
545  CV_DEPRECATED ::cvflann::IndexParams getParameters()
546  {
547  if (nnIndex_L1) return nnIndex_L1->getParameters();
548  if (nnIndex_L2) return nnIndex_L2->getParameters();
549 
550  }
551 
552  CV_DEPRECATED const ::cvflann::IndexParams* getIndexParameters()
553  {
554  if (nnIndex_L1) return nnIndex_L1->getIndexParameters();
555  if (nnIndex_L2) return nnIndex_L2->getIndexParameters();
556  }
557 
558 private:
559  // providing backwards compatibility for L2 and L1 distances (most common)
560  ::cvflann::Index< L2<ElementType> >* nnIndex_L2;
561  ::cvflann::Index< L1<ElementType> >* nnIndex_L1;
562 };
563 
565 
583 template <typename Distance>
584 int hierarchicalClustering(const Mat& features, Mat& centers, const ::cvflann::KMeansIndexParams& params,
585  Distance d = Distance())
586 {
587  typedef typename Distance::ElementType ElementType;
588  typedef typename Distance::CentersType CentersType;
589 
590  CV_Assert(features.type() == CvType<ElementType>::type());
591  CV_Assert(features.isContinuous());
592  ::cvflann::Matrix<ElementType> m_features((ElementType*)features.ptr<ElementType>(0), features.rows, features.cols);
593 
594  CV_Assert(centers.type() == CvType<CentersType>::type());
595  CV_Assert(centers.isContinuous());
596  ::cvflann::Matrix<CentersType> m_centers((CentersType*)centers.ptr<CentersType>(0), centers.rows, centers.cols);
597 
598  return ::cvflann::hierarchicalClustering<Distance>(m_features, m_centers, params, d);
599 }
600 
602 
603 template <typename ELEM_TYPE, typename DIST_TYPE>
604 CV_DEPRECATED int hierarchicalClustering(const Mat& features, Mat& centers, const ::cvflann::KMeansIndexParams& params)
605 {
606  printf("[WARNING] cv::flann::hierarchicalClustering<ELEM_TYPE,DIST_TYPE> is deprecated, use "
607  "cv::flann::hierarchicalClustering<Distance> instead\n");
608 
609  if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L2 ) {
610  return hierarchicalClustering< L2<ELEM_TYPE> >(features, centers, params);
611  }
612  else if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L1 ) {
613  return hierarchicalClustering< L1<ELEM_TYPE> >(features, centers, params);
614  }
615  else {
616  printf("[ERROR] cv::flann::hierarchicalClustering<ELEM_TYPE,DIST_TYPE> only provides backwards "
617  "compatibility for the L1 and L2 distances. "
618  "For other distance types you must use cv::flann::hierarchicalClustering<Distance>\n");
619  CV_Assert(0);
620  }
621 }
622 
624 
626 
627 } } // namespace cv::flann
628 
629 #endif
n-dimensional dense array class
Definition: mat.hpp:812
uchar * ptr(int i0=0)
Returns a pointer to the specified matrix row.
int cols
Definition: mat.hpp:2138
bool isContinuous() const
Reports whether the matrix is continuous or not.
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
Definition: mat.hpp:2138
int type() const
Returns the type of a matrix element.
The FLANN nearest neighbor index class. This class is templated with the type of elements for which t...
Definition: flann.hpp:171
Distance::ElementType ElementType
Definition: flann.hpp:173
void knnSearch(const Mat &queries, Mat &indices, Mat &dists, int knn, const ::cvflann::SearchParams &params)
void save(String filename)
Definition: flann.hpp:320
Distance::ResultType DistanceType
Definition: flann.hpp:174
void knnSearch(const std::vector< ElementType > &query, std::vector< int > &indices, std::vector< DistanceType > &dists, int knn, const ::cvflann::SearchParams &params)
Performs a K-nearest neighbor search for a given query point using the index.
int veclen() const
Definition: flann.hpp:322
CV_DEPRECATEDconst ::cvflann::IndexParams * getIndexParameters()
Definition: flann.hpp:328
GenericIndex(const Mat &features, const ::cvflann::IndexParams &params, Distance distance=Distance())
Constructs a nearest neighbor search index for a given dataset.
::cvflann::IndexParams getParameters()
Definition: flann.hpp:326
int radiusSearch(const std::vector< ElementType > &query, std::vector< int > &indices, std::vector< DistanceType > &dists, DistanceType radius, const ::cvflann::SearchParams &params)
Performs a radius nearest neighbor search for a given query point using the index.
int radiusSearch(const Mat &query, Mat &indices, Mat &dists, DistanceType radius, const ::cvflann::SearchParams &params)
int size() const
Definition: flann.hpp:324
T printf(T... args)
std::string String
Definition: cvstd.hpp:151
CvArr int order
Definition: core_c.h:1311
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
#define CV_8S
Definition: interface.h:74
#define CV_64F
Definition: interface.h:79
#define CV_8U
Definition: interface.h:73
#define CV_32S
Definition: interface.h:77
#define CV_32F
Definition: interface.h:78
#define CV_16S
Definition: interface.h:76
#define CV_16U
Definition: interface.h:75
#define CV_EXPORTS
Definition: cvdef.h:435
#define CV_DEPRECATED
Definition: cvdef.h:450
Hamming HammingLUT
Definition: base.hpp:393
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails.
Definition: base.hpp:342
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
const CvArr int distance_type
Definition: imgproc_c.h:392
"black box" representation of the file storage associated with a file on disk.
Definition: calib3d.hpp:441
Definition: flann.hpp:60
CV_EXPORTS flann_distance_t flann_distance_type()
CV_DEPRECATED CV_EXPORTS void set_distance_type(flann_distance_t distance_type, int order)
T size(T... args)
Accumulator< T >::Type ResultType
Definition: features2d.hpp:956
static int type()
Definition: flann.hpp:77
static int type()
Definition: flann.hpp:82
static int type()
Definition: flann.hpp:81
static int type()
Definition: flann.hpp:80
static int type()
Definition: flann.hpp:79
static int type()
Definition: flann.hpp:76
static int type()
Definition: flann.hpp:78
Definition: flann.hpp:75