31 #ifndef OPENCV_FLANN_INDEX_TESTING_H_
32 #define OPENCV_FLANN_INDEX_TESTING_H_
41 #include "result_set.h"
49 inline int countCorrectMatches(
int* neighbors,
int* groundTruth,
int n)
52 for (
int i=0; i<n; ++i) {
53 for (
int k=0;
k<n; ++
k) {
54 if (neighbors[i]==groundTruth[
k]) {
64 template <
typename Distance>
65 typename Distance::ResultType computeDistanceRaport(
const Matrix<typename Distance::ElementType>& inputData,
typename Distance::ElementType* target,
66 int* neighbors,
int* groundTruth,
int veclen,
int n,
const Distance& distance)
68 typedef typename Distance::ResultType DistanceType;
71 for (
int i=0; i<n; ++i) {
72 DistanceType den =
distance(inputData[groundTruth[i]], target, veclen);
73 DistanceType num =
distance(inputData[neighbors[i]], target, veclen);
75 if ((den==0)&&(num==0)) {
86 template <
typename Distance>
87 float search_with_ground_truth(NNIndex<Distance>&
index,
const Matrix<typename Distance::ElementType>& inputData,
88 const Matrix<typename Distance::ElementType>& testData,
const Matrix<int>& matches,
int nn,
int checks,
89 float& time,
typename Distance::ResultType& dist,
const Distance& distance,
int skipMatches)
91 typedef typename Distance::ResultType DistanceType;
93 if (matches.cols<
size_t(nn)) {
94 Logger::info(
"matches.cols=%d, nn=%d\n",matches.cols,nn);
96 FLANN_THROW(
cv::Error::StsError,
"Ground truth is not computed for as many neighbors as requested");
99 KNNResultSet<DistanceType> resultSet(nn+skipMatches);
100 SearchParams searchParams(checks);
104 int* neighbors = &indices[skipMatches];
107 DistanceType distR = 0;
110 while (t.value<0.2) {
115 for (
size_t i = 0; i < testData.rows; i++) {
116 resultSet.init(&indices[0], &dists[0]);
117 index.findNeighbors(resultSet, testData[i], searchParams);
119 correct += countCorrectMatches(neighbors,matches[i], nn);
120 distR += computeDistanceRaport<Distance>(inputData, testData[i], neighbors, matches[i], (
int)testData.cols, nn, distance);
124 time = float(t.value/repeats);
126 float precicion = (float)correct/(nn*testData.rows);
128 dist = distR/(testData.rows*nn);
130 Logger::info(
"%8d %10.4g %10.5g %10.5g %10.5g\n",
131 checks, precicion, time, 1000.0 * time / testData.rows, dist);
137 template <
typename Distance>
138 float test_index_checks(NNIndex<Distance>&
index,
const Matrix<typename Distance::ElementType>& inputData,
139 const Matrix<typename Distance::ElementType>& testData,
const Matrix<int>& matches,
140 int checks,
float& precision,
const Distance& distance,
int nn = 1,
int skipMatches = 0)
142 typedef typename Distance::ResultType DistanceType;
144 Logger::info(
" Nodes Precision(%) Time(s) Time/vec(ms) Mean dist\n");
145 Logger::info(
"---------------------------------------------------------\n");
148 DistanceType dist = 0;
149 precision = search_with_ground_truth(
index, inputData, testData, matches, nn, checks, time, dist, distance, skipMatches);
154 template <
typename Distance>
155 float test_index_precision(NNIndex<Distance>&
index,
const Matrix<typename Distance::ElementType>& inputData,
156 const Matrix<typename Distance::ElementType>& testData,
const Matrix<int>& matches,
157 float precision,
int& checks,
const Distance& distance,
int nn = 1,
int skipMatches = 0)
159 typedef typename Distance::ResultType DistanceType;
160 const float SEARCH_EPS = 0.001f;
162 Logger::info(
" Nodes Precision(%) Time(s) Time/vec(ms) Mean dist\n");
163 Logger::info(
"---------------------------------------------------------\n");
172 p2 = search_with_ground_truth(
index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
175 Logger::info(
"Got as close as I can\n");
180 while (p2<precision) {
184 p2 = search_with_ground_truth(
index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
189 if (
fabs(p2-precision)>SEARCH_EPS) {
190 Logger::info(
"Start linear estimation\n");
195 realPrecision = search_with_ground_truth(
index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
196 while (
fabs(realPrecision-precision)>SEARCH_EPS) {
198 if (realPrecision<precision) {
206 Logger::info(
"Got as close as I can\n");
209 realPrecision = search_with_ground_truth(
index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
217 Logger::info(
"No need for linear estimation\n");
227 template <
typename Distance>
228 void test_index_precisions(NNIndex<Distance>&
index,
const Matrix<typename Distance::ElementType>& inputData,
229 const Matrix<typename Distance::ElementType>& testData,
const Matrix<int>& matches,
230 float* precisions,
int precisions_length,
const Distance& distance,
int nn = 1,
int skipMatches = 0,
float maxTime = 0)
232 typedef typename Distance::ResultType DistanceType;
234 const float SEARCH_EPS = 0.001;
237 std::sort(precisions, precisions+precisions_length);
240 float precision = precisions[pindex];
242 Logger::info(
" Nodes Precision(%) Time(s) Time/vec(ms) Mean dist\n");
243 Logger::info(
"---------------------------------------------------------\n");
253 p2 = search_with_ground_truth(
index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
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) {
273 p2 = search_with_ground_truth(
index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
274 if ((maxTime> 0)&&(time > maxTime)&&(p2<precision))
return;
279 if (
fabs(p2-precision)>SEARCH_EPS) {
280 Logger::info(
"Start linear estimation\n");
285 realPrecision = search_with_ground_truth(
index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
286 while (
fabs(realPrecision-precision)>SEARCH_EPS) {
288 if (realPrecision<precision) {
296 Logger::info(
"Got as close as I can\n");
299 realPrecision = search_with_ground_truth(
index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
307 Logger::info(
"No need for linear estimation\n");
int index
Definition: core_c.h:634
int count
Definition: core_c.h:1413
CV_EXPORTS OutputArray int double double InputArray OutputArray int int bool double k
Definition: imgproc.hpp:2133
@ StsError
unknown /unspecified error
Definition: base.hpp:71