OpenCV 4.5.3(日本語機械翻訳)
composite_index.h
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_COMPOSITE_INDEX_H_
32 #define OPENCV_FLANN_COMPOSITE_INDEX_H_
33
35
36 #include "nn_index.h"
37 #include "kdtree_index.h"
38 #include "kmeans_index.h"
39
40 namespace cvflann
41{
42
46 struct CompositeIndexParams : public IndexParams
47{
48 CompositeIndexParams(int trees = 4, int branching = 32, int iterations = 11,
49 flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM, float cb_index = 0.2 )
50 {
51 (*this)["algorithm"] = FLANN_INDEX_KMEANS;
52 // number of randomized trees to use (for kdtree)
53 (*this)["trees"] = trees;
54 // branching factor
55 (*this)["branching"] = branching;
56 // max iterations to perform in one kmeans clustering (kmeans tree)
57 (*this)["iterations"] = iterations;
58 // algorithm used for picking the initial cluster centers for kmeans tree
59 (*this)["centers_init"] = centers_init;
60 // cluster boundary index. Used when searching the kmeans tree
61 (*this)["cb_index"] = cb_index;
62 }
63};
64
65
71 template <typename Distance>
72 class CompositeIndex : public NNIndex<Distance>
73{
74 public:
75 typedef typename Distance::ElementType ElementType;
76 typedef typename Distance::ResultType DistanceType;
77
85 CompositeIndex(const Matrix<ElementType>& inputData, const IndexParams& params = CompositeIndexParams(),
86 Distance d = Distance()) : index_params_(params)
87 {
88 kdtree_index_ = new KDTreeIndex<Distance>(inputData, params, d);
89 kmeans_index_ = new KMeansIndex<Distance>(inputData, params, d);
90
91 }
92
93 CompositeIndex(const CompositeIndex&);
94 CompositeIndex& operator=(const CompositeIndex&);
95
96 virtual ~CompositeIndex()
97 {
98 delete kdtree_index_;
99 delete kmeans_index_;
100 }
101
105 flann_algorithm_t getType() const CV_OVERRIDE
106 {
107 return FLANN_INDEX_COMPOSITE;
108 }
109
113 size_t size() const CV_OVERRIDE
114 {
115 return kdtree_index_->size();
116 }
117
121 size_t veclen() const CV_OVERRIDE
122 {
123 return kdtree_index_->veclen();
124 }
125
129 int usedMemory() const CV_OVERRIDE
130 {
131 return kmeans_index_->usedMemory() + kdtree_index_->usedMemory();
132 }
133
137 void buildIndex() CV_OVERRIDE
138 {
139 Logger::info("Building kmeans tree...\n");
140 kmeans_index_->buildIndex();
141 Logger::info("Building kdtree tree...\n");
142 kdtree_index_->buildIndex();
143 }
144
149 void saveIndex(FILE* stream) CV_OVERRIDE
150 {
151 kmeans_index_->saveIndex(stream);
152 kdtree_index_->saveIndex(stream);
153 }
154
159 void loadIndex(FILE* stream) CV_OVERRIDE
160 {
161 kmeans_index_->loadIndex(stream);
162 kdtree_index_->loadIndex(stream);
163 }
164
168 IndexParams getParameters() const CV_OVERRIDE
169 {
170 return index_params_;
171 }
172
176 void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) CV_OVERRIDE
177 {
178 kmeans_index_->findNeighbors(result, vec, searchParams);
179 kdtree_index_->findNeighbors(result, vec, searchParams);
180 }
181
182 private:
184 KMeansIndex<Distance>* kmeans_index_;
185
187 KDTreeIndex<Distance>* kdtree_index_;
188
190 const IndexParams index_params_;
191};
192
193}
194
196
197 #endif //OPENCV_FLANN_COMPOSITE_INDEX_H_