extract_features.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 extract_features.cc Main file for the extract_features executable.
00022 
00023 \section wpUsage Usage
00024 
00025 <code> extract_features [--VAR VAL] [--exec FILE] IMAGE1 [IMAGE2 ...]</code>
00026 
00027 \section Description
00028 
00029 This program loads a learned FAST-ER tree and extracts features so that an 
00030 accelerated tree can be learned. The output is is suitable for consumption 
00031 by \link learn_fast_tree.cc learn_fast_tree\endlink.
00032 
00033 The program accpets standard GVars3 commandline arguments, and the default
00034 parameters are contained in \p extract_features.cfg :
00035 
00036 \include extract_features.cfg
00037 
00038 The images from which
00039 features should be extracted are specified on the commandline. 
00040 
00041 */
00042 
00043 #include <gvars3/instances.h>
00044 #include <cvd/image_io.h>
00045 #include <stdint.h>
00046 #include <map>
00047 #include <iterator>
00048 #include <string>
00049 #include "offsets.h"
00050 #include "faster_tree.h"
00051 
00052 using namespace std;
00053 using namespace CVD;
00054 using namespace GVars3;
00055 
00056 static const char BrighterFlag = 'b'; ///< Character code for pixels significantly brighter than the centre
00057 static const char DarkerFlag = 'd';///< Character code for pixels significantly darker than the centre
00058 static const char SimilarFlag = 's';///< Character code for pixels similar to the centre
00059 
00060 ///Extracts a feature from an image.
00061 ///@param s String to extract feature in to
00062 ///@param im Image to extract feature from
00063 ///@param pos Location to extract feature from
00064 ///@param barrier Threshold used to compute feature
00065 ///@param o Index in to offsets (i.e.\ feature orientation) to use
00066 ///@param invert_sense Whether or not to invert the extracted feature
00067 void extract_feature(string& s, const BasicImage<byte>& im, const ImageRef&  pos, int barrier, int o, bool invert_sense)
00068 {
00069     int cb = im[pos] + barrier;
00070     int c_b = im[pos] - barrier;
00071 
00072     for(int i=0; i < num_offsets; i++)
00073     {
00074         int pix = im[pos + offsets[o][i]];
00075     
00076         if(pix > cb)
00077             if(invert_sense == false)
00078                 s[i] = BrighterFlag;
00079             else
00080                 s[i] = DarkerFlag;
00081         else if(pix < c_b)
00082             if(invert_sense == false)
00083                 s[i] = DarkerFlag;
00084             else
00085                 s[i] = BrighterFlag;
00086         else
00087             s[i] = SimilarFlag;
00088     }
00089 }
00090 
00091 ///Driving program
00092 ///@param argc Number of commandline arguments
00093 ///@param argv List of commandline arguments. Contains GVars3 arguments, and images to process.
00094 int main(int argc, char** argv)
00095 {
00096     //The usual initialization.
00097     GUI.LoadFile("extract_features.cfg");
00098     int lastarg = GUI.parseArguments(argc, argv);
00099 
00100     create_offsets();
00101     
00102     //Store corners and noncorners by the string representing the feature.
00103     map<string, uint64_t> corners, non_corners;
00104     
00105     //Scratch string of the correct length for extracting features in to.
00106     string scratch(num_offsets, '.');
00107     
00108     //Don't bother examining points outside this border.
00109     int border = max(max(offsets_bbox.first.x, offsets_bbox.first.y), max(offsets_bbox.second.x, offsets_bbox.second.y));
00110 
00111     int threshold = GV3::get<int>("threshold", 30);
00112     string fname=GV3::get<string>("detector", "best_faster.tree");
00113     
00114     //Load a detector from a tree file
00115     tree_element* faster_detector;
00116 
00117     ifstream i;
00118     i.open(fname.c_str());
00119 
00120     if(!i.good())
00121     {
00122         cerr << "Error: " << fname << ": " << strerror(errno) << endl;
00123         exit(1);
00124     }
00125     
00126     try{
00127         faster_detector = load_a_tree(i);
00128     }
00129     catch(ParseError p)
00130     {
00131         cerr << "Parse error in " << fname << endl;
00132         exit(1);
00133     }
00134 
00135     //Iterate over all images, extracting features
00136     for(int i=lastarg; i < argc; i++)
00137     {
00138         try{
00139             Image<byte> im = img_load(argv[i]);
00140             for(int r=border; r < im.size().y - border; r++)
00141                 for(int c=border; c < im.size().x - border; c++)
00142                 {
00143                     ImageRef pos(c,r);
00144                     //Test for cornerness
00145                     bool is_corner = faster_detector->detect_corner(im, pos, threshold);
00146 
00147                     //Iterate over all feature orientations and inversions,
00148                     //extracting the reatures, and inserting them in to the
00149                     //correct bin
00150                     for(unsigned int k=0; k < offsets.size(); k++)
00151                         for(int l=0; l < 2; l++)
00152                         {
00153                             extract_feature(scratch, im, pos, threshold, k, l);
00154 
00155                             if(is_corner)
00156                             {
00157                                 corners[scratch]++;
00158 
00159                                 if(non_corners.count(scratch))
00160                                 {
00161                                     cerr << "Fatal error! extracted corner has an identical non-corner!\n";
00162                                     cerr << "Are your offsets correct?\n";
00163                                     exit(1);
00164                                 }
00165                             }
00166                             else
00167                             {
00168                                 non_corners[scratch]++;
00169                                 if(corners.count(scratch))
00170                                 {
00171                                     cerr << "Fatal error! extracted non-corner has an identical corner!\n";
00172                                     cerr << "Are your offsets correct?\n";
00173                                     exit(1);
00174                                 }
00175                             }
00176                         }
00177                 }
00178             cerr << "Processed " << argv[i] << endl;
00179         }
00180         catch(Exceptions::All e)
00181         {
00182             cerr << "Failed to load " << argv[i] << ": " << e.what << endl;
00183         }
00184     }
00185 
00186     cout << num_offsets << endl;
00187     copy(offsets[0].begin(), offsets[0].end(), ostream_iterator<ImageRef>(cout, " "));
00188     cout << endl;
00189 
00190     for(map<string, uint64_t>::iterator i=corners.begin(); i != corners.end(); i++)
00191         cout << i->first << " " << i->second << " 1" << endl;
00192     for(map<string, uint64_t>::iterator i=non_corners.begin(); i != non_corners.end(); i++)
00193         cout << i->first << " " << i->second << " 0" << endl;
00194 }

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