00001 /*M/////////////////////////////////////////////////////////////////////////////////////// 00002 // 00003 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. 00004 // 00005 // By downloading, copying, installing or using the software you agree to this license. 00006 // If you do not agree to this license, do not download, install, 00007 // copy or use the software. 00008 // 00009 // 00010 // License Agreement 00011 // For Open Source Computer Vision Library 00012 // 00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. 00014 // Copyright (C) 2009, Willow Garage Inc., all rights reserved. 00015 // Third party copyrights are property of their respective owners. 00016 // 00017 // Redistribution and use in source and binary forms, with or without modification, 00018 // are permitted provided that the following conditions are met: 00019 // 00020 // * Redistribution's of source code must retain the above copyright notice, 00021 // this list of conditions and the following disclaimer. 00022 // 00023 // * Redistribution's in binary form must reproduce the above copyright notice, 00024 // this list of conditions and the following disclaimer in the documentation 00025 // and/or other materials provided with the distribution. 00026 // 00027 // * The name of the copyright holders may not be used to endorse or promote products 00028 // derived from this software without specific prior written permission. 00029 // 00030 // This software is provided by the copyright holders and contributors "as is" and 00031 // any express or implied warranties, including, but not limited to, the implied 00032 // warranties of merchantability and fitness for a particular purpose are disclaimed. 00033 // In no event shall the Intel Corporation or contributors be liable for any direct, 00034 // indirect, incidental, special, exemplary, or consequential damages 00035 // (including, but not limited to, procurement of substitute goods or services; 00036 // loss of use, data, or profits; or business interruption) however caused 00037 // and on any theory of liability, whether in contract, strict liability, 00038 // or tort (including negligence or otherwise) arising in any way out of 00039 // the use of this software, even if advised of the possibility of such damage. 00040 // 00041 //M*/ 00042 00043 #ifndef __OPENCV_FLANN_H__ 00044 #define __OPENCV_FLANN_H__ 00045 00046 #ifdef __cplusplus 00047 00048 namespace flann 00049 { 00050 class Index; 00051 } 00052 00053 namespace cv { 00054 00055 namespace flann { 00056 00057 /* Nearest neighbor index algorithms */ 00058 enum flann_algorithm_t { 00059 LINEAR = 0, 00060 KDTREE = 1, 00061 KMEANS = 2, 00062 COMPOSITE = 3, 00063 SAVED = 254, 00064 AUTOTUNED = 255 00065 }; 00066 00067 enum flann_centers_init_t { 00068 CENTERS_RANDOM = 0, 00069 CENTERS_GONZALES = 1, 00070 CENTERS_KMEANSPP = 2 00071 }; 00072 00073 00074 enum flann_log_level_t { 00075 LOG_NONE = 0, 00076 LOG_FATAL = 1, 00077 LOG_ERROR = 2, 00078 LOG_WARN = 3, 00079 LOG_INFO = 4 00080 }; 00081 00082 enum flann_distance_t { 00083 EUCLIDEAN = 1, 00084 MANHATTAN = 2, 00085 MINKOWSKI = 3 00086 }; 00087 00088 class CV_EXPORTS IndexFactory 00089 { 00090 public: 00091 virtual ~IndexFactory() {} 00092 virtual ::flann::Index* createIndex(const Mat& dataset) const = 0; 00093 }; 00094 00095 struct CV_EXPORTS IndexParams : public IndexFactory { 00096 protected: 00097 IndexParams() {}; 00098 00099 }; 00100 00101 struct CV_EXPORTS LinearIndexParams : public IndexParams { 00102 LinearIndexParams() {}; 00103 00104 ::flann::Index* createIndex(const Mat& dataset) const; 00105 }; 00106 00107 00108 00109 struct CV_EXPORTS KDTreeIndexParams : public IndexParams { 00110 KDTreeIndexParams(int trees_ = 4) : trees(trees_) {}; 00111 00112 int trees; // number of randomized trees to use (for kdtree) 00113 00114 ::flann::Index* createIndex(const Mat& dataset) const; 00115 }; 00116 00117 struct CV_EXPORTS KMeansIndexParams : public IndexParams { 00118 KMeansIndexParams(int branching_ = 32, int iterations_ = 11, 00119 flann_centers_init_t centers_init_ = CENTERS_RANDOM, float cb_index_ = 0.2 ) : 00120 branching(branching_), 00121 iterations(iterations_), 00122 centers_init(centers_init_), 00123 cb_index(cb_index_) {}; 00124 00125 int branching; // branching factor (for kmeans tree) 00126 int iterations; // max iterations to perform in one kmeans clustering (kmeans tree) 00127 flann_centers_init_t centers_init; // algorithm used for picking the initial cluster centers for kmeans tree 00128 float cb_index; // cluster boundary index. Used when searching the kmeans tree 00129 00130 ::flann::Index* createIndex(const Mat& dataset) const; 00131 }; 00132 00133 00134 struct CV_EXPORTS CompositeIndexParams : public IndexParams { 00135 CompositeIndexParams(int trees_ = 4, int branching_ = 32, int iterations_ = 11, 00136 flann_centers_init_t centers_init_ = CENTERS_RANDOM, float cb_index_ = 0.2 ) : 00137 trees(trees_), 00138 branching(branching_), 00139 iterations(iterations_), 00140 centers_init(centers_init_), 00141 cb_index(cb_index_) {}; 00142 00143 int trees; // number of randomized trees to use (for kdtree) 00144 int branching; // branching factor (for kmeans tree) 00145 int iterations; // max iterations to perform in one kmeans clustering (kmeans tree) 00146 flann_centers_init_t centers_init; // algorithm used for picking the initial cluster centers for kmeans tree 00147 float cb_index; // cluster boundary index. Used when searching the kmeans tree 00148 00149 ::flann::Index* createIndex(const Mat& dataset) const; 00150 }; 00151 00152 00153 struct CV_EXPORTS AutotunedIndexParams : public IndexParams { 00154 AutotunedIndexParams( float target_precision_ = 0.9, float build_weight_ = 0.01, 00155 float memory_weight_ = 0, float sample_fraction_ = 0.1) : 00156 target_precision(target_precision_), 00157 build_weight(build_weight_), 00158 memory_weight(memory_weight_), 00159 sample_fraction(sample_fraction_) {}; 00160 00161 float target_precision; // precision desired (used for autotuning, -1 otherwise) 00162 float build_weight; // build tree time weighting factor 00163 float memory_weight; // index memory weighting factor 00164 float sample_fraction; // what fraction of the dataset to use for autotuning 00165 00166 ::flann::Index* createIndex(const Mat& dataset) const; 00167 }; 00168 00169 00170 struct CV_EXPORTS SavedIndexParams : public IndexParams { 00171 SavedIndexParams() {} 00172 SavedIndexParams(std::string filename_) : filename(filename_) {} 00173 00174 std::string filename; // filename of the stored index 00175 00176 ::flann::Index* createIndex(const Mat& dataset) const; 00177 }; 00178 00179 00180 struct CV_EXPORTS SearchParams { 00181 SearchParams(int checks_ = 32) : 00182 checks(checks_) {}; 00183 00184 int checks; 00185 }; 00186 00187 00188 00189 class CV_EXPORTS Index { 00190 ::flann::Index* nnIndex; 00191 00192 public: 00193 Index(const Mat& features, const IndexParams& params); 00194 00195 ~Index(); 00196 00197 void knnSearch(const vector<float>& queries, vector<int>& indices, vector<float>& dists, int knn, const SearchParams& params); 00198 void knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const SearchParams& params); 00199 00200 int radiusSearch(const vector<float>& query, vector<int>& indices, vector<float>& dists, float radius, const SearchParams& params); 00201 int radiusSearch(const Mat& query, Mat& indices, Mat& dists, float radius, const SearchParams& params); 00202 00203 void save(std::string filename); 00204 00205 int veclen() const; 00206 00207 int size() const; 00208 }; 00209 00210 00211 CV_EXPORTS int hierarchicalClustering(const Mat& features, Mat& centers, 00212 const KMeansIndexParams& params); 00213 00214 } 00215 00216 } 00217 00218 #endif // __cplusplus 00219 00220 #endif