1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
57 /* Nearest neighbor index algorithms */
58 enum flann_algorithm_t {
67 enum flann_centers_init_t {
74 enum flann_log_level_t {
82 enum flann_distance_t {
88 class CV_EXPORTS IndexFactory
91 virtual ~IndexFactory() {}
92 virtual ::flann::Index* createIndex(const Mat& dataset) const = 0;
95 struct CV_EXPORTS IndexParams : public IndexFactory {
101 struct CV_EXPORTS LinearIndexParams : public IndexParams {
102 LinearIndexParams() {};
104 ::flann::Index* createIndex(const Mat& dataset) const;
109 struct CV_EXPORTS KDTreeIndexParams : public IndexParams {
110 KDTreeIndexParams(int trees_ = 4) : trees(trees_) {};
112 int trees; // number of randomized trees to use (for kdtree)
114 ::flann::Index* createIndex(const Mat& dataset) const;
117 struct CV_EXPORTS KMeansIndexParams : public IndexParams {
118 KMeansIndexParams(int branching_ = 32, int iterations_ = 11,
119 flann_centers_init_t centers_init_ = CENTERS_RANDOM, float cb_index_ = 0.2 ) :
120 branching(branching_),
121 iterations(iterations_),
122 centers_init(centers_init_),
123 cb_index(cb_index_) {};
125 int branching; // branching factor (for kmeans tree)
126 int iterations; // max iterations to perform in one kmeans clustering (kmeans tree)
127 flann_centers_init_t centers_init; // algorithm used for picking the initial cluster centers for kmeans tree
128 float cb_index; // cluster boundary index. Used when searching the kmeans tree
130 ::flann::Index* createIndex(const Mat& dataset) const;
134 struct CV_EXPORTS CompositeIndexParams : public IndexParams {
135 CompositeIndexParams(int trees_ = 4, int branching_ = 32, int iterations_ = 11,
136 flann_centers_init_t centers_init_ = CENTERS_RANDOM, float cb_index_ = 0.2 ) :
138 branching(branching_),
139 iterations(iterations_),
140 centers_init(centers_init_),
141 cb_index(cb_index_) {};
143 int trees; // number of randomized trees to use (for kdtree)
144 int branching; // branching factor (for kmeans tree)
145 int iterations; // max iterations to perform in one kmeans clustering (kmeans tree)
146 flann_centers_init_t centers_init; // algorithm used for picking the initial cluster centers for kmeans tree
147 float cb_index; // cluster boundary index. Used when searching the kmeans tree
149 ::flann::Index* createIndex(const Mat& dataset) const;
153 struct CV_EXPORTS AutotunedIndexParams : public IndexParams {
154 AutotunedIndexParams( float target_precision_ = 0.9, float build_weight_ = 0.01,
155 float memory_weight_ = 0, float sample_fraction_ = 0.1) :
156 target_precision(target_precision_),
157 build_weight(build_weight_),
158 memory_weight(memory_weight_),
159 sample_fraction(sample_fraction_) {};
161 float target_precision; // precision desired (used for autotuning, -1 otherwise)
162 float build_weight; // build tree time weighting factor
163 float memory_weight; // index memory weighting factor
164 float sample_fraction; // what fraction of the dataset to use for autotuning
166 ::flann::Index* createIndex(const Mat& dataset) const;
170 struct CV_EXPORTS SavedIndexParams : public IndexParams {
171 SavedIndexParams() {}
172 SavedIndexParams(std::string filename_) : filename(filename_) {}
174 std::string filename; // filename of the stored index
176 ::flann::Index* createIndex(const Mat& dataset) const;
180 struct CV_EXPORTS SearchParams {
181 SearchParams(int checks_ = 32) :
189 class CV_EXPORTS Index {
190 ::flann::Index* nnIndex;
193 Index(const Mat& features, const IndexParams& params);
197 void knnSearch(const vector<float>& queries, vector<int>& indices, vector<float>& dists, int knn, const SearchParams& params);
198 void knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const SearchParams& params);
200 int radiusSearch(const vector<float>& query, vector<int>& indices, vector<float>& dists, float radius, const SearchParams& params);
201 int radiusSearch(const Mat& query, Mat& indices, Mat& dists, float radius, const SearchParams& params);
203 void save(std::string filename);
211 CV_EXPORTS int hierarchicalClustering(const Mat& features, Mat& centers,
212 const KMeansIndexParams& params);
218 #endif // __cplusplus
220 #endif /* CXFLANN_H_ */