1
/***********************************************************************
2
* Software License Agreement (BSD License)
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.
9
* Redistribution and use in source and binary forms, with or without
10
* modification, are permitted provided that the following conditions
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.
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
*************************************************************************/
31
#ifndef OPENCV_FLANN_BASE_HPP_
32
#define OPENCV_FLANN_BASE_HPP_
43
#include "all_indices.h"
49
* Sets the log level used for all flann functions
50
* @param level Verbosity level
52
inline void log_verbosity(int level)
55
Logger::setLevel(level);
60
* (Deprecated) Index parameters for creating a saved index.
62
struct SavedIndexParams : public IndexParams
64
SavedIndexParams(cv::String filename)
66
(* this)["algorithm"] = FLANN_INDEX_SAVED;
67
(*this)["filename"] = filename;
72
template<typename Distance>
73
NNIndex<Distance>* load_saved_index(const Matrix<typename Distance::ElementType>& dataset, const cv::String& filename, Distance distance)
75
typedef typename Distance::ElementType ElementType;
77
FILE* fin = fopen(filename.c_str(), "rb");
81
IndexHeader header = load_header(fin);
82
if (header.data_type != Datatype<ElementType>::type()) {
83
throw FLANNException("Datatype of saved index is different than of the one to be created.");
85
if ((size_t(header.rows) != dataset.rows)||(size_t(header.cols) != dataset.cols)) {
86
throw FLANNException("The index saved belongs to a different dataset");
90
params["algorithm"] = header.index_type;
91
NNIndex<Distance>* nnIndex = create_index_by_type<Distance>(dataset, params, distance);
92
nnIndex->loadIndex(fin);
99
template<typename Distance>
100
class Index : public NNIndex<Distance>
103
typedef typename Distance::ElementType ElementType;
104
typedef typename Distance::ResultType DistanceType;
106
Index(const Matrix<ElementType>& features, const IndexParams& params, Distance distance = Distance() )
107
: index_params_(params)
109
flann_algorithm_t index_type = get_param<flann_algorithm_t>(params,"algorithm");
112
if (index_type == FLANN_INDEX_SAVED) {
113
nnIndex_ = load_saved_index<Distance>(features, get_param<cv::String>(params,"filename"), distance);
117
nnIndex_ = create_index_by_type<Distance>(features, params, distance);
132
nnIndex_->buildIndex();
136
void save(cv::String filename)
138
FILE* fout = fopen(filename.c_str(), "wb");
140
throw FLANNException("Cannot open file");
142
save_header(fout, *nnIndex_);
148
* \brief Saves the index to a stream
149
* \param stream The stream to save the index to
151
virtual void saveIndex(FILE* stream)
153
nnIndex_->saveIndex(stream);
157
* \brief Loads the index from a stream
158
* \param stream The stream from which the index is loaded
160
virtual void loadIndex(FILE* stream)
162
nnIndex_->loadIndex(stream);
166
* \returns number of features in this index.
168
size_t veclen() const
170
return nnIndex_->veclen();
174
* \returns The dimensionality of the features in this index.
178
return nnIndex_->size();
182
* \returns The index type (kdtree, kmeans,...)
184
flann_algorithm_t getType() const
186
return nnIndex_->getType();
190
* \returns The amount of memory (in bytes) used by the index.
192
virtual int usedMemory() const
194
return nnIndex_->usedMemory();
199
* \returns The index parameters
201
IndexParams getParameters() const
203
return nnIndex_->getParameters();
207
* \brief Perform k-nearest neighbor search
208
* \param[in] queries The query points for which to find the nearest neighbors
209
* \param[out] indices The indices of the nearest neighbors found
210
* \param[out] dists Distances to the nearest neighbors found
211
* \param[in] knn Number of nearest neighbors to return
212
* \param[in] params Search parameters
214
void knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, int knn, const SearchParams& params)
216
nnIndex_->knnSearch(queries, indices, dists, knn, params);
220
* \brief Perform radius search
221
* \param[in] query The query point
222
* \param[out] indices The indinces of the neighbors found within the given radius
223
* \param[out] dists The distances to the nearest neighbors found
224
* \param[in] radius The radius used for search
225
* \param[in] params Search parameters
226
* \returns Number of neighbors found
228
int radiusSearch(const Matrix<ElementType>& query, Matrix<int>& indices, Matrix<DistanceType>& dists, float radius, const SearchParams& params)
230
return nnIndex_->radiusSearch(query, indices, dists, radius, params);
234
* \brief Method that searches for nearest-neighbours
236
void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams)
238
nnIndex_->findNeighbors(result, vec, searchParams);
242
* \brief Returns actual index
244
FLANN_DEPRECATED NNIndex<Distance>* getIndex()
250
* \brief Returns index parameters.
251
* \deprecated use getParameters() instead.
253
FLANN_DEPRECATED const IndexParams* getIndexParameters()
255
return &index_params_;
259
/** Pointer to actual index class */
260
NNIndex<Distance>* nnIndex_;
261
/** Indices if the index was loaded from a file */
263
/** Parameters passed to the index */
264
IndexParams index_params_;
268
* Performs a hierarchical clustering of the points passed as argument and then takes a cut in the
269
* the clustering tree to return a flat clustering.
270
* @param[in] points Points to be clustered
271
* @param centers The computed cluster centres. Matrix should be preallocated and centers.rows is the
272
* number of clusters requested.
273
* @param params Clustering parameters (The same as for cvflann::KMeansIndex)
274
* @param d Distance to be used for clustering (eg: cvflann::L2)
275
* @return number of clusters computed (can be different than clusters.rows and is the highest number
276
* of the form (branching-1)*K+1 smaller than clusters.rows).
278
template <typename Distance>
279
int hierarchicalClustering(const Matrix<typename Distance::ElementType>& points, Matrix<typename Distance::ResultType>& centers,
280
const KMeansIndexParams& params, Distance d = Distance())
282
KMeansIndex<Distance> kmeans(points, params, d);
285
int clusterNum = kmeans.getClusterCenters(centers);
290
#endif /* OPENCV_FLANN_BASE_HPP_ */