Renesas / opencv-lib

Dependents:   RZ_A2M_Mbed_samples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ground_truth.h Source File

ground_truth.h

00001 /***********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright 2008-2009  Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
00005  * Copyright 2008-2009  David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
00006  *
00007  * THE BSD LICENSE
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  *
00013  * 1. Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00020  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00021  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00022  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00023  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00024  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00028  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *************************************************************************/
00030 
00031 #ifndef OPENCV_FLANN_GROUND_TRUTH_H_
00032 #define OPENCV_FLANN_GROUND_TRUTH_H_
00033 
00034 #include "dist.h"
00035 #include "matrix.h"
00036 
00037 
00038 namespace cvflann
00039 {
00040 
00041 template <typename Distance>
00042 void find_nearest(const Matrix<typename Distance::ElementType>& dataset, typename Distance::ElementType* query, int* matches, int nn,
00043                   int skip = 0, Distance distance = Distance())
00044 {
00045     typedef typename Distance::ResultType DistanceType;
00046     int n = nn + skip;
00047 
00048     std::vector<int> match(n);
00049     std::vector<DistanceType> dists(n);
00050 
00051     dists[0] = distance(dataset[0], query, dataset.cols);
00052     match[0] = 0;
00053     int dcnt = 1;
00054 
00055     for (size_t i=1; i<dataset.rows; ++i) {
00056         DistanceType tmp = distance(dataset[i], query, dataset.cols);
00057 
00058         if (dcnt<n) {
00059             match[dcnt] = (int)i;
00060             dists[dcnt++] = tmp;
00061         }
00062         else if (tmp < dists[dcnt-1]) {
00063             dists[dcnt-1] = tmp;
00064             match[dcnt-1] = (int)i;
00065         }
00066 
00067         int j = dcnt-1;
00068         // bubble up
00069         while (j>=1 && dists[j]<dists[j-1]) {
00070             std::swap(dists[j],dists[j-1]);
00071             std::swap(match[j],match[j-1]);
00072             j--;
00073         }
00074     }
00075 
00076     for (int i=0; i<nn; ++i) {
00077         matches[i] = match[i+skip];
00078     }
00079 }
00080 
00081 
00082 template <typename Distance>
00083 void compute_ground_truth(const Matrix<typename Distance::ElementType>& dataset, const Matrix<typename Distance::ElementType>& testset, Matrix<int>& matches,
00084                           int skip=0, Distance d = Distance())
00085 {
00086     for (size_t i=0; i<testset.rows; ++i) {
00087         find_nearest<Distance>(dataset, testset[i], matches[i], (int)matches.cols, skip, d);
00088     }
00089 }
00090 
00091 
00092 }
00093 
00094 #endif //OPENCV_FLANN_GROUND_TRUTH_H_