nn_index.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_NNINDEX_H
  31. #define OPENCV_FLANN_NNINDEX_H
  32. #include "matrix.h"
  33. #include "result_set.h"
  34. #include "params.h"
  35. //! @cond IGNORED
  36. namespace cvflann
  37. {
  38. /**
  39. * Nearest-neighbour index base class
  40. */
  41. template <typename Distance>
  42. class NNIndex
  43. {
  44. typedef typename Distance::ElementType ElementType;
  45. typedef typename Distance::ResultType DistanceType;
  46. public:
  47. virtual ~NNIndex() {}
  48. /**
  49. * \brief Builds the index
  50. */
  51. virtual void buildIndex() = 0;
  52. /**
  53. * \brief Perform k-nearest neighbor search
  54. * \param[in] queries The query points for which to find the nearest neighbors
  55. * \param[out] indices The indices of the nearest neighbors found
  56. * \param[out] dists Distances to the nearest neighbors found
  57. * \param[in] knn Number of nearest neighbors to return
  58. * \param[in] params Search parameters
  59. */
  60. virtual void knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, int knn, const SearchParams& params)
  61. {
  62. CV_Assert(queries.cols == veclen());
  63. CV_Assert(indices.rows >= queries.rows);
  64. CV_Assert(dists.rows >= queries.rows);
  65. CV_Assert(int(indices.cols) >= knn);
  66. CV_Assert(int(dists.cols) >= knn);
  67. #if 0
  68. KNNResultSet<DistanceType> resultSet(knn);
  69. for (size_t i = 0; i < queries.rows; i++) {
  70. resultSet.init(indices[i], dists[i]);
  71. findNeighbors(resultSet, queries[i], params);
  72. }
  73. #else
  74. KNNUniqueResultSet<DistanceType> resultSet(knn);
  75. for (size_t i = 0; i < queries.rows; i++) {
  76. resultSet.clear();
  77. findNeighbors(resultSet, queries[i], params);
  78. if (get_param(params,"sorted",true)) resultSet.sortAndCopy(indices[i], dists[i], knn);
  79. else resultSet.copy(indices[i], dists[i], knn);
  80. }
  81. #endif
  82. }
  83. /**
  84. * \brief Perform radius search
  85. * \param[in] query The query point
  86. * \param[out] indices The indinces of the neighbors found within the given radius
  87. * \param[out] dists The distances to the nearest neighbors found
  88. * \param[in] radius The radius used for search
  89. * \param[in] params Search parameters
  90. * \returns Number of neighbors found
  91. */
  92. virtual int radiusSearch(const Matrix<ElementType>& query, Matrix<int>& indices, Matrix<DistanceType>& dists, float radius, const SearchParams& params)
  93. {
  94. if (query.rows != 1) {
  95. fprintf(stderr, "I can only search one feature at a time for range search\n");
  96. return -1;
  97. }
  98. CV_Assert(query.cols == veclen());
  99. CV_Assert(indices.cols == dists.cols);
  100. int n = 0;
  101. int* indices_ptr = NULL;
  102. DistanceType* dists_ptr = NULL;
  103. if (indices.cols > 0) {
  104. n = (int)indices.cols;
  105. indices_ptr = indices[0];
  106. dists_ptr = dists[0];
  107. }
  108. RadiusUniqueResultSet<DistanceType> resultSet((DistanceType)radius);
  109. resultSet.clear();
  110. findNeighbors(resultSet, query[0], params);
  111. if (n>0) {
  112. if (get_param(params,"sorted",true)) resultSet.sortAndCopy(indices_ptr, dists_ptr, n);
  113. else resultSet.copy(indices_ptr, dists_ptr, n);
  114. }
  115. return (int)resultSet.size();
  116. }
  117. /**
  118. * \brief Saves the index to a stream
  119. * \param stream The stream to save the index to
  120. */
  121. virtual void saveIndex(FILE* stream) = 0;
  122. /**
  123. * \brief Loads the index from a stream
  124. * \param stream The stream from which the index is loaded
  125. */
  126. virtual void loadIndex(FILE* stream) = 0;
  127. /**
  128. * \returns number of features in this index.
  129. */
  130. virtual size_t size() const = 0;
  131. /**
  132. * \returns The dimensionality of the features in this index.
  133. */
  134. virtual size_t veclen() const = 0;
  135. /**
  136. * \returns The amount of memory (in bytes) used by the index.
  137. */
  138. virtual int usedMemory() const = 0;
  139. /**
  140. * \returns The index type (kdtree, kmeans,...)
  141. */
  142. virtual flann_algorithm_t getType() const = 0;
  143. /**
  144. * \returns The index parameters
  145. */
  146. virtual IndexParams getParameters() const = 0;
  147. /**
  148. * \brief Method that searches for nearest-neighbours
  149. */
  150. virtual void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) = 0;
  151. };
  152. }
  153. //! @endcond
  154. #endif //OPENCV_FLANN_NNINDEX_H