43 #ifndef OPENCV_FLANN_HPP
44 #define OPENCV_FLANN_HPP
46 #include "opencv2/core.hpp"
47 #include "opencv2/flann/miniflann.hpp"
48 #include "opencv2/flann/flann_base.hpp"
75 template <
typename T>
struct CvType {};
86 using ::cvflann::get_param;
87 using ::cvflann::print_params;
90 using ::cvflann::L2_Simple;
93 using ::cvflann::MinkowskiDistance;
94 using ::cvflann::MaxDistance;
96 using ::cvflann::Hamming;
97 using ::cvflann::Hamming2;
98 using ::cvflann::DNAmmingLUT;
99 using ::cvflann::DNAmming2;
100 using ::cvflann::HistIntersectionDistance;
101 using ::cvflann::HellingerDistance;
102 using ::cvflann::ChiSquareDistance;
103 using ::cvflann::KL_Divergence;
169 template <
typename Distance>
286 GenericIndex(
const Mat& features, const ::cvflann::IndexParams& params, Distance distance = Distance());
302 void knnSearch(
const Mat& queries,
Mat& indices,
Mat& dists,
int knn, const ::cvflann::SearchParams& params);
322 int veclen()
const {
return nnIndex->veclen(); }
324 int size()
const {
return (
int)nnIndex->size(); }
331 ::cvflann::Index<Distance>* nnIndex;
337 #define FLANN_DISTANCE_CHECK \
338 if ( ::cvflann::flann_distance_type() != cvflann::FLANN_DIST_L2) { \
339 printf("[WARNING] You are using cv::flann::Index (or cv::flann::GenericIndex) and have also changed "\
340 "the distance using cvflann::set_distance_type. This is no longer working as expected "\
341 "(cv::flann::Index always uses L2). You should create the index templated on the distance, "\
342 "for example for L1 distance use: GenericIndex< L1<float> > \n"); \
346 template <
typename Distance>
354 nnIndex = new ::cvflann::Index<Distance>(m_dataset, params, distance);
358 nnIndex->buildIndex();
361 template <
typename Distance>
367 template <
typename Distance>
370 ::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
371 ::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.
size());
372 ::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.
size());
376 nnIndex->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
380 template <
typename Distance>
385 ::cvflann::Matrix<ElementType> m_queries((ElementType*)queries.ptr<ElementType>(0), queries.rows, queries.cols);
389 ::cvflann::Matrix<int> m_indices((
int*)indices.ptr<
int>(0), indices.rows, indices.cols);
393 ::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
397 nnIndex->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
400 template <
typename Distance>
403 ::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
404 ::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.
size());
405 ::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.
size());
409 return nnIndex->radiusSearch(m_query,m_indices,m_dists,
radius,searchParams);
412 template <
typename Distance>
417 ::cvflann::Matrix<ElementType> m_query((ElementType*)query.ptr<ElementType>(0), query.rows, query.cols);
421 ::cvflann::Matrix<int> m_indices((
int*)indices.ptr<
int>(0), indices.rows, indices.cols);
425 ::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
429 return nnIndex->radiusSearch(m_query,m_indices,m_dists,
radius,searchParams);
435 template <
typename T>
439 typedef typename L2<T>::ElementType ElementType;
442 CV_DEPRECATED Index_(
const Mat& dataset, const ::cvflann::IndexParams& params)
444 printf(
"[WARNING] The cv::flann::Index_<T> class is deperecated, use cv::flann::GenericIndex<Distance> instead\n");
448 ::cvflann::Matrix<ElementType> m_dataset((ElementType*)dataset.ptr<ElementType>(0), dataset.rows, dataset.cols);
452 nnIndex_L2 = new ::cvflann::Index< L2<ElementType> >(m_dataset, params);
455 nnIndex_L1 = new ::cvflann::Index< L1<ElementType> >(m_dataset, params);
459 printf(
"[ERROR] cv::flann::Index_<T> only provides backwards compatibility for the L1 and L2 distances. "
460 "For other distance types you must use cv::flann::GenericIndex<Distance>\n");
463 if (nnIndex_L1) nnIndex_L1->buildIndex();
464 if (nnIndex_L2) nnIndex_L2->buildIndex();
468 if (nnIndex_L1)
delete nnIndex_L1;
469 if (nnIndex_L2)
delete nnIndex_L2;
474 ::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
475 ::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.
size());
476 ::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.
size());
478 if (nnIndex_L1) nnIndex_L1->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
479 if (nnIndex_L2) nnIndex_L2->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
481 CV_DEPRECATED void knnSearch(
const Mat& queries, Mat& indices, Mat& dists,
int knn, const ::cvflann::SearchParams& searchParams)
485 ::cvflann::Matrix<ElementType> m_queries((ElementType*)queries.ptr<ElementType>(0), queries.rows, queries.cols);
489 ::cvflann::Matrix<int> m_indices((
int*)indices.ptr<
int>(0), indices.rows, indices.cols);
493 ::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
495 if (nnIndex_L1) nnIndex_L1->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
496 if (nnIndex_L2) nnIndex_L2->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
501 ::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
502 ::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.
size());
503 ::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.
size());
505 if (nnIndex_L1)
return nnIndex_L1->radiusSearch(m_query,m_indices,m_dists,
radius,searchParams);
506 if (nnIndex_L2)
return nnIndex_L2->radiusSearch(m_query,m_indices,m_dists,
radius,searchParams);
509 CV_DEPRECATED int radiusSearch(
const Mat& query, Mat& indices, Mat& dists, DistanceType
radius, const ::cvflann::SearchParams& searchParams)
513 ::cvflann::Matrix<ElementType> m_query((ElementType*)query.ptr<ElementType>(0), query.rows, query.cols);
517 ::cvflann::Matrix<int> m_indices((
int*)indices.ptr<
int>(0), indices.rows, indices.cols);
521 ::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
523 if (nnIndex_L1)
return nnIndex_L1->radiusSearch(m_query,m_indices,m_dists,
radius,searchParams);
524 if (nnIndex_L2)
return nnIndex_L2->radiusSearch(m_query,m_indices,m_dists,
radius,searchParams);
529 if (nnIndex_L1) nnIndex_L1->save(
filename);
530 if (nnIndex_L2) nnIndex_L2->save(
filename);
535 if (nnIndex_L1)
return nnIndex_L1->veclen();
536 if (nnIndex_L2)
return nnIndex_L2->veclen();
541 if (nnIndex_L1)
return nnIndex_L1->size();
542 if (nnIndex_L2)
return nnIndex_L2->size();
545 CV_DEPRECATED ::cvflann::IndexParams getParameters()
547 if (nnIndex_L1)
return nnIndex_L1->getParameters();
548 if (nnIndex_L2)
return nnIndex_L2->getParameters();
552 CV_DEPRECATED const ::cvflann::IndexParams* getIndexParameters()
554 if (nnIndex_L1)
return nnIndex_L1->getIndexParameters();
555 if (nnIndex_L2)
return nnIndex_L2->getIndexParameters();
560 ::cvflann::Index< L2<ElementType> >* nnIndex_L2;
561 ::cvflann::Index< L1<ElementType> >* nnIndex_L1;
583 template <
typename Distance>
585 Distance d = Distance())
587 typedef typename Distance::ElementType ElementType;
588 typedef typename Distance::CentersType CentersType;
592 ::cvflann::Matrix<ElementType> m_features((ElementType*)features.
ptr<ElementType>(0), features.
rows, features.
cols);
596 ::cvflann::Matrix<CentersType> m_centers((CentersType*)centers.
ptr<CentersType>(0), centers.
rows, centers.
cols);
598 return ::cvflann::hierarchicalClustering<Distance>(m_features, m_centers, params, d);
603 template <
typename ELEM_TYPE,
typename DIST_TYPE>
606 printf(
"[WARNING] cv::flann::hierarchicalClustering<ELEM_TYPE,DIST_TYPE> is deprecated, use "
607 "cv::flann::hierarchicalClustering<Distance> instead\n");
610 return hierarchicalClustering< L2<ELEM_TYPE> >(features, centers, params);
613 return hierarchicalClustering< L1<ELEM_TYPE> >(features, centers, params);
616 printf(
"[ERROR] cv::flann::hierarchicalClustering<ELEM_TYPE,DIST_TYPE> only provides backwards "
617 "compatibility for the L1 and L2 distances. "
618 "For other distance types you must use cv::flann::hierarchicalClustering<Distance>\n");
n-dimensional dense array class
Definition: mat.hpp:812
uchar * ptr(int i0=0)
Returns a pointer to the specified matrix row.
int cols
Definition: mat.hpp:2138
bool isContinuous() const
Reports whether the matrix is continuous or not.
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
Definition: mat.hpp:2138
int type() const
Returns the type of a matrix element.
The FLANN nearest neighbor index class. This class is templated with the type of elements for which t...
Definition: flann.hpp:171
Distance::ElementType ElementType
Definition: flann.hpp:173
void knnSearch(const Mat &queries, Mat &indices, Mat &dists, int knn, const ::cvflann::SearchParams ¶ms)
void save(String filename)
Definition: flann.hpp:320
Distance::ResultType DistanceType
Definition: flann.hpp:174
void knnSearch(const std::vector< ElementType > &query, std::vector< int > &indices, std::vector< DistanceType > &dists, int knn, const ::cvflann::SearchParams ¶ms)
Performs a K-nearest neighbor search for a given query point using the index.
int veclen() const
Definition: flann.hpp:322
CV_DEPRECATEDconst ::cvflann::IndexParams * getIndexParameters()
Definition: flann.hpp:328
GenericIndex(const Mat &features, const ::cvflann::IndexParams ¶ms, Distance distance=Distance())
Constructs a nearest neighbor search index for a given dataset.
::cvflann::IndexParams getParameters()
Definition: flann.hpp:326
int radiusSearch(const std::vector< ElementType > &query, std::vector< int > &indices, std::vector< DistanceType > &dists, DistanceType radius, const ::cvflann::SearchParams ¶ms)
Performs a radius nearest neighbor search for a given query point using the index.
int radiusSearch(const Mat &query, Mat &indices, Mat &dists, DistanceType radius, const ::cvflann::SearchParams ¶ms)
int size() const
Definition: flann.hpp:324
std::string String
Definition: cvstd.hpp:151
CvArr int order
Definition: core_c.h:1311
CvSize size
Definition: core_c.h:112
int int type
Definition: core_c.h:221
const char const char ** filename
Definition: core_c.h:2630
#define CV_8S
Definition: interface.h:74
#define CV_64F
Definition: interface.h:79
#define CV_8U
Definition: interface.h:73
#define CV_32S
Definition: interface.h:77
#define CV_32F
Definition: interface.h:78
#define CV_16S
Definition: interface.h:76
#define CV_16U
Definition: interface.h:75
#define CV_EXPORTS
Definition: cvdef.h:435
#define CV_DEPRECATED
Definition: cvdef.h:450
Hamming HammingLUT
Definition: base.hpp:393
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails.
Definition: base.hpp:342
int hierarchicalClustering(const Mat &features, Mat ¢ers, const ::cvflann::KMeansIndexParams ¶ms, Distance d=Distance())
Clusters features using hierarchical k-means algorithm.
Definition: flann.hpp:584
CvPoint2D32f float * radius
Definition: imgproc_c.h:534
const CvArr int distance_type
Definition: imgproc_c.h:392
"black box" representation of the file storage associated with a file on disk.
Definition: calib3d.hpp:441
CV_EXPORTS flann_distance_t flann_distance_type()
CV_DEPRECATED CV_EXPORTS void set_distance_type(flann_distance_t distance_type, int order)
Accumulator< T >::Type ResultType
Definition: features2d.hpp:956
static int type()
Definition: flann.hpp:77
static int type()
Definition: flann.hpp:82
static int type()
Definition: flann.hpp:81
static int type()
Definition: flann.hpp:80
static int type()
Definition: flann.hpp:79
static int type()
Definition: flann.hpp:76
static int type()
Definition: flann.hpp:78