Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/flann/include/opencv2/flann.hpp
16358 views
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
50
/**
51
@defgroup flann Clustering and Search in Multi-Dimensional Spaces
52
53
This section documents OpenCV's interface to the FLANN library. FLANN (Fast Library for Approximate
54
Nearest Neighbors) is a library that contains a collection of algorithms optimized for fast nearest
55
neighbor search in large datasets and for high dimensional features. More information about FLANN
56
can be found in @cite Muja2009 .
57
*/
58
59
namespace cvflann
60
{
61
CV_EXPORTS flann_distance_t flann_distance_type();
62
CV_DEPRECATED CV_EXPORTS void set_distance_type(flann_distance_t distance_type, int order);
63
}
64
65
66
namespace cv
67
{
68
namespace flann
69
{
70
71
72
//! @addtogroup flann
73
//! @{
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;
95
using ::cvflann::HammingLUT;
96
using ::cvflann::Hamming;
97
using ::cvflann::Hamming2;
98
using ::cvflann::HistIntersectionDistance;
99
using ::cvflann::HellingerDistance;
100
using ::cvflann::ChiSquareDistance;
101
using ::cvflann::KL_Divergence;
102
103
104
/** @brief The FLANN nearest neighbor index class. This class is templated with the type of elements for which
105
the index is built.
106
107
`Distance` functor specifies the metric to be used to calculate the distance between two points.
108
There are several `Distance` functors that are readily available:
109
110
@link cvflann::L2_Simple cv::flann::L2_Simple @endlink- Squared Euclidean distance functor.
111
This is the simpler, unrolled version. This is preferable for very low dimensionality data (eg 3D points)
112
113
@link cvflann::L2 cv::flann::L2 @endlink- Squared Euclidean distance functor, optimized version.
114
115
@link cvflann::L1 cv::flann::L1 @endlink - Manhattan distance functor, optimized version.
116
117
@link cvflann::MinkowskiDistance cv::flann::MinkowskiDistance @endlink - The Minkowsky distance functor.
118
This is highly optimised with loop unrolling.
119
The computation of squared root at the end is omitted for efficiency.
120
121
@link cvflann::MaxDistance cv::flann::MaxDistance @endlink - The max distance functor. It computes the
122
maximum distance between two vectors. This distance is not a valid kdtree distance, it's not
123
dimensionwise additive.
124
125
@link cvflann::HammingLUT cv::flann::HammingLUT @endlink - %Hamming distance functor. It counts the bit
126
differences between two strings using a lookup table implementation.
127
128
@link cvflann::Hamming cv::flann::Hamming @endlink - %Hamming distance functor. Population count is
129
performed using library calls, if available. Lookup table implementation is used as a fallback.
130
131
@link cvflann::Hamming2 cv::flann::Hamming2 @endlink- %Hamming distance functor. Population count is
132
implemented in 12 arithmetic operations (one of which is multiplication).
133
134
@link cvflann::HistIntersectionDistance cv::flann::HistIntersectionDistance @endlink - The histogram
135
intersection distance functor.
136
137
@link cvflann::HellingerDistance cv::flann::HellingerDistance @endlink - The Hellinger distance functor.
138
139
@link cvflann::ChiSquareDistance cv::flann::ChiSquareDistance @endlink - The chi-square distance functor.
140
141
@link cvflann::KL_Divergence cv::flann::KL_Divergence @endlink - The Kullback-Leibler divergence functor.
142
143
Although the provided implementations cover a vast range of cases, it is also possible to use
144
a custom implementation. The distance functor is a class whose `operator()` computes the distance
145
between two features. If the distance is also a kd-tree compatible distance, it should also provide an
146
`accum_dist()` method that computes the distance between individual feature dimensions.
147
148
In addition to `operator()` and `accum_dist()`, a distance functor should also define the
149
`ElementType` and the `ResultType` as the types of the elements it operates on and the type of the
150
result it computes. If a distance functor can be used as a kd-tree distance (meaning that the full
151
distance between a pair of features can be accumulated from the partial distances between the
152
individual dimensions) a typedef `is_kdtree_distance` should be present inside the distance functor.
153
If the distance is not a kd-tree distance, but it's a distance in a vector space (the individual
154
dimensions of the elements it operates on can be accessed independently) a typedef
155
`is_vector_space_distance` should be defined inside the functor. If neither typedef is defined, the
156
distance is assumed to be a metric distance and will only be used with indexes operating on
157
generic metric distances.
158
*/
159
template <typename Distance>
160
class GenericIndex
161
{
162
public:
163
typedef typename Distance::ElementType ElementType;
164
typedef typename Distance::ResultType DistanceType;
165
166
/** @brief Constructs a nearest neighbor search index for a given dataset.
167
168
@param features Matrix of containing the features(points) to index. The size of the matrix is
169
num_features x feature_dimensionality and the data type of the elements in the matrix must
170
coincide with the type of the index.
171
@param params Structure containing the index parameters. The type of index that will be
172
constructed depends on the type of this parameter. See the description.
173
@param distance
174
175
The method constructs a fast search structure from a set of features using the specified algorithm
176
with specified parameters, as defined by params. params is a reference to one of the following class
177
IndexParams descendants:
178
179
- **LinearIndexParams** When passing an object of this type, the index will perform a linear,
180
brute-force search. :
181
@code
182
struct LinearIndexParams : public IndexParams
183
{
184
};
185
@endcode
186
- **KDTreeIndexParams** When passing an object of this type the index constructed will consist of
187
a set of randomized kd-trees which will be searched in parallel. :
188
@code
189
struct KDTreeIndexParams : public IndexParams
190
{
191
KDTreeIndexParams( int trees = 4 );
192
};
193
@endcode
194
- **KMeansIndexParams** When passing an object of this type the index constructed will be a
195
hierarchical k-means tree. :
196
@code
197
struct KMeansIndexParams : public IndexParams
198
{
199
KMeansIndexParams(
200
int branching = 32,
201
int iterations = 11,
202
flann_centers_init_t centers_init = CENTERS_RANDOM,
203
float cb_index = 0.2 );
204
};
205
@endcode
206
- **CompositeIndexParams** When using a parameters object of this type the index created
207
combines the randomized kd-trees and the hierarchical k-means tree. :
208
@code
209
struct CompositeIndexParams : public IndexParams
210
{
211
CompositeIndexParams(
212
int trees = 4,
213
int branching = 32,
214
int iterations = 11,
215
flann_centers_init_t centers_init = CENTERS_RANDOM,
216
float cb_index = 0.2 );
217
};
218
@endcode
219
- **LshIndexParams** When using a parameters object of this type the index created uses
220
multi-probe LSH (by Multi-Probe LSH: Efficient Indexing for High-Dimensional Similarity Search
221
by Qin Lv, William Josephson, Zhe Wang, Moses Charikar, Kai Li., Proceedings of the 33rd
222
International Conference on Very Large Data Bases (VLDB). Vienna, Austria. September 2007) :
223
@code
224
struct LshIndexParams : public IndexParams
225
{
226
LshIndexParams(
227
unsigned int table_number,
228
unsigned int key_size,
229
unsigned int multi_probe_level );
230
};
231
@endcode
232
- **AutotunedIndexParams** When passing an object of this type the index created is
233
automatically tuned to offer the best performance, by choosing the optimal index type
234
(randomized kd-trees, hierarchical kmeans, linear) and parameters for the dataset provided. :
235
@code
236
struct AutotunedIndexParams : public IndexParams
237
{
238
AutotunedIndexParams(
239
float target_precision = 0.9,
240
float build_weight = 0.01,
241
float memory_weight = 0,
242
float sample_fraction = 0.1 );
243
};
244
@endcode
245
- **SavedIndexParams** This object type is used for loading a previously saved index from the
246
disk. :
247
@code
248
struct SavedIndexParams : public IndexParams
249
{
250
SavedIndexParams( String filename );
251
};
252
@endcode
253
*/
254
GenericIndex(const Mat& features, const ::cvflann::IndexParams& params, Distance distance = Distance());
255
256
~GenericIndex();
257
258
/** @brief Performs a K-nearest neighbor search for a given query point using the index.
259
260
@param query The query point
261
@param indices Vector that will contain the indices of the K-nearest neighbors found. It must have
262
at least knn size.
263
@param dists Vector that will contain the distances to the K-nearest neighbors found. It must have
264
at least knn size.
265
@param knn Number of nearest neighbors to search for.
266
@param params SearchParams
267
*/
268
void knnSearch(const std::vector<ElementType>& query, std::vector<int>& indices,
269
std::vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& params);
270
void knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& params);
271
272
/** @brief Performs a radius nearest neighbor search for a given query point using the index.
273
274
@param query The query point.
275
@param indices Vector that will contain the indices of the nearest neighbors found.
276
@param dists Vector that will contain the distances to the nearest neighbors found. It has the same
277
number of elements as indices.
278
@param radius The search radius.
279
@param params SearchParams
280
281
This function returns the number of nearest neighbors found.
282
*/
283
int radiusSearch(const std::vector<ElementType>& query, std::vector<int>& indices,
284
std::vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& params);
285
int radiusSearch(const Mat& query, Mat& indices, Mat& dists,
286
DistanceType radius, const ::cvflann::SearchParams& params);
287
288
void save(String filename) { nnIndex->save(filename); }
289
290
int veclen() const { return nnIndex->veclen(); }
291
292
int size() const { return nnIndex->size(); }
293
294
::cvflann::IndexParams getParameters() { return nnIndex->getParameters(); }
295
296
CV_DEPRECATED const ::cvflann::IndexParams* getIndexParameters() { return nnIndex->getIndexParameters(); }
297
298
private:
299
::cvflann::Index<Distance>* nnIndex;
300
};
301
302
//! @cond IGNORED
303
304
#define FLANN_DISTANCE_CHECK \
305
if ( ::cvflann::flann_distance_type() != cvflann::FLANN_DIST_L2) { \
306
printf("[WARNING] You are using cv::flann::Index (or cv::flann::GenericIndex) and have also changed "\
307
"the distance using cvflann::set_distance_type. This is no longer working as expected "\
308
"(cv::flann::Index always uses L2). You should create the index templated on the distance, "\
309
"for example for L1 distance use: GenericIndex< L1<float> > \n"); \
310
}
311
312
313
template <typename Distance>
314
GenericIndex<Distance>::GenericIndex(const Mat& dataset, const ::cvflann::IndexParams& params, Distance distance)
315
{
316
CV_Assert(dataset.type() == CvType<ElementType>::type());
317
CV_Assert(dataset.isContinuous());
318
::cvflann::Matrix<ElementType> m_dataset((ElementType*)dataset.ptr<ElementType>(0), dataset.rows, dataset.cols);
319
320
nnIndex = new ::cvflann::Index<Distance>(m_dataset, params, distance);
321
322
FLANN_DISTANCE_CHECK
323
324
nnIndex->buildIndex();
325
}
326
327
template <typename Distance>
328
GenericIndex<Distance>::~GenericIndex()
329
{
330
delete nnIndex;
331
}
332
333
template <typename Distance>
334
void GenericIndex<Distance>::knnSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& searchParams)
335
{
336
::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
337
::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
338
::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
339
340
FLANN_DISTANCE_CHECK
341
342
nnIndex->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
343
}
344
345
346
template <typename Distance>
347
void GenericIndex<Distance>::knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& searchParams)
348
{
349
CV_Assert(queries.type() == CvType<ElementType>::type());
350
CV_Assert(queries.isContinuous());
351
::cvflann::Matrix<ElementType> m_queries((ElementType*)queries.ptr<ElementType>(0), queries.rows, queries.cols);
352
353
CV_Assert(indices.type() == CV_32S);
354
CV_Assert(indices.isContinuous());
355
::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
356
357
CV_Assert(dists.type() == CvType<DistanceType>::type());
358
CV_Assert(dists.isContinuous());
359
::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
360
361
FLANN_DISTANCE_CHECK
362
363
nnIndex->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
364
}
365
366
template <typename Distance>
367
int GenericIndex<Distance>::radiusSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
368
{
369
::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
370
::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
371
::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
372
373
FLANN_DISTANCE_CHECK
374
375
return nnIndex->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
376
}
377
378
template <typename Distance>
379
int GenericIndex<Distance>::radiusSearch(const Mat& query, Mat& indices, Mat& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
380
{
381
CV_Assert(query.type() == CvType<ElementType>::type());
382
CV_Assert(query.isContinuous());
383
::cvflann::Matrix<ElementType> m_query((ElementType*)query.ptr<ElementType>(0), query.rows, query.cols);
384
385
CV_Assert(indices.type() == CV_32S);
386
CV_Assert(indices.isContinuous());
387
::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
388
389
CV_Assert(dists.type() == CvType<DistanceType>::type());
390
CV_Assert(dists.isContinuous());
391
::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
392
393
FLANN_DISTANCE_CHECK
394
395
return nnIndex->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
396
}
397
398
//! @endcond
399
400
/**
401
* @deprecated Use GenericIndex class instead
402
*/
403
template <typename T>
404
class Index_
405
{
406
public:
407
typedef typename L2<T>::ElementType ElementType;
408
typedef typename L2<T>::ResultType DistanceType;
409
410
CV_DEPRECATED Index_(const Mat& dataset, const ::cvflann::IndexParams& params)
411
{
412
printf("[WARNING] The cv::flann::Index_<T> class is deperecated, use cv::flann::GenericIndex<Distance> instead\n");
413
414
CV_Assert(dataset.type() == CvType<ElementType>::type());
415
CV_Assert(dataset.isContinuous());
416
::cvflann::Matrix<ElementType> m_dataset((ElementType*)dataset.ptr<ElementType>(0), dataset.rows, dataset.cols);
417
418
if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L2 ) {
419
nnIndex_L1 = NULL;
420
nnIndex_L2 = new ::cvflann::Index< L2<ElementType> >(m_dataset, params);
421
}
422
else if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L1 ) {
423
nnIndex_L1 = new ::cvflann::Index< L1<ElementType> >(m_dataset, params);
424
nnIndex_L2 = NULL;
425
}
426
else {
427
printf("[ERROR] cv::flann::Index_<T> only provides backwards compatibility for the L1 and L2 distances. "
428
"For other distance types you must use cv::flann::GenericIndex<Distance>\n");
429
CV_Assert(0);
430
}
431
if (nnIndex_L1) nnIndex_L1->buildIndex();
432
if (nnIndex_L2) nnIndex_L2->buildIndex();
433
}
434
CV_DEPRECATED ~Index_()
435
{
436
if (nnIndex_L1) delete nnIndex_L1;
437
if (nnIndex_L2) delete nnIndex_L2;
438
}
439
440
CV_DEPRECATED void knnSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& searchParams)
441
{
442
::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
443
::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
444
::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
445
446
if (nnIndex_L1) nnIndex_L1->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
447
if (nnIndex_L2) nnIndex_L2->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
448
}
449
CV_DEPRECATED void knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& searchParams)
450
{
451
CV_Assert(queries.type() == CvType<ElementType>::type());
452
CV_Assert(queries.isContinuous());
453
::cvflann::Matrix<ElementType> m_queries((ElementType*)queries.ptr<ElementType>(0), queries.rows, queries.cols);
454
455
CV_Assert(indices.type() == CV_32S);
456
CV_Assert(indices.isContinuous());
457
::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
458
459
CV_Assert(dists.type() == CvType<DistanceType>::type());
460
CV_Assert(dists.isContinuous());
461
::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
462
463
if (nnIndex_L1) nnIndex_L1->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
464
if (nnIndex_L2) nnIndex_L2->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
465
}
466
467
CV_DEPRECATED int radiusSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
468
{
469
::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
470
::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
471
::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
472
473
if (nnIndex_L1) return nnIndex_L1->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
474
if (nnIndex_L2) return nnIndex_L2->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
475
}
476
477
CV_DEPRECATED int radiusSearch(const Mat& query, Mat& indices, Mat& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
478
{
479
CV_Assert(query.type() == CvType<ElementType>::type());
480
CV_Assert(query.isContinuous());
481
::cvflann::Matrix<ElementType> m_query((ElementType*)query.ptr<ElementType>(0), query.rows, query.cols);
482
483
CV_Assert(indices.type() == CV_32S);
484
CV_Assert(indices.isContinuous());
485
::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
486
487
CV_Assert(dists.type() == CvType<DistanceType>::type());
488
CV_Assert(dists.isContinuous());
489
::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
490
491
if (nnIndex_L1) return nnIndex_L1->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
492
if (nnIndex_L2) return nnIndex_L2->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
493
}
494
495
CV_DEPRECATED void save(String filename)
496
{
497
if (nnIndex_L1) nnIndex_L1->save(filename);
498
if (nnIndex_L2) nnIndex_L2->save(filename);
499
}
500
501
CV_DEPRECATED int veclen() const
502
{
503
if (nnIndex_L1) return nnIndex_L1->veclen();
504
if (nnIndex_L2) return nnIndex_L2->veclen();
505
}
506
507
CV_DEPRECATED int size() const
508
{
509
if (nnIndex_L1) return nnIndex_L1->size();
510
if (nnIndex_L2) return nnIndex_L2->size();
511
}
512
513
CV_DEPRECATED ::cvflann::IndexParams getParameters()
514
{
515
if (nnIndex_L1) return nnIndex_L1->getParameters();
516
if (nnIndex_L2) return nnIndex_L2->getParameters();
517
518
}
519
520
CV_DEPRECATED const ::cvflann::IndexParams* getIndexParameters()
521
{
522
if (nnIndex_L1) return nnIndex_L1->getIndexParameters();
523
if (nnIndex_L2) return nnIndex_L2->getIndexParameters();
524
}
525
526
private:
527
// providing backwards compatibility for L2 and L1 distances (most common)
528
::cvflann::Index< L2<ElementType> >* nnIndex_L2;
529
::cvflann::Index< L1<ElementType> >* nnIndex_L1;
530
};
531
532
533
/** @brief Clusters features using hierarchical k-means algorithm.
534
535
@param features The points to be clustered. The matrix must have elements of type
536
Distance::ElementType.
537
@param centers The centers of the clusters obtained. The matrix must have type
538
Distance::ResultType. The number of rows in this matrix represents the number of clusters desired,
539
however, because of the way the cut in the hierarchical tree is chosen, the number of clusters
540
computed will be the highest number of the form (branching-1)\*k+1 that's lower than the number of
541
clusters desired, where branching is the tree's branching factor (see description of the
542
KMeansIndexParams).
543
@param params Parameters used in the construction of the hierarchical k-means tree.
544
@param d Distance to be used for clustering.
545
546
The method clusters the given feature vectors by constructing a hierarchical k-means tree and
547
choosing a cut in the tree that minimizes the cluster's variance. It returns the number of clusters
548
found.
549
*/
550
template <typename Distance>
551
int hierarchicalClustering(const Mat& features, Mat& centers, const ::cvflann::KMeansIndexParams& params,
552
Distance d = Distance())
553
{
554
typedef typename Distance::ElementType ElementType;
555
typedef typename Distance::ResultType DistanceType;
556
557
CV_Assert(features.type() == CvType<ElementType>::type());
558
CV_Assert(features.isContinuous());
559
::cvflann::Matrix<ElementType> m_features((ElementType*)features.ptr<ElementType>(0), features.rows, features.cols);
560
561
CV_Assert(centers.type() == CvType<DistanceType>::type());
562
CV_Assert(centers.isContinuous());
563
::cvflann::Matrix<DistanceType> m_centers((DistanceType*)centers.ptr<DistanceType>(0), centers.rows, centers.cols);
564
565
return ::cvflann::hierarchicalClustering<Distance>(m_features, m_centers, params, d);
566
}
567
568
/** @deprecated
569
*/
570
template <typename ELEM_TYPE, typename DIST_TYPE>
571
CV_DEPRECATED int hierarchicalClustering(const Mat& features, Mat& centers, const ::cvflann::KMeansIndexParams& params)
572
{
573
printf("[WARNING] cv::flann::hierarchicalClustering<ELEM_TYPE,DIST_TYPE> is deprecated, use "
574
"cv::flann::hierarchicalClustering<Distance> instead\n");
575
576
if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L2 ) {
577
return hierarchicalClustering< L2<ELEM_TYPE> >(features, centers, params);
578
}
579
else if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L1 ) {
580
return hierarchicalClustering< L1<ELEM_TYPE> >(features, centers, params);
581
}
582
else {
583
printf("[ERROR] cv::flann::hierarchicalClustering<ELEM_TYPE,DIST_TYPE> only provides backwards "
584
"compatibility for the L1 and L2 distances. "
585
"For other distance types you must use cv::flann::hierarchicalClustering<Distance>\n");
586
CV_Assert(0);
587
}
588
}
589
590
//! @} flann
591
592
} } // namespace cv::flann
593
594
#endif
595
596