miniflann.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #ifndef OPENCV_MINIFLANN_HPP
  43. #define OPENCV_MINIFLANN_HPP
  44. //! @cond IGNORED
  45. #include "opencv2/core.hpp"
  46. #include "opencv2/flann/defines.h"
  47. namespace cv
  48. {
  49. namespace flann
  50. {
  51. enum FlannIndexType {
  52. FLANN_INDEX_TYPE_8U = CV_8U,
  53. FLANN_INDEX_TYPE_8S = CV_8S,
  54. FLANN_INDEX_TYPE_16U = CV_16U,
  55. FLANN_INDEX_TYPE_16S = CV_16S,
  56. FLANN_INDEX_TYPE_32S = CV_32S,
  57. FLANN_INDEX_TYPE_32F = CV_32F,
  58. FLANN_INDEX_TYPE_64F = CV_64F,
  59. FLANN_INDEX_TYPE_STRING,
  60. FLANN_INDEX_TYPE_BOOL,
  61. FLANN_INDEX_TYPE_ALGORITHM,
  62. LAST_VALUE_FLANN_INDEX_TYPE = FLANN_INDEX_TYPE_ALGORITHM
  63. };
  64. struct CV_EXPORTS IndexParams
  65. {
  66. IndexParams();
  67. ~IndexParams();
  68. String getString(const String& key, const String& defaultVal=String()) const;
  69. int getInt(const String& key, int defaultVal=-1) const;
  70. double getDouble(const String& key, double defaultVal=-1) const;
  71. void setString(const String& key, const String& value);
  72. void setInt(const String& key, int value);
  73. void setDouble(const String& key, double value);
  74. void setFloat(const String& key, float value);
  75. void setBool(const String& key, bool value);
  76. void setAlgorithm(int value);
  77. // FIXIT: replace by void write(FileStorage& fs) const + read()
  78. void getAll(std::vector<String>& names,
  79. std::vector<FlannIndexType>& types,
  80. std::vector<String>& strValues,
  81. std::vector<double>& numValues) const;
  82. void* params;
  83. private:
  84. IndexParams(const IndexParams &); // copy disabled
  85. IndexParams& operator=(const IndexParams &); // assign disabled
  86. };
  87. struct CV_EXPORTS KDTreeIndexParams : public IndexParams
  88. {
  89. KDTreeIndexParams(int trees=4);
  90. };
  91. struct CV_EXPORTS LinearIndexParams : public IndexParams
  92. {
  93. LinearIndexParams();
  94. };
  95. struct CV_EXPORTS CompositeIndexParams : public IndexParams
  96. {
  97. CompositeIndexParams(int trees = 4, int branching = 32, int iterations = 11,
  98. cvflann::flann_centers_init_t centers_init = cvflann::FLANN_CENTERS_RANDOM, float cb_index = 0.2f );
  99. };
  100. struct CV_EXPORTS AutotunedIndexParams : public IndexParams
  101. {
  102. AutotunedIndexParams(float target_precision = 0.8f, float build_weight = 0.01f,
  103. float memory_weight = 0, float sample_fraction = 0.1f);
  104. };
  105. struct CV_EXPORTS HierarchicalClusteringIndexParams : public IndexParams
  106. {
  107. HierarchicalClusteringIndexParams(int branching = 32,
  108. cvflann::flann_centers_init_t centers_init = cvflann::FLANN_CENTERS_RANDOM, int trees = 4, int leaf_size = 100 );
  109. };
  110. struct CV_EXPORTS KMeansIndexParams : public IndexParams
  111. {
  112. KMeansIndexParams(int branching = 32, int iterations = 11,
  113. cvflann::flann_centers_init_t centers_init = cvflann::FLANN_CENTERS_RANDOM, float cb_index = 0.2f );
  114. };
  115. struct CV_EXPORTS LshIndexParams : public IndexParams
  116. {
  117. LshIndexParams(int table_number, int key_size, int multi_probe_level);
  118. };
  119. struct CV_EXPORTS SavedIndexParams : public IndexParams
  120. {
  121. SavedIndexParams(const String& filename);
  122. };
  123. struct CV_EXPORTS SearchParams : public IndexParams
  124. {
  125. SearchParams( int checks, float eps, bool sorted, bool explore_all_trees );
  126. SearchParams( int checks = 32, float eps = 0, bool sorted = true );
  127. };
  128. class CV_EXPORTS_W Index
  129. {
  130. public:
  131. CV_WRAP Index();
  132. CV_WRAP Index(InputArray features, const IndexParams& params, cvflann::flann_distance_t distType=cvflann::FLANN_DIST_L2);
  133. virtual ~Index();
  134. CV_WRAP virtual void build(InputArray features, const IndexParams& params, cvflann::flann_distance_t distType=cvflann::FLANN_DIST_L2);
  135. CV_WRAP virtual void knnSearch(InputArray query, OutputArray indices,
  136. OutputArray dists, int knn, const SearchParams& params=SearchParams());
  137. CV_WRAP virtual int radiusSearch(InputArray query, OutputArray indices,
  138. OutputArray dists, double radius, int maxResults,
  139. const SearchParams& params=SearchParams());
  140. CV_WRAP virtual void save(const String& filename) const;
  141. CV_WRAP virtual bool load(InputArray features, const String& filename);
  142. CV_WRAP virtual void release();
  143. CV_WRAP cvflann::flann_distance_t getDistance() const;
  144. CV_WRAP cvflann::flann_algorithm_t getAlgorithm() const;
  145. protected:
  146. bool load_(const String& filename);
  147. cvflann::flann_distance_t distType;
  148. cvflann::flann_algorithm_t algo;
  149. int featureType;
  150. void* index;
  151. Mat features_clone; // index may store features pointer internally for searching, so avoid dangling pointers: https://github.com/opencv/opencv/issues/17553
  152. };
  153. } } // namespace cv::flann
  154. //! @endcond
  155. #endif