test_repeatability.cc

Go to the documentation of this file.
00001 /*
00002 
00003     This file is part of the FAST-ER machine learning system.
00004     Copyright (C) 2008  Edward Rosten and Los Alamos National Laboratory
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License along
00017     with this program; if not, write to the Free Software Foundation, Inc.,
00018     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00019 */
00020 /**
00021 \file test_repeatability.cc Main file for the test_repeatability executable.
00022 
00023 \section wpUsage Usage
00024 
00025 <code> test_repeatability [--var VAL] [--exec FILE] </code>
00026 
00027 \section Description
00028 
00029 This program \link gDataset loads a dataset\endlink and then 
00030 computes repeatability of the specified detector on the dataset. This
00031 program accepts standard GVars3 commandline arguments and loads 
00032 <code>learn_detector.cfg</code> as a the default configuration:
00033 
00034 \include test_repeatability.cfg
00035 
00036 The available detectors are selected using the <code>detector</code> variable.
00037 Options are given in ::get_detector.
00038 
00039 */
00040 
00041 
00042 #include <iostream>
00043 #include <sstream>
00044 #include <cfloat>
00045 #include <map>
00046 #include <utility>
00047 
00048 #include <cvd/image_io.h>
00049 #include <cvd/random.h>
00050 #include <cvd/image_interpolate.h>
00051 #include <tag/printf.h>
00052 #include <tag/stdpp.h>
00053 
00054 
00055 #include "gvars_vector.h"
00056 #include "load_data.h"
00057 #include "detectors.h"
00058 #include "utility.h"
00059 
00060 using namespace std;
00061 using namespace CVD;
00062 using namespace tag;
00063 using namespace GVars3;
00064 using namespace TooN;
00065 
00066 
00067 ///Computes repeatability the slow way to avoid rounding errors, by comparing the warped
00068 ///corner position to every detected corner. A warp to x=-1, y=? is considered to be outside
00069 ///the image, so it is not counted.
00070 ///
00071 /// @param warps    Every warping where warps[i][j] specifies warp from image i to image j.
00072 /// @param corners  Detected corners
00073 /// @param r        A corner must be as close as this to be considered repeated
00074 /// @return         The repeatability. No corners means zero repeatability.
00075 /// @ingroup gRepeatability
00076 double compute_repeatability_exact(const vector<vector<Image<array<float,2> > > >& warps, const vector<vector<ImageRef> >& corners, double r)
00077 {
00078     unsigned int n = corners.size();
00079 
00080     int repeatable_corners = 0;
00081     int repeated_corners = 0;
00082 
00083     r *= r;
00084 
00085     for(unsigned int i=0; i < n; i++)
00086         for(unsigned int j=0; j < n; j++)
00087         {
00088             if(i==j)
00089                 continue;
00090 
00091             for(unsigned int k=0; k < corners[i].size(); k++)
00092             {
00093                 const array<float, 2>& p = warps[i][j][corners[i][k]];
00094 
00095                 if(p[0] != -1) //pixel does not warp to inside image j
00096                 {
00097 
00098                     repeatable_corners++;
00099 
00100                     for(unsigned int l=0; l < corners[j].size(); l++)
00101                     {
00102                         Vector<2> d = Vec(p) - vec(corners[j][l]);
00103 
00104                         if(d*d < r)
00105                         {
00106                             repeated_corners++;
00107                             break;
00108                         }
00109                     }
00110                 }
00111             }
00112         }
00113     
00114     return 1.0 * (repeated_corners) / (repeatable_corners + DBL_EPSILON);
00115 }
00116 
00117 ///This wrapper function computed the repeatability for a given detector and a given
00118 ///container of corner densities. The result is printed to stdout.
00119 ///
00120 /// @param images   Images to test repeatability on
00121 /// @param warps    Every warping where warps[i][j] specifies warp from image i to image j.
00122 /// @param detector Pointer to the corner detection function.
00123 /// @param cpf      The number of corners per frame to be tested.
00124 /// @param fuzz     A corner must be as close as this to be considered repeated
00125 /// @ingroup gRepeatability
00126 void compute_repeatability_all(const vector<Image<byte> >& images, const vector<vector<Image<array<float, 2> > > >& warps, const DetectN& detector, const vector<int>& cpf, double fuzz)
00127 {
00128     
00129     for(unsigned int i=0; i < cpf.size(); i++)
00130     {
00131         //Detect corners in each if the frames
00132         vector<vector<ImageRef> > corners;
00133         double  num_corners = 0;
00134 
00135         for(unsigned int j=0; j < images.size(); j++)
00136         {
00137             vector<ImageRef> c;
00138             detector(images[j], c, cpf[i]);
00139             corners.push_back(c);
00140 
00141             num_corners += c.size();
00142         }
00143 
00144         //Compute and print the repeatability.
00145         cout << print << num_corners / images.size() << compute_repeatability_exact(warps, corners, fuzz);
00146     }
00147 }
00148 
00149 
00150 ///This wrapper function computed the repeatability for a given detector and a given
00151 ///container of corner densities for variable levels of noise, from 0 to n in steps of 1
00152 ///The result is printed to stdout.
00153 ///
00154 /// @param images   Images to test repeatability on
00155 /// @param warps    Every warping where warps[i][j] specifies warp from image i to image j.
00156 /// @param detector Pointer to the corner detection function.
00157 /// @param cpf      The number of corners per frame to be tested.
00158 /// @param n        The initial noise level
00159 /// @param fuzz     A corner must be as close as this to be considered repeated
00160 /// @ingroup gRepeatability
00161 void compute_repeatability_noise(const vector<Image<byte> >& images, const vector<vector<Image<array<float, 2> > > >& warps, const DetectN& detector, int cpf,  float n, double fuzz)
00162 {
00163         
00164     for(float s=0; s <= n; s++)
00165     {
00166 
00167 
00168         //Detect corners in each if the frames
00169         vector<vector<ImageRef> > corners;
00170         double  num_corners = 0;
00171 
00172         for(unsigned int j=0; j < images.size(); j++)
00173         {
00174             Image<byte> ni = images[j].copy_from_me();
00175 
00176             //Add noise to the image
00177             for(Image<byte>::iterator i=ni.begin(); i != ni.end(); i++)
00178                 *i = max(0, min(255, (int)floor(*i + rand_g() * s + .5)));
00179 
00180             vector<ImageRef> c;
00181             detector(ni, c, cpf);
00182             corners.push_back(c);
00183 
00184             num_corners += c.size();
00185         }
00186 
00187         //Compute and print the repeatability.
00188         cout << print << s << compute_repeatability_exact(warps, corners, fuzz) << num_corners / images.size();
00189     }
00190 }
00191 
00192 
00193 ///This is the driver function. It reads the command line arguments and calls
00194 ///functions to load the data and compute the repeatability.
00195 ///
00196 /// @param argc    Number of command line argumentsd.
00197 /// @param argv    Pointer to command line arguments.
00198 /// @ingroup gRepeatability
00199 void mmain(int argc, char** argv)
00200 {
00201     GUI.LoadFile("test_repeatability.cfg");
00202     GUI.parseArguments(argc, argv);
00203 
00204     vector<Image<byte> > images;
00205     vector<vector<Image<array<float, 2> > > > warps;
00206 
00207 
00208     int n = GV3::get<int>("num", 2, 1);
00209     string dir = GV3::get<string>("dir", "./", 1);
00210     string format = GV3::get<string>("type", "cambridge", 1);
00211     double fuzz = GV3::get<double>("r", 5, 1);
00212     vector<int> cpf = GV3::get<vector<int> >("cpf", "0 10 20 30 40 50 60 70 80 90 100 150 200 250 300 350 400 450 500 550 600 650 700 750 800 850 900 950 1000 1100 1200 1300 1400 1500 1600 1700 1800 1900 2000 2200", 1);
00213     int ncpf = GV3::get<int>("ncpf", 500, 1);
00214     float nmax = GV3::get<int>("nmax", 50, 1);
00215     string test = GV3::get<string>("test", "normal", 1);
00216     
00217     auto_ptr<DetectN> detector = get_detector();
00218 
00219     rpair(images, warps) = load_data(dir, n, format);
00220     
00221     if(test == "noise")
00222         compute_repeatability_noise(images, warps, *detector, ncpf, nmax, fuzz);
00223     else
00224         compute_repeatability_all(images, warps, *detector, cpf, fuzz);
00225 
00226 }
00227 
00228 ///Driving function which catches exceptions
00229 ///@param argc Number of command line arguments
00230 ///@param argv Commandline argument list
00231 int main(int argc, char** argv)
00232 {
00233     try
00234     {
00235         mmain(argc, argv);
00236     }
00237     catch(Exceptions::All e)
00238     {
00239         cerr << "Error: " << e.what << endl;
00240     }   
00241 }

Generated on Mon Mar 2 12:47:12 2009 for FAST-ER by  doxygen 1.5.3