EstervQrCode 2.0.0
Library for qr code manipulation
Loading...
Searching...
No Matches
nn_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_NNINDEX_H
32#define OPENCV_FLANN_NNINDEX_H
33
34#include "matrix.h"
35#include "result_set.h"
36#include "params.h"
37
39
40namespace cvflann
41{
42
46template <typename Distance>
47class NNIndex
48{
49 typedef typename Distance::ElementType ElementType;
50 typedef typename Distance::ResultType DistanceType;
51
52public:
53
54 virtual ~NNIndex() {}
55
59 virtual void buildIndex() = 0;
60
69 virtual void knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, int knn, const SearchParams& params)
70 {
71 CV_Assert(queries.cols == veclen());
72 CV_Assert(indices.rows >= queries.rows);
73 CV_Assert(dists.rows >= queries.rows);
74 CV_Assert(int(indices.cols) >= knn);
75 CV_Assert(int(dists.cols) >= knn);
76
77#if 0
78 KNNResultSet<DistanceType> resultSet(knn);
79 for (size_t i = 0; i < queries.rows; i++) {
80 resultSet.init(indices[i], dists[i]);
81 findNeighbors(resultSet, queries[i], params);
82 }
83#else
84 KNNUniqueResultSet<DistanceType> resultSet(knn);
85 for (size_t i = 0; i < queries.rows; i++) {
86 resultSet.clear();
87 findNeighbors(resultSet, queries[i], params);
88 if (get_param(params,"sorted",true)) resultSet.sortAndCopy(indices[i], dists[i], knn);
89 else resultSet.copy(indices[i], dists[i], knn);
90 }
91#endif
92 }
93
103 virtual int radiusSearch(const Matrix<ElementType>& query, Matrix<int>& indices, Matrix<DistanceType>& dists, float radius, const SearchParams& params)
104 {
105 if (query.rows != 1) {
106 fprintf(stderr, "I can only search one feature at a time for range search\n");
107 return -1;
108 }
109 CV_Assert(query.cols == veclen());
110 CV_Assert(indices.cols == dists.cols);
111
112 int n = 0;
113 int* indices_ptr = NULL;
114 DistanceType* dists_ptr = NULL;
115 if (indices.cols > 0) {
116 n = (int)indices.cols;
117 indices_ptr = indices[0];
118 dists_ptr = dists[0];
119 }
120
121 RadiusUniqueResultSet<DistanceType> resultSet((DistanceType)radius);
122 resultSet.clear();
123 findNeighbors(resultSet, query[0], params);
124 if (n>0) {
125 if (get_param(params,"sorted",true)) resultSet.sortAndCopy(indices_ptr, dists_ptr, n);
126 else resultSet.copy(indices_ptr, dists_ptr, n);
127 }
128
129 return (int)resultSet.size();
130 }
131
136 virtual void saveIndex(FILE* stream) = 0;
137
142 virtual void loadIndex(FILE* stream) = 0;
143
147 virtual size_t size() const = 0;
148
152 virtual size_t veclen() const = 0;
153
157 virtual int usedMemory() const = 0;
158
162 virtual flann_algorithm_t getType() const = 0;
163
167 virtual IndexParams getParameters() const = 0;
168
169
173 virtual void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) = 0;
174};
175
176}
177
179
180#endif //OPENCV_FLANN_NNINDEX_H
T fprintf(T... args)
CvSize size
Definition core_c.h:112
const CvArr const CvArr CvArr * result
Definition core_c.h:1423
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails.
Definition base.hpp:342
CvPoint2D32f float * radius
Definition imgproc_c.h:534
Definition flann.hpp:60