|
| 1 | +#include <iostream> |
| 2 | +#include <iomanip> |
| 3 | +#include <fstream> |
| 4 | +#include <sstream> |
| 5 | +#include <cmath> |
| 6 | +#include <vector> |
| 7 | +#include <algorithm> |
| 8 | +#include <cstring> |
| 9 | + |
| 10 | +#include <boost/program_options.hpp> |
| 11 | + |
| 12 | +#include "csf_feat.h" |
| 13 | +#include "csf_options.h" |
| 14 | + |
| 15 | +using namespace std; |
| 16 | + |
| 17 | +typedef vector<float> desc_t; |
| 18 | + |
| 19 | +/** Read a file containing SIFT descriptors |
| 20 | + */ |
| 21 | +void readFile( vector<feat_t>& vec, const std::string& filename ); |
| 22 | + |
| 23 | +/** Write the average descriptor differences to file |
| 24 | + * @param file The output file |
| 25 | + * @param stats A 128-float vector containing the sum of differences |
| 26 | + * @param sz The number of samples that have been collected in stats |
| 27 | + */ |
| 28 | +void writeSummary( ostream& file, const vector<float>& stats, int sz ); |
| 29 | + |
| 30 | +/** Open a new stream or return the default stream. The default stream is |
| 31 | + * returned if the name is empty or opening the stream fails. |
| 32 | + * @param name A string containing the name of the file to open or empty |
| 33 | + * @param default_stream The default stream to return |
| 34 | + */ |
| 35 | +ostream* openOutputFile( const string& name, ostream* default_stream ); |
| 36 | + |
| 37 | +int main( int argc, char* argv[] ) |
| 38 | +{ |
| 39 | + Parameters param; |
| 40 | + |
| 41 | + parseargs( argc, argv, param ); |
| 42 | + |
| 43 | + vector<feat_t> l_one; |
| 44 | + vector<feat_t> l_two; |
| 45 | + |
| 46 | + readFile( l_one, param.input[0] ); |
| 47 | + readFile( l_two, param.input[1] ); |
| 48 | + |
| 49 | + ostream* outfile = openOutputFile( param.outfile_name, &cout ); |
| 50 | + ostream* descfile = openOutputFile( param.descfile_name, nullptr ); |
| 51 | + |
| 52 | + int len = l_one.size(); |
| 53 | + int ct = 0; |
| 54 | + float nextpercent = 10; |
| 55 | + |
| 56 | + vector<float> desc_stats( 128, 0.0f ); |
| 57 | + |
| 58 | + ostream* print_dists = param.descfile_verbose ? descfile : 0; |
| 59 | + |
| 60 | + for( auto l : l_one ) |
| 61 | + { |
| 62 | + l.compareBestMatch( *outfile, print_dists, l_two, desc_stats, param.briefinfo ); |
| 63 | + ct++; |
| 64 | + if( float(ct * 100) / len >= float(nextpercent) ) |
| 65 | + { |
| 66 | + cerr << nextpercent << "% " << ct << endl; |
| 67 | + nextpercent += 10; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if( descfile ) |
| 72 | + { |
| 73 | + writeSummary( *descfile, desc_stats, l_one.size() ); |
| 74 | + } |
| 75 | + |
| 76 | + if( ! param.outfile_name.empty() ) |
| 77 | + { |
| 78 | + delete outfile; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +void writeSummary( ostream& descfile, const vector<float>& desc_stats, int sz ) |
| 83 | +{ |
| 84 | + descfile << "========== Summary Stats ==========" << endl |
| 85 | + << "Average values:" << endl |
| 86 | + << setprecision(3); |
| 87 | + for( int i=0; i<128; i++ ) |
| 88 | + { |
| 89 | + if( i%32==0 ) descfile << "X=0 | "; |
| 90 | + if( i%32==8 ) descfile << "X=1 | "; |
| 91 | + if( i%32==16 ) descfile << "X=2 | "; |
| 92 | + if( i%32==24 ) descfile << "X=3 | "; |
| 93 | + float d = desc_stats[i] / sz; |
| 94 | + descfile << setw(8) << d << " "; |
| 95 | + if( i%8==7 ) descfile << endl; |
| 96 | + } |
| 97 | + descfile << endl; |
| 98 | +} |
| 99 | + |
| 100 | +void readFile( vector<feat_t>& vec, const std::string& filename ) |
| 101 | +{ |
| 102 | + ifstream infile( filename ); |
| 103 | + |
| 104 | + if( ! infile.good() ) |
| 105 | + { |
| 106 | + cerr << "File " << filename << " is not open." << endl; |
| 107 | + exit( -1 ); |
| 108 | + } |
| 109 | + |
| 110 | + int lines_read = readFeats( vec, infile ); |
| 111 | + cerr << "Read " << lines_read << " lines from " << filename << endl; |
| 112 | +} |
| 113 | + |
| 114 | +ostream* openOutputFile( const string& outfile_name, ostream* default_stream ) |
| 115 | +{ |
| 116 | + ostream* outfile = default_stream; |
| 117 | + if( outfile_name.empty() ) return outfile; |
| 118 | + |
| 119 | + ostream* o = new ofstream( outfile_name ); |
| 120 | + if( o->good() ) |
| 121 | + { |
| 122 | + outfile = o; |
| 123 | + } |
| 124 | + else |
| 125 | + { |
| 126 | + delete o; |
| 127 | + } |
| 128 | + |
| 129 | + return outfile; |
| 130 | +} |
| 131 | + |
0 commit comments