OpenCV 4.5.3(日本語機械翻訳)
flann_base.hpp
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
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
53 inline void log_verbosity(int level)
54{
55 if (level >= 0) {
56 Logger::setLevel(level);
57 }
58}
59
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>
74NNIndex<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
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
154 virtual void saveIndex(FILE* stream) CV_OVERRIDE
155 {
156 nnIndex_->saveIndex(stream);
157 }
158
163 virtual void loadIndex(FILE* stream) CV_OVERRIDE
164 {
165 nnIndex_->loadIndex(stream);
166 }
167
171 size_t veclen() const CV_OVERRIDE
172 {
173 return nnIndex_->veclen();
174 }
175
179 size_t size() const CV_OVERRIDE
180 {
181 return nnIndex_->size();
182 }
183
187 flann_algorithm_t getType() const CV_OVERRIDE
188 {
189 return nnIndex_->getType();
190 }
191
195 virtual int usedMemory() const CV_OVERRIDE
196 {
197 return nnIndex_->usedMemory();
198 }
199
200
204 IndexParams getParameters() const CV_OVERRIDE
205 {
206 return nnIndex_->getParameters();
207 }
208
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
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
239 void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) CV_OVERRIDE
240 {
241 nnIndex_->findNeighbors(result, vec, searchParams);
242 }
243
247 CV_DEPRECATED NNIndex<Distance>* getIndex()
248 {
249 return nnIndex_;
250 }
251
256 CV_DEPRECATED const IndexParams* getIndexParameters()
257 {
258 return &index_params_;
259 }
260
261 private:
263 NNIndex<Distance>* nnIndex_;
265 bool loaded_;
267 IndexParams index_params_;
268
269 Index(const Index &); // copy disabled
270 Index& operator=(const Index &); // assign disabled
271};
272
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
298
299 #endif /* OPENCV_FLANN_BASE_HPP_ */
CV_EXPORTS_W double kmeans(InputArray data, int K, InputOutputArray bestLabels, TermCriteria criteria, int attempts, int flags, OutputArray centers=noArray())
Finds centers of clusters and groups input samples around the clusters.
int hierarchicalClustering(const Mat &features, Mat &centers, const ::cvflann::KMeansIndexParams &params, Distance d=Distance())
Clusters features using hierarchical k-means algorithm.
Definition: flann.hpp:584