flann.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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_FLANN_HPP_
  43. #define _OPENCV_FLANN_HPP_
  44. #include "opencv2/core.hpp"
  45. #include "opencv2/flann/miniflann.hpp"
  46. #include "opencv2/flann/flann_base.hpp"
  47. /**
  48. @defgroup flann Clustering and Search in Multi-Dimensional Spaces
  49. This section documents OpenCV's interface to the FLANN library. FLANN (Fast Library for Approximate
  50. Nearest Neighbors) is a library that contains a collection of algorithms optimized for fast nearest
  51. neighbor search in large datasets and for high dimensional features. More information about FLANN
  52. can be found in @cite Muja2009 .
  53. */
  54. namespace cvflann
  55. {
  56. CV_EXPORTS flann_distance_t flann_distance_type();
  57. FLANN_DEPRECATED CV_EXPORTS void set_distance_type(flann_distance_t distance_type, int order);
  58. }
  59. namespace cv
  60. {
  61. namespace flann
  62. {
  63. //! @addtogroup flann
  64. //! @{
  65. template <typename T> struct CvType {};
  66. template <> struct CvType<unsigned char> { static int type() { return CV_8U; } };
  67. template <> struct CvType<char> { static int type() { return CV_8S; } };
  68. template <> struct CvType<unsigned short> { static int type() { return CV_16U; } };
  69. template <> struct CvType<short> { static int type() { return CV_16S; } };
  70. template <> struct CvType<int> { static int type() { return CV_32S; } };
  71. template <> struct CvType<float> { static int type() { return CV_32F; } };
  72. template <> struct CvType<double> { static int type() { return CV_64F; } };
  73. // bring the flann parameters into this namespace
  74. using ::cvflann::get_param;
  75. using ::cvflann::print_params;
  76. // bring the flann distances into this namespace
  77. using ::cvflann::L2_Simple;
  78. using ::cvflann::L2;
  79. using ::cvflann::L1;
  80. using ::cvflann::MinkowskiDistance;
  81. using ::cvflann::MaxDistance;
  82. using ::cvflann::HammingLUT;
  83. using ::cvflann::Hamming;
  84. using ::cvflann::Hamming2;
  85. using ::cvflann::HistIntersectionDistance;
  86. using ::cvflann::HellingerDistance;
  87. using ::cvflann::ChiSquareDistance;
  88. using ::cvflann::KL_Divergence;
  89. /** @brief The FLANN nearest neighbor index class. This class is templated with the type of elements for which
  90. the index is built.
  91. */
  92. template <typename Distance>
  93. class GenericIndex
  94. {
  95. public:
  96. typedef typename Distance::ElementType ElementType;
  97. typedef typename Distance::ResultType DistanceType;
  98. /** @brief Constructs a nearest neighbor search index for a given dataset.
  99. @param features Matrix of containing the features(points) to index. The size of the matrix is
  100. num_features x feature_dimensionality and the data type of the elements in the matrix must
  101. coincide with the type of the index.
  102. @param params Structure containing the index parameters. The type of index that will be
  103. constructed depends on the type of this parameter. See the description.
  104. @param distance
  105. The method constructs a fast search structure from a set of features using the specified algorithm
  106. with specified parameters, as defined by params. params is a reference to one of the following class
  107. IndexParams descendants:
  108. - **LinearIndexParams** When passing an object of this type, the index will perform a linear,
  109. brute-force search. :
  110. @code
  111. struct LinearIndexParams : public IndexParams
  112. {
  113. };
  114. @endcode
  115. - **KDTreeIndexParams** When passing an object of this type the index constructed will consist of
  116. a set of randomized kd-trees which will be searched in parallel. :
  117. @code
  118. struct KDTreeIndexParams : public IndexParams
  119. {
  120. KDTreeIndexParams( int trees = 4 );
  121. };
  122. @endcode
  123. - **KMeansIndexParams** When passing an object of this type the index constructed will be a
  124. hierarchical k-means tree. :
  125. @code
  126. struct KMeansIndexParams : public IndexParams
  127. {
  128. KMeansIndexParams(
  129. int branching = 32,
  130. int iterations = 11,
  131. flann_centers_init_t centers_init = CENTERS_RANDOM,
  132. float cb_index = 0.2 );
  133. };
  134. @endcode
  135. - **CompositeIndexParams** When using a parameters object of this type the index created
  136. combines the randomized kd-trees and the hierarchical k-means tree. :
  137. @code
  138. struct CompositeIndexParams : public IndexParams
  139. {
  140. CompositeIndexParams(
  141. int trees = 4,
  142. int branching = 32,
  143. int iterations = 11,
  144. flann_centers_init_t centers_init = CENTERS_RANDOM,
  145. float cb_index = 0.2 );
  146. };
  147. @endcode
  148. - **LshIndexParams** When using a parameters object of this type the index created uses
  149. multi-probe LSH (by Multi-Probe LSH: Efficient Indexing for High-Dimensional Similarity Search
  150. by Qin Lv, William Josephson, Zhe Wang, Moses Charikar, Kai Li., Proceedings of the 33rd
  151. International Conference on Very Large Data Bases (VLDB). Vienna, Austria. September 2007) :
  152. @code
  153. struct LshIndexParams : public IndexParams
  154. {
  155. LshIndexParams(
  156. unsigned int table_number,
  157. unsigned int key_size,
  158. unsigned int multi_probe_level );
  159. };
  160. @endcode
  161. - **AutotunedIndexParams** When passing an object of this type the index created is
  162. automatically tuned to offer the best performance, by choosing the optimal index type
  163. (randomized kd-trees, hierarchical kmeans, linear) and parameters for the dataset provided. :
  164. @code
  165. struct AutotunedIndexParams : public IndexParams
  166. {
  167. AutotunedIndexParams(
  168. float target_precision = 0.9,
  169. float build_weight = 0.01,
  170. float memory_weight = 0,
  171. float sample_fraction = 0.1 );
  172. };
  173. @endcode
  174. - **SavedIndexParams** This object type is used for loading a previously saved index from the
  175. disk. :
  176. @code
  177. struct SavedIndexParams : public IndexParams
  178. {
  179. SavedIndexParams( String filename );
  180. };
  181. @endcode
  182. */
  183. GenericIndex(const Mat& features, const ::cvflann::IndexParams& params, Distance distance = Distance());
  184. ~GenericIndex();
  185. /** @brief Performs a K-nearest neighbor search for a given query point using the index.
  186. @param query The query point
  187. @param indices Vector that will contain the indices of the K-nearest neighbors found. It must have
  188. at least knn size.
  189. @param dists Vector that will contain the distances to the K-nearest neighbors found. It must have
  190. at least knn size.
  191. @param knn Number of nearest neighbors to search for.
  192. @param params SearchParams
  193. */
  194. void knnSearch(const std::vector<ElementType>& query, std::vector<int>& indices,
  195. std::vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& params);
  196. void knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& params);
  197. int radiusSearch(const std::vector<ElementType>& query, std::vector<int>& indices,
  198. std::vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& params);
  199. int radiusSearch(const Mat& query, Mat& indices, Mat& dists,
  200. DistanceType radius, const ::cvflann::SearchParams& params);
  201. void save(String filename) { nnIndex->save(filename); }
  202. int veclen() const { return nnIndex->veclen(); }
  203. int size() const { return nnIndex->size(); }
  204. ::cvflann::IndexParams getParameters() { return nnIndex->getParameters(); }
  205. FLANN_DEPRECATED const ::cvflann::IndexParams* getIndexParameters() { return nnIndex->getIndexParameters(); }
  206. private:
  207. ::cvflann::Index<Distance>* nnIndex;
  208. };
  209. //! @cond IGNORED
  210. #define FLANN_DISTANCE_CHECK \
  211. if ( ::cvflann::flann_distance_type() != cvflann::FLANN_DIST_L2) { \
  212. printf("[WARNING] You are using cv::flann::Index (or cv::flann::GenericIndex) and have also changed "\
  213. "the distance using cvflann::set_distance_type. This is no longer working as expected "\
  214. "(cv::flann::Index always uses L2). You should create the index templated on the distance, "\
  215. "for example for L1 distance use: GenericIndex< L1<float> > \n"); \
  216. }
  217. template <typename Distance>
  218. GenericIndex<Distance>::GenericIndex(const Mat& dataset, const ::cvflann::IndexParams& params, Distance distance)
  219. {
  220. CV_Assert(dataset.type() == CvType<ElementType>::type());
  221. CV_Assert(dataset.isContinuous());
  222. ::cvflann::Matrix<ElementType> m_dataset((ElementType*)dataset.ptr<ElementType>(0), dataset.rows, dataset.cols);
  223. nnIndex = new ::cvflann::Index<Distance>(m_dataset, params, distance);
  224. FLANN_DISTANCE_CHECK
  225. nnIndex->buildIndex();
  226. }
  227. template <typename Distance>
  228. GenericIndex<Distance>::~GenericIndex()
  229. {
  230. delete nnIndex;
  231. }
  232. template <typename Distance>
  233. void GenericIndex<Distance>::knnSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& searchParams)
  234. {
  235. ::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
  236. ::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
  237. ::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
  238. FLANN_DISTANCE_CHECK
  239. nnIndex->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
  240. }
  241. template <typename Distance>
  242. void GenericIndex<Distance>::knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& searchParams)
  243. {
  244. CV_Assert(queries.type() == CvType<ElementType>::type());
  245. CV_Assert(queries.isContinuous());
  246. ::cvflann::Matrix<ElementType> m_queries((ElementType*)queries.ptr<ElementType>(0), queries.rows, queries.cols);
  247. CV_Assert(indices.type() == CV_32S);
  248. CV_Assert(indices.isContinuous());
  249. ::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
  250. CV_Assert(dists.type() == CvType<DistanceType>::type());
  251. CV_Assert(dists.isContinuous());
  252. ::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
  253. FLANN_DISTANCE_CHECK
  254. nnIndex->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
  255. }
  256. template <typename Distance>
  257. int GenericIndex<Distance>::radiusSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
  258. {
  259. ::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
  260. ::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
  261. ::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
  262. FLANN_DISTANCE_CHECK
  263. return nnIndex->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
  264. }
  265. template <typename Distance>
  266. int GenericIndex<Distance>::radiusSearch(const Mat& query, Mat& indices, Mat& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
  267. {
  268. CV_Assert(query.type() == CvType<ElementType>::type());
  269. CV_Assert(query.isContinuous());
  270. ::cvflann::Matrix<ElementType> m_query((ElementType*)query.ptr<ElementType>(0), query.rows, query.cols);
  271. CV_Assert(indices.type() == CV_32S);
  272. CV_Assert(indices.isContinuous());
  273. ::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
  274. CV_Assert(dists.type() == CvType<DistanceType>::type());
  275. CV_Assert(dists.isContinuous());
  276. ::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
  277. FLANN_DISTANCE_CHECK
  278. return nnIndex->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
  279. }
  280. //! @endcond
  281. /**
  282. * @deprecated Use GenericIndex class instead
  283. */
  284. template <typename T>
  285. class
  286. #ifndef _MSC_VER
  287. FLANN_DEPRECATED
  288. #endif
  289. Index_ {
  290. public:
  291. typedef typename L2<T>::ElementType ElementType;
  292. typedef typename L2<T>::ResultType DistanceType;
  293. Index_(const Mat& features, const ::cvflann::IndexParams& params);
  294. ~Index_();
  295. void knnSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& params);
  296. void knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& params);
  297. int radiusSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& params);
  298. int radiusSearch(const Mat& query, Mat& indices, Mat& dists, DistanceType radius, const ::cvflann::SearchParams& params);
  299. void save(String filename)
  300. {
  301. if (nnIndex_L1) nnIndex_L1->save(filename);
  302. if (nnIndex_L2) nnIndex_L2->save(filename);
  303. }
  304. int veclen() const
  305. {
  306. if (nnIndex_L1) return nnIndex_L1->veclen();
  307. if (nnIndex_L2) return nnIndex_L2->veclen();
  308. }
  309. int size() const
  310. {
  311. if (nnIndex_L1) return nnIndex_L1->size();
  312. if (nnIndex_L2) return nnIndex_L2->size();
  313. }
  314. ::cvflann::IndexParams getParameters()
  315. {
  316. if (nnIndex_L1) return nnIndex_L1->getParameters();
  317. if (nnIndex_L2) return nnIndex_L2->getParameters();
  318. }
  319. FLANN_DEPRECATED const ::cvflann::IndexParams* getIndexParameters()
  320. {
  321. if (nnIndex_L1) return nnIndex_L1->getIndexParameters();
  322. if (nnIndex_L2) return nnIndex_L2->getIndexParameters();
  323. }
  324. private:
  325. // providing backwards compatibility for L2 and L1 distances (most common)
  326. ::cvflann::Index< L2<ElementType> >* nnIndex_L2;
  327. ::cvflann::Index< L1<ElementType> >* nnIndex_L1;
  328. };
  329. #ifdef _MSC_VER
  330. template <typename T>
  331. class FLANN_DEPRECATED Index_;
  332. #endif
  333. //! @cond IGNORED
  334. template <typename T>
  335. Index_<T>::Index_(const Mat& dataset, const ::cvflann::IndexParams& params)
  336. {
  337. printf("[WARNING] The cv::flann::Index_<T> class is deperecated, use cv::flann::GenericIndex<Distance> instead\n");
  338. CV_Assert(dataset.type() == CvType<ElementType>::type());
  339. CV_Assert(dataset.isContinuous());
  340. ::cvflann::Matrix<ElementType> m_dataset((ElementType*)dataset.ptr<ElementType>(0), dataset.rows, dataset.cols);
  341. if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L2 ) {
  342. nnIndex_L1 = NULL;
  343. nnIndex_L2 = new ::cvflann::Index< L2<ElementType> >(m_dataset, params);
  344. }
  345. else if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L1 ) {
  346. nnIndex_L1 = new ::cvflann::Index< L1<ElementType> >(m_dataset, params);
  347. nnIndex_L2 = NULL;
  348. }
  349. else {
  350. printf("[ERROR] cv::flann::Index_<T> only provides backwards compatibility for the L1 and L2 distances. "
  351. "For other distance types you must use cv::flann::GenericIndex<Distance>\n");
  352. CV_Assert(0);
  353. }
  354. if (nnIndex_L1) nnIndex_L1->buildIndex();
  355. if (nnIndex_L2) nnIndex_L2->buildIndex();
  356. }
  357. template <typename T>
  358. Index_<T>::~Index_()
  359. {
  360. if (nnIndex_L1) delete nnIndex_L1;
  361. if (nnIndex_L2) delete nnIndex_L2;
  362. }
  363. template <typename T>
  364. void Index_<T>::knnSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& searchParams)
  365. {
  366. ::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
  367. ::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
  368. ::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
  369. if (nnIndex_L1) nnIndex_L1->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
  370. if (nnIndex_L2) nnIndex_L2->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
  371. }
  372. template <typename T>
  373. void Index_<T>::knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& searchParams)
  374. {
  375. CV_Assert(queries.type() == CvType<ElementType>::type());
  376. CV_Assert(queries.isContinuous());
  377. ::cvflann::Matrix<ElementType> m_queries((ElementType*)queries.ptr<ElementType>(0), queries.rows, queries.cols);
  378. CV_Assert(indices.type() == CV_32S);
  379. CV_Assert(indices.isContinuous());
  380. ::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
  381. CV_Assert(dists.type() == CvType<DistanceType>::type());
  382. CV_Assert(dists.isContinuous());
  383. ::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
  384. if (nnIndex_L1) nnIndex_L1->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
  385. if (nnIndex_L2) nnIndex_L2->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
  386. }
  387. template <typename T>
  388. int Index_<T>::radiusSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
  389. {
  390. ::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
  391. ::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
  392. ::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
  393. if (nnIndex_L1) return nnIndex_L1->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
  394. if (nnIndex_L2) return nnIndex_L2->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
  395. }
  396. template <typename T>
  397. int Index_<T>::radiusSearch(const Mat& query, Mat& indices, Mat& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
  398. {
  399. CV_Assert(query.type() == CvType<ElementType>::type());
  400. CV_Assert(query.isContinuous());
  401. ::cvflann::Matrix<ElementType> m_query((ElementType*)query.ptr<ElementType>(0), query.rows, query.cols);
  402. CV_Assert(indices.type() == CV_32S);
  403. CV_Assert(indices.isContinuous());
  404. ::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
  405. CV_Assert(dists.type() == CvType<DistanceType>::type());
  406. CV_Assert(dists.isContinuous());
  407. ::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
  408. if (nnIndex_L1) return nnIndex_L1->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
  409. if (nnIndex_L2) return nnIndex_L2->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
  410. }
  411. //! @endcond
  412. /** @brief Clusters features using hierarchical k-means algorithm.
  413. @param features The points to be clustered. The matrix must have elements of type
  414. Distance::ElementType.
  415. @param centers The centers of the clusters obtained. The matrix must have type
  416. Distance::ResultType. The number of rows in this matrix represents the number of clusters desired,
  417. however, because of the way the cut in the hierarchical tree is chosen, the number of clusters
  418. computed will be the highest number of the form (branching-1)\*k+1 that's lower than the number of
  419. clusters desired, where branching is the tree's branching factor (see description of the
  420. KMeansIndexParams).
  421. @param params Parameters used in the construction of the hierarchical k-means tree.
  422. @param d Distance to be used for clustering.
  423. The method clusters the given feature vectors by constructing a hierarchical k-means tree and
  424. choosing a cut in the tree that minimizes the cluster's variance. It returns the number of clusters
  425. found.
  426. */
  427. template <typename Distance>
  428. int hierarchicalClustering(const Mat& features, Mat& centers, const ::cvflann::KMeansIndexParams& params,
  429. Distance d = Distance())
  430. {
  431. typedef typename Distance::ElementType ElementType;
  432. typedef typename Distance::ResultType DistanceType;
  433. CV_Assert(features.type() == CvType<ElementType>::type());
  434. CV_Assert(features.isContinuous());
  435. ::cvflann::Matrix<ElementType> m_features((ElementType*)features.ptr<ElementType>(0), features.rows, features.cols);
  436. CV_Assert(centers.type() == CvType<DistanceType>::type());
  437. CV_Assert(centers.isContinuous());
  438. ::cvflann::Matrix<DistanceType> m_centers((DistanceType*)centers.ptr<DistanceType>(0), centers.rows, centers.cols);
  439. return ::cvflann::hierarchicalClustering<Distance>(m_features, m_centers, params, d);
  440. }
  441. /** @deprecated
  442. */
  443. template <typename ELEM_TYPE, typename DIST_TYPE>
  444. FLANN_DEPRECATED int hierarchicalClustering(const Mat& features, Mat& centers, const ::cvflann::KMeansIndexParams& params)
  445. {
  446. printf("[WARNING] cv::flann::hierarchicalClustering<ELEM_TYPE,DIST_TYPE> is deprecated, use "
  447. "cv::flann::hierarchicalClustering<Distance> instead\n");
  448. if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L2 ) {
  449. return hierarchicalClustering< L2<ELEM_TYPE> >(features, centers, params);
  450. }
  451. else if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L1 ) {
  452. return hierarchicalClustering< L1<ELEM_TYPE> >(features, centers, params);
  453. }
  454. else {
  455. printf("[ERROR] cv::flann::hierarchicalClustering<ELEM_TYPE,DIST_TYPE> only provides backwards "
  456. "compatibility for the L1 and L2 distances. "
  457. "For other distance types you must use cv::flann::hierarchicalClustering<Distance>\n");
  458. CV_Assert(0);
  459. }
  460. }
  461. //! @} flann
  462. } } // namespace cv::flann
  463. #endif