composite_index.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #ifndef OPENCV_FLANN_COMPOSITE_INDEX_H_
  31. #define OPENCV_FLANN_COMPOSITE_INDEX_H_
  32. #include "general.h"
  33. #include "nn_index.h"
  34. #include "kdtree_index.h"
  35. #include "kmeans_index.h"
  36. namespace cvflann
  37. {
  38. /**
  39. * Index parameters for the CompositeIndex.
  40. */
  41. struct CompositeIndexParams : public IndexParams
  42. {
  43. CompositeIndexParams(int trees = 4, int branching = 32, int iterations = 11,
  44. flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM, float cb_index = 0.2 )
  45. {
  46. (*this)["algorithm"] = FLANN_INDEX_KMEANS;
  47. // number of randomized trees to use (for kdtree)
  48. (*this)["trees"] = trees;
  49. // branching factor
  50. (*this)["branching"] = branching;
  51. // max iterations to perform in one kmeans clustering (kmeans tree)
  52. (*this)["iterations"] = iterations;
  53. // algorithm used for picking the initial cluster centers for kmeans tree
  54. (*this)["centers_init"] = centers_init;
  55. // cluster boundary index. Used when searching the kmeans tree
  56. (*this)["cb_index"] = cb_index;
  57. }
  58. };
  59. /**
  60. * This index builds a kd-tree index and a k-means index and performs nearest
  61. * neighbour search both indexes. This gives a slight boost in search performance
  62. * as some of the neighbours that are missed by one index are found by the other.
  63. */
  64. template <typename Distance>
  65. class CompositeIndex : public NNIndex<Distance>
  66. {
  67. public:
  68. typedef typename Distance::ElementType ElementType;
  69. typedef typename Distance::ResultType DistanceType;
  70. /**
  71. * Index constructor
  72. * @param inputData dataset containing the points to index
  73. * @param params Index parameters
  74. * @param d Distance functor
  75. * @return
  76. */
  77. CompositeIndex(const Matrix<ElementType>& inputData, const IndexParams& params = CompositeIndexParams(),
  78. Distance d = Distance()) : index_params_(params)
  79. {
  80. kdtree_index_ = new KDTreeIndex<Distance>(inputData, params, d);
  81. kmeans_index_ = new KMeansIndex<Distance>(inputData, params, d);
  82. }
  83. CompositeIndex(const CompositeIndex&);
  84. CompositeIndex& operator=(const CompositeIndex&);
  85. virtual ~CompositeIndex()
  86. {
  87. delete kdtree_index_;
  88. delete kmeans_index_;
  89. }
  90. /**
  91. * @return The index type
  92. */
  93. flann_algorithm_t getType() const CV_OVERRIDE
  94. {
  95. return FLANN_INDEX_COMPOSITE;
  96. }
  97. /**
  98. * @return Size of the index
  99. */
  100. size_t size() const CV_OVERRIDE
  101. {
  102. return kdtree_index_->size();
  103. }
  104. /**
  105. * \returns The dimensionality of the features in this index.
  106. */
  107. size_t veclen() const CV_OVERRIDE
  108. {
  109. return kdtree_index_->veclen();
  110. }
  111. /**
  112. * \returns The amount of memory (in bytes) used by the index.
  113. */
  114. int usedMemory() const CV_OVERRIDE
  115. {
  116. return kmeans_index_->usedMemory() + kdtree_index_->usedMemory();
  117. }
  118. /**
  119. * \brief Builds the index
  120. */
  121. void buildIndex() CV_OVERRIDE
  122. {
  123. Logger::info("Building kmeans tree...\n");
  124. kmeans_index_->buildIndex();
  125. Logger::info("Building kdtree tree...\n");
  126. kdtree_index_->buildIndex();
  127. }
  128. /**
  129. * \brief Saves the index to a stream
  130. * \param stream The stream to save the index to
  131. */
  132. void saveIndex(FILE* stream) CV_OVERRIDE
  133. {
  134. kmeans_index_->saveIndex(stream);
  135. kdtree_index_->saveIndex(stream);
  136. }
  137. /**
  138. * \brief Loads the index from a stream
  139. * \param stream The stream from which the index is loaded
  140. */
  141. void loadIndex(FILE* stream) CV_OVERRIDE
  142. {
  143. kmeans_index_->loadIndex(stream);
  144. kdtree_index_->loadIndex(stream);
  145. }
  146. /**
  147. * \returns The index parameters
  148. */
  149. IndexParams getParameters() const CV_OVERRIDE
  150. {
  151. return index_params_;
  152. }
  153. /**
  154. * \brief Method that searches for nearest-neighbours
  155. */
  156. void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) CV_OVERRIDE
  157. {
  158. kmeans_index_->findNeighbors(result, vec, searchParams);
  159. kdtree_index_->findNeighbors(result, vec, searchParams);
  160. }
  161. private:
  162. /** The k-means index */
  163. KMeansIndex<Distance>* kmeans_index_;
  164. /** The kd-tree index */
  165. KDTreeIndex<Distance>* kdtree_index_;
  166. /** The index parameters */
  167. const IndexParams index_params_;
  168. };
  169. }
  170. #endif //OPENCV_FLANN_COMPOSITE_INDEX_H_