github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/opencv4/include/opencv2/flann/flann_base.hpp (about) 1 /*********************************************************************** 2 * Software License Agreement (BSD License) 3 * 4 * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved. 5 * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved. 6 * 7 * THE BSD LICENSE 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 *************************************************************************/ 30 31 #ifndef OPENCV_FLANN_BASE_HPP_ 32 #define OPENCV_FLANN_BASE_HPP_ 33 34 //! @cond IGNORED 35 36 #include <vector> 37 #include <cstdio> 38 39 #include "general.h" 40 #include "matrix.h" 41 #include "params.h" 42 #include "saving.h" 43 44 #include "all_indices.h" 45 46 namespace cvflann 47 { 48 49 /** 50 * Sets the log level used for all flann functions 51 * @param level Verbosity level 52 */ 53 inline void log_verbosity(int level) 54 { 55 if (level >= 0) { 56 Logger::setLevel(level); 57 } 58 } 59 60 /** 61 * (Deprecated) Index parameters for creating a saved index. 62 */ 63 struct SavedIndexParams : public IndexParams 64 { 65 SavedIndexParams(cv::String filename) 66 { 67 (* this)["algorithm"] = FLANN_INDEX_SAVED; 68 (*this)["filename"] = filename; 69 } 70 }; 71 72 73 template<typename Distance> 74 NNIndex<Distance>* load_saved_index(const Matrix<typename Distance::ElementType>& dataset, const cv::String& filename, Distance distance) 75 { 76 typedef typename Distance::ElementType ElementType; 77 78 FILE* fin = fopen(filename.c_str(), "rb"); 79 if (fin == NULL) { 80 return NULL; 81 } 82 IndexHeader header = load_header(fin); 83 if (header.data_type != Datatype<ElementType>::type()) { 84 fclose(fin); 85 FLANN_THROW(cv::Error::StsError, "Datatype of saved index is different than of the one to be created."); 86 } 87 if ((size_t(header.rows) != dataset.rows)||(size_t(header.cols) != dataset.cols)) { 88 fclose(fin); 89 FLANN_THROW(cv::Error::StsError, "The index saved belongs to a different dataset"); 90 } 91 92 IndexParams params; 93 params["algorithm"] = header.index_type; 94 NNIndex<Distance>* nnIndex = create_index_by_type<Distance>(dataset, params, distance); 95 nnIndex->loadIndex(fin); 96 fclose(fin); 97 98 return nnIndex; 99 } 100 101 102 template<typename Distance> 103 class Index : public NNIndex<Distance> 104 { 105 public: 106 typedef typename Distance::ElementType ElementType; 107 typedef typename Distance::ResultType DistanceType; 108 109 Index(const Matrix<ElementType>& features, const IndexParams& params, Distance distance = Distance() ) 110 : index_params_(params) 111 { 112 flann_algorithm_t index_type = get_param<flann_algorithm_t>(params,"algorithm"); 113 loaded_ = false; 114 115 if (index_type == FLANN_INDEX_SAVED) { 116 nnIndex_ = load_saved_index<Distance>(features, get_param<cv::String>(params,"filename"), distance); 117 loaded_ = true; 118 } 119 else { 120 nnIndex_ = create_index_by_type<Distance>(features, params, distance); 121 } 122 } 123 124 ~Index() 125 { 126 delete nnIndex_; 127 } 128 129 /** 130 * Builds the index. 131 */ 132 void buildIndex() CV_OVERRIDE 133 { 134 if (!loaded_) { 135 nnIndex_->buildIndex(); 136 } 137 } 138 139 void save(cv::String filename) 140 { 141 FILE* fout = fopen(filename.c_str(), "wb"); 142 if (fout == NULL) { 143 FLANN_THROW(cv::Error::StsError, "Cannot open file"); 144 } 145 save_header(fout, *nnIndex_); 146 saveIndex(fout); 147 fclose(fout); 148 } 149 150 /** 151 * \brief Saves the index to a stream 152 * \param stream The stream to save the index to 153 */ 154 virtual void saveIndex(FILE* stream) CV_OVERRIDE 155 { 156 nnIndex_->saveIndex(stream); 157 } 158 159 /** 160 * \brief Loads the index from a stream 161 * \param stream The stream from which the index is loaded 162 */ 163 virtual void loadIndex(FILE* stream) CV_OVERRIDE 164 { 165 nnIndex_->loadIndex(stream); 166 } 167 168 /** 169 * \returns number of features in this index. 170 */ 171 size_t veclen() const CV_OVERRIDE 172 { 173 return nnIndex_->veclen(); 174 } 175 176 /** 177 * \returns The dimensionality of the features in this index. 178 */ 179 size_t size() const CV_OVERRIDE 180 { 181 return nnIndex_->size(); 182 } 183 184 /** 185 * \returns The index type (kdtree, kmeans,...) 186 */ 187 flann_algorithm_t getType() const CV_OVERRIDE 188 { 189 return nnIndex_->getType(); 190 } 191 192 /** 193 * \returns The amount of memory (in bytes) used by the index. 194 */ 195 virtual int usedMemory() const CV_OVERRIDE 196 { 197 return nnIndex_->usedMemory(); 198 } 199 200 201 /** 202 * \returns The index parameters 203 */ 204 IndexParams getParameters() const CV_OVERRIDE 205 { 206 return nnIndex_->getParameters(); 207 } 208 209 /** 210 * \brief Perform k-nearest neighbor search 211 * \param[in] queries The query points for which to find the nearest neighbors 212 * \param[out] indices The indices of the nearest neighbors found 213 * \param[out] dists Distances to the nearest neighbors found 214 * \param[in] knn Number of nearest neighbors to return 215 * \param[in] params Search parameters 216 */ 217 void knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, int knn, const SearchParams& params) CV_OVERRIDE 218 { 219 nnIndex_->knnSearch(queries, indices, dists, knn, params); 220 } 221 222 /** 223 * \brief Perform radius search 224 * \param[in] query The query point 225 * \param[out] indices The indinces of the neighbors found within the given radius 226 * \param[out] dists The distances to the nearest neighbors found 227 * \param[in] radius The radius used for search 228 * \param[in] params Search parameters 229 * \returns Number of neighbors found 230 */ 231 int radiusSearch(const Matrix<ElementType>& query, Matrix<int>& indices, Matrix<DistanceType>& dists, float radius, const SearchParams& params) CV_OVERRIDE 232 { 233 return nnIndex_->radiusSearch(query, indices, dists, radius, params); 234 } 235 236 /** 237 * \brief Method that searches for nearest-neighbours 238 */ 239 void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) CV_OVERRIDE 240 { 241 nnIndex_->findNeighbors(result, vec, searchParams); 242 } 243 244 /** 245 * \brief Returns actual index 246 */ 247 CV_DEPRECATED NNIndex<Distance>* getIndex() 248 { 249 return nnIndex_; 250 } 251 252 /** 253 * \brief Returns index parameters. 254 * \deprecated use getParameters() instead. 255 */ 256 CV_DEPRECATED const IndexParams* getIndexParameters() 257 { 258 return &index_params_; 259 } 260 261 private: 262 /** Pointer to actual index class */ 263 NNIndex<Distance>* nnIndex_; 264 /** Indices if the index was loaded from a file */ 265 bool loaded_; 266 /** Parameters passed to the index */ 267 IndexParams index_params_; 268 269 Index(const Index &); // copy disabled 270 Index& operator=(const Index &); // assign disabled 271 }; 272 273 /** 274 * Performs a hierarchical clustering of the points passed as argument and then takes a cut in the 275 * the clustering tree to return a flat clustering. 276 * @param[in] points Points to be clustered 277 * @param centers The computed cluster centres. Matrix should be preallocated and centers.rows is the 278 * number of clusters requested. 279 * @param params Clustering parameters (The same as for cvflann::KMeansIndex) 280 * @param d Distance to be used for clustering (eg: cvflann::L2) 281 * @return number of clusters computed (can be different than clusters.rows and is the highest number 282 * of the form (branching-1)*K+1 smaller than clusters.rows). 283 */ 284 template <typename Distance> 285 int hierarchicalClustering(const Matrix<typename Distance::ElementType>& points, Matrix<typename Distance::CentersType>& centers, 286 const KMeansIndexParams& params, Distance d = Distance()) 287 { 288 KMeansIndex<Distance> kmeans(points, params, d); 289 kmeans.buildIndex(); 290 291 int clusterNum = kmeans.getClusterCenters(centers); 292 return clusterNum; 293 } 294 295 } 296 297 //! @endcond 298 299 #endif /* OPENCV_FLANN_BASE_HPP_ */