【计算机视觉】OpenCV的最近邻开源库FLANN

FLANN介绍

FLANN库全称是Fast Library for Approximate Nearest Neighbors,,它是目前最完整的(近似)最近邻开源库。不但实现了一系列查找算法,还包含了一种自动选取最快算法的机制。

flann::Index_类

该类模板是最近邻索引类,该类用于抽象不同类型的最近邻搜索的索引。 以下是flann::Index_类的声明:

template <typename T>class#ifndef _MSC_VER FLANN_DEPRECATED#endif Index_ {public:typedef typename L2<T>::ElementType ElementType;typedef typename L2<T>::ResultType DistanceType;Index_(const Mat& features, const ::cvflann::IndexParams& params);~Index_();void knnSearch(const vector<ElementType>& query, vector<int>& indices, vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& params);void knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& params);int radiusSearch(const vector<ElementType>& query, vector<int>& indices, vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& params);int radiusSearch(const Mat& query, Mat& indices, Mat& dists, DistanceType radius, const ::cvflann::SearchParams& params);void save(std::string filename){if (nnIndex_L1) nnIndex_L1->save(filename);if (nnIndex_L2) nnIndex_L2->save(filename);}int veclen() const{if (nnIndex_L1) return nnIndex_L1->veclen();if (nnIndex_L2) return nnIndex_L2->veclen();}int size() const{if (nnIndex_L1) return nnIndex_L1->size();if (nnIndex_L2) return nnIndex_L2->size();}::cvflann::IndexParams getParameters(){if (nnIndex_L1) return nnIndex_L1->getParameters();if (nnIndex_L2) return nnIndex_L2->getParameters();}FLANN_DEPRECATED const ::cvflann::IndexParams* getIndexParameters(){if (nnIndex_L1) return nnIndex_L1->getIndexParameters();if (nnIndex_L2) return nnIndex_L2->getIndexParameters();}private:// providing backwards compatibility for L2 and L1 distances (most common)::cvflann::Index< L2<ElementType> >* nnIndex_L2;::cvflann::Index< L1<ElementType> >* nnIndex_L1;};构造函数flann::Index_::Index_flann::Index_<T>::Index_(const Mat& features, const IndexParams& params)/*Parameters: features – Matrix of containing the features(points) to index. The size of the matrix is num_features x feature_dimensionality and the data type of the elements in the matrix must coincide with the type of the index.params – Structure containing the index parameters. The type of index that will be constructed depends on the type of this parameter. See the description.*/

参数features,是包含用于构建索引的特征的矩阵;参数params,是包含索引参数的结构。 该构造函数所实例的快速搜索结构是根据参数params所指定的特定算法来构建的。params是由IndexParams的派生类的引用。 其中: * LinearIndexParams,该结构对应的索引进行线性的、暴力(brute-force)的搜索。

KDTreeIndexParams,该方法对应的索引结构由一组随机kd树构成(randomized kd-trees),它可以平行地进行搜索。struct KDTreeIndexParams : public IndexParams{KDTreeIndexParams( int trees = 4 );};//trees:The number of parallel kd-trees to use. Good values are in the rangeKMeansIndexParams,该方法对应的索引结构是一个层次k均值树(a hierarchical k-means tree)。struct KMeansIndexParams : public IndexParams{KMeansIndexParams(int branching = 32,int iterations = 11,flann_centers_init_t centers_init = CENTERS_RANDOM,float cb_index = 0.2 );};CompositeIndexParams,该结构结合随机kd树和层次k均值树来构建索引。struct CompositeIndexParams : public IndexParams{CompositeIndexParams(int trees = 4,int branching = 32,int iterations = 11,flann_centers_init_t centers_init = CENTERS_RANDOM,float cb_index = 0.2 );};LshIndexParams,该结构使用multi-probe LSH方法创建索引(Multi-Probe LSH: Efficient Indexing for High-Dimensional Similarity Search by Qin Lv, William Josephson, Zhe Wang, Moses Charikar, Kai Li., Proceedings of the 33rd International Conference on Very Large Data Bases (VLDB). Vienna, Austria. September 2007)。struct LshIndexParams : public IndexParams{LshIndexParams(unsigned int table_number,unsigned int key_size,unsigned int multi_probe_level );};AutotunedIndexParams,该结构是根据数据自动选取最优的索引类型来提供最好的性能。struct AutotunedIndexParams : public IndexParams{AutotunedIndexParams(float target_precision = 0.9,float build_weight = 0.01,float memory_weight = 0,float sample_fraction = 0.1 );};SavedIndexParams,该结构用于加载存放在硬盘的索引结构。struct SavedIndexParams : public IndexParams{SavedIndexParams( std::string filename );};//filename:The filename in which the index was saved.flann::Index_::knnSearch人要想成为生活的主人,不仅要适应生活,而且还要发挥主动性,

【计算机视觉】OpenCV的最近邻开源库FLANN

相关文章:

你感兴趣的文章:

标签云: