31 #ifndef OPENCV_FLANN_INDEX_TESTING_H_
32 #define OPENCV_FLANN_INDEX_TESTING_H_
51 for (
int i=0; i<
n; ++i) {
52 for (
int k=0;
k<
n; ++
k) {
53 if (neighbors[i]==groundTruth[
k]) {
63 template <
typename Distance>
65 int* neighbors,
int* groundTruth,
int veclen,
int n,
const Distance& distance)
67 typedef typename Distance::ResultType DistanceType;
70 for (
int i=0; i<
n; ++i) {
71 DistanceType den = distance(inputData[groundTruth[i]], target, veclen);
72 DistanceType
num = distance(inputData[neighbors[i]], target, veclen);
74 if ((den==0)&&(num==0)) {
85 template <
typename Distance>
88 float& time,
typename Distance::ResultType&
dist,
const Distance& distance,
int skipMatches)
90 typedef typename Distance::ResultType DistanceType;
92 if (matches.
cols<
size_t(nn)) {
93 Logger::info(
"matches.cols=%d, nn=%d\n",matches.
cols,nn);
95 throw FLANNException(
"Ground truth is not computed for as many neighbors as requested");
101 std::vector<int>
indices(nn+skipMatches);
102 std::vector<DistanceType> dists(nn+skipMatches);
103 int* neighbors = &indices[skipMatches];
106 DistanceType distR = 0;
109 while (t.
value<0.2) {
114 for (
size_t i = 0; i < testData.
rows; i++) {
115 resultSet.
init(&indices[0], &dists[0]);
119 distR += computeDistanceRaport<Distance>(inputData, testData[i], neighbors, matches[i], (
int)testData.
cols, nn, distance);
125 float precicion = (
float)correct/(nn*testData.
rows);
127 dist = distR/(testData.
rows*nn);
129 Logger::info(
"%8d %10.4g %10.5g %10.5g %10.5g\n",
130 checks, precicion, time, 1000.0 * time / testData.
rows, dist);
136 template <
typename Distance>
139 int checks,
float& precision,
const Distance& distance,
int nn = 1,
int skipMatches = 0)
141 typedef typename Distance::ResultType DistanceType;
143 Logger::info(
" Nodes Precision(%) Time(s) Time/vec(ms) Mean dist\n");
144 Logger::info(
"---------------------------------------------------------\n");
147 DistanceType
dist = 0;
148 precision =
search_with_ground_truth(index, inputData, testData, matches, nn, checks, time, dist, distance, skipMatches);
153 template <
typename Distance>
156 float precision,
int& checks,
const Distance& distance,
int nn = 1,
int skipMatches = 0)
158 typedef typename Distance::ResultType DistanceType;
159 const float SEARCH_EPS = 0.001f;
161 Logger::info(
" Nodes Precision(%) Time(s) Time/vec(ms) Mean dist\n");
162 Logger::info(
"---------------------------------------------------------\n");
174 Logger::info(
"Got as close as I can\n");
179 while (p2<precision) {
188 if (fabs(p2-precision)>SEARCH_EPS) {
189 Logger::info(
"Start linear estimation\n");
194 realPrecision =
search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
195 while (fabs(realPrecision-precision)>SEARCH_EPS) {
197 if (realPrecision<precision) {
205 Logger::info(
"Got as close as I can\n");
208 realPrecision =
search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
216 Logger::info(
"No need for linear estimation\n");
226 template <
typename Distance>
229 float* precisions,
int precisions_length,
const Distance& distance,
int nn = 1,
int skipMatches = 0,
float maxTime = 0)
231 typedef typename Distance::ResultType DistanceType;
233 const float SEARCH_EPS = 0.001;
236 std::sort(precisions, precisions+precisions_length);
239 float precision = precisions[pindex];
241 Logger::info(
" Nodes Precision(%) Time(s) Time/vec(ms) Mean dist\n");
242 Logger::info(
"---------------------------------------------------------\n");
258 while (precisions[pindex]<p2 && pindex<precisions_length) {
262 if (pindex==precisions_length) {
263 Logger::info(
"Got as close as I can\n");
267 for (
int i=pindex; i<precisions_length; ++i) {
269 precision = precisions[i];
270 while (p2<precision) {
275 if ((maxTime> 0)&&(time > maxTime)&&(p2<precision))
return;
280 if (fabs(p2-precision)>SEARCH_EPS) {
281 Logger::info(
"Start linear estimation\n");
286 realPrecision =
search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
287 while (fabs(realPrecision-precision)>SEARCH_EPS) {
289 if (realPrecision<precision) {
297 Logger::info(
"Got as close as I can\n");
300 realPrecision =
search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
308 Logger::info(
"No need for linear estimation\n");
318 #endif //OPENCV_FLANN_INDEX_TESTING_H_
void start()
Definition: timer.h:67
CvPoint2D32f p2
Definition: legacy.hpp:578
size_t cols
Definition: matrix.h:52
CV_EXPORTS_W void sort(InputArray src, OutputArray dst, int flags)
sorts independently each matrix row or each matrix column
CvSize CvPoint2D32f int count
Definition: calib3d.hpp:221
float search_with_ground_truth(NNIndex< Distance > &index, const Matrix< typename Distance::ElementType > &inputData, const Matrix< typename Distance::ElementType > &testData, const Matrix< int > &matches, int nn, int checks, float &time, typename Distance::ResultType &dist, const Distance &distance, int skipMatches)
Definition: index_testing.h:86
virtual void findNeighbors(ResultSet< DistanceType > &result, const ElementType *vec, const SearchParams &searchParams)=0
Method that searches for nearest-neighbours.
GLuint index
Definition: core_c.h:986
int countCorrectMatches(int *neighbors, int *groundTruth, int n)
Definition: index_testing.h:48
void test_index_precisions(NNIndex< Distance > &index, const Matrix< typename Distance::ElementType > &inputData, const Matrix< typename Distance::ElementType > &testData, const Matrix< int > &matches, float *precisions, int precisions_length, const Distance &distance, int nn=1, int skipMatches=0, float maxTime=0)
Definition: index_testing.h:227
void init(int *indices_, DistanceType *dists_)
Definition: result_set.h:165
Definition: result_set.h:152
const CvMat CvMat CvMat int k
Definition: legacy.hpp:3052
GLuint GLuint GLsizei count
Definition: core_c.h:973
double value
Definition: timer.h:53
const CvMat CvMat * indices
Definition: legacy.hpp:3052
float test_index_checks(NNIndex< Distance > &index, const Matrix< typename Distance::ElementType > &inputData, const Matrix< typename Distance::ElementType > &testData, const Matrix< int > &matches, int checks, float &precision, const Distance &distance, int nn=1, int skipMatches=0)
Definition: index_testing.h:137
float test_index_precision(NNIndex< Distance > &index, const Matrix< typename Distance::ElementType > &inputData, const Matrix< typename Distance::ElementType > &testData, const Matrix< int > &matches, float precision, int &checks, const Distance &distance, int nn=1, int skipMatches=0)
Definition: index_testing.h:154
void stop()
Definition: timer.h:75
Definition: nn_index.h:48
int n
Definition: legacy.hpp:3070
size_t rows
Definition: matrix.h:51
::max::max::max float
Definition: functional.hpp:326
::max::max int
Definition: functional.hpp:324
Distance::ResultType computeDistanceRaport(const Matrix< typename Distance::ElementType > &inputData, typename Distance::ElementType *target, int *neighbors, int *groundTruth, int veclen, int n, const Distance &distance)
Definition: index_testing.h:64
CvPoint3D64f double * dist
Definition: legacy.hpp:556