00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef _OPENCV_GROUND_TRUTH_H_
00032 #define _OPENCV_GROUND_TRUTH_H_
00033
00034 #include "opencv2/flann/dist.h"
00035 #include "opencv2/flann/matrix.h"
00036
00037 namespace cvflann
00038 {
00039
00040 template <typename T>
00041 void find_nearest(const Matrix<T>& dataset, T* query, int* matches, int nn, int skip = 0)
00042 {
00043 int n = nn + skip;
00044
00045 T* query_end = query + dataset.cols;
00046
00047 long* match = new long[n];
00048 T* dists = new T[n];
00049
00050 dists[0] = (float)flann_dist(query, query_end, dataset[0]);
00051 match[0] = 0;
00052 int dcnt = 1;
00053
00054 for (size_t i=1;i<dataset.rows;++i) {
00055 T tmp = (T)flann_dist(query, query_end, dataset[i]);
00056
00057 if (dcnt<n) {
00058 match[dcnt] = (long)i;
00059 dists[dcnt++] = tmp;
00060 }
00061 else if (tmp < dists[dcnt-1]) {
00062 dists[dcnt-1] = tmp;
00063 match[dcnt-1] = (long)i;
00064 }
00065
00066 int j = dcnt-1;
00067
00068 while (j>=1 && dists[j]<dists[j-1]) {
00069 swap(dists[j],dists[j-1]);
00070 swap(match[j],match[j-1]);
00071 j--;
00072 }
00073 }
00074
00075 for (int i=0;i<nn;++i) {
00076 matches[i] = match[i+skip];
00077 }
00078
00079 delete[] match;
00080 delete[] dists;
00081 }
00082
00083
00084 template <typename T>
00085 void compute_ground_truth(const Matrix<T>& dataset, const Matrix<T>& testset, Matrix<int>& matches, int skip=0)
00086 {
00087 for (size_t i=0;i<testset.rows;++i) {
00088 find_nearest(dataset, testset[i], matches[i], (int)matches.cols, skip);
00089 }
00090 }
00091
00092
00093 }
00094
00095 #endif //_OPENCV_GROUND_TRUTH_H_