|
| 1 | +#include <sstream> |
| 2 | +#include <cmath> |
| 3 | +#include <algorithm> |
| 4 | + |
| 5 | +#include "csf_feat.h" |
| 6 | + |
| 7 | +using namespace std; |
| 8 | + |
| 9 | +const float M_PI2 = 2.0f * 3.14159265358979323846f; |
| 10 | + |
| 11 | +bool feat_t::_use_l2_distance = true; |
| 12 | + |
| 13 | +int readFeats( vector<feat_t>& l_one, ifstream& f_one ) |
| 14 | +{ |
| 15 | + char buffer[1024]; |
| 16 | + int lines_read; |
| 17 | + |
| 18 | + lines_read = 0; |
| 19 | + while( f_one.good() ) |
| 20 | + { |
| 21 | + f_one.getline( buffer, 1024 ); |
| 22 | + if( f_one.good() ) |
| 23 | + { |
| 24 | + bool success = addFeat( l_one, buffer ); |
| 25 | + if( success ) |
| 26 | + { |
| 27 | + lines_read++; |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + return lines_read; |
| 32 | +} |
| 33 | + |
| 34 | +bool addFeat( vector<feat_t>& features, char* line ) |
| 35 | +{ |
| 36 | + vector<float> values(5+128); // 4 or 5 values followed by 128 desc values |
| 37 | + |
| 38 | + int i = 0; |
| 39 | + istringstream s( line ); |
| 40 | + while( s >> values[i] ) |
| 41 | + { |
| 42 | + i++; |
| 43 | + } |
| 44 | + |
| 45 | + // cerr << "Found " << i << " floats in line" << endl; |
| 46 | + features.emplace_back( i, values ); |
| 47 | + |
| 48 | + if( i == 0 ) return false; |
| 49 | + return true; |
| 50 | +} |
| 51 | + |
| 52 | +feat_t::feat_t( int num, const vector<float>& input ) |
| 53 | + : desc(128) |
| 54 | +{ |
| 55 | + auto it = input.begin(); |
| 56 | + auto to = desc.begin(); |
| 57 | + if( num == 132 ) |
| 58 | + { |
| 59 | + x = *it++; |
| 60 | + y = *it++; |
| 61 | + sigma = *it++; |
| 62 | + ori = *it++; |
| 63 | + for( int i=0; i<128; i++ ) *to++ = *it++; |
| 64 | + } |
| 65 | + else if( num == 133 ) |
| 66 | + { |
| 67 | + float odbss; |
| 68 | + x = *it++; |
| 69 | + y = *it++; |
| 70 | + odbss = *it++; |
| 71 | + sigma = odbss == 0.0f ? 0.0f : sqrtf( 1.0f / odbss ); |
| 72 | + ori = 0.0f; |
| 73 | + it++; |
| 74 | + it++; |
| 75 | + for( int i=0; i<128; i++ ) *to++ = *it++; |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + cerr << "The keypoint line contains an unexpected number of floats (" << num << ")" << endl; |
| 80 | + return; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +void feat_t::print( ostream& ostr ) const |
| 85 | +{ |
| 86 | + ostr << "(" << x << "," << y << ")"; |
| 87 | + ostr << " sigma=" << sigma << " ori=" << ori; |
| 88 | + for( auto it : desc ) |
| 89 | + { |
| 90 | + ostr << " " << it; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +void feat_t::compareBestMatch( ostream& ostr, ostream* dstr, const vector<feat_t>& l_one, vector<float>& desc_stats, bool minOnly ) const |
| 95 | +{ |
| 96 | + vector<float> distances; |
| 97 | + distances.reserve( l_one.size() ); |
| 98 | + |
| 99 | + if( !minOnly ) ostr << "==========" << endl; |
| 100 | + for( auto r : l_one ) |
| 101 | + { |
| 102 | + float v = dist( r ); |
| 103 | + distances.push_back( v ); |
| 104 | + } |
| 105 | + |
| 106 | + auto it = distances.begin(); |
| 107 | + |
| 108 | + auto m = min_element( distances.begin(), distances.end() ); |
| 109 | + |
| 110 | + float second = INFINITY; |
| 111 | + |
| 112 | + for( auto r : l_one ) |
| 113 | + { |
| 114 | + if( minOnly ) |
| 115 | + { |
| 116 | + if( it == m ) |
| 117 | + { |
| 118 | + ostr << "desc dist " << *it |
| 119 | + << " MIN" |
| 120 | + << " pixdist " << sqrtf( (x-r.x)*(x-r.x) + (y-r.y)*(y-r.y) ) |
| 121 | + << " angledist " << fabsf( ori/M_PI2*360.0f - r.ori/M_PI2*360.0f ); |
| 122 | + |
| 123 | + if( dstr ) |
| 124 | + { |
| 125 | + auto left = desc.begin(); |
| 126 | + auto right = r.desc.begin(); |
| 127 | + for( int i=0; i<128; i++, left++, right++ ) |
| 128 | + { |
| 129 | + float diff = *left - *right; |
| 130 | + (*dstr) << diff << " "; |
| 131 | + desc_stats[i] += diff; |
| 132 | + } |
| 133 | + (*dstr) << endl; |
| 134 | + } |
| 135 | + } |
| 136 | + else if( *it > *m ) |
| 137 | + { |
| 138 | + second = min<float>( second, *it ); |
| 139 | + } |
| 140 | + it++; |
| 141 | + } |
| 142 | + else |
| 143 | + { |
| 144 | + ostr << "desc dist " << *it; |
| 145 | + if( it == m ) |
| 146 | + ostr << " MIN "; |
| 147 | + else |
| 148 | + ostr << " "; |
| 149 | + it++; |
| 150 | + ostr << " pixdist " << sqrtf( (x-r.x)*(x-r.x) + (y-r.y)*(y-r.y) ) |
| 151 | + << " angledist " << fabsf( ori/M_PI2*360.0f - r.ori/M_PI2*360.0f ) |
| 152 | + << endl; |
| 153 | + } |
| 154 | + } |
| 155 | + if( minOnly ) |
| 156 | + { |
| 157 | + ostr << " 2best " << second << endl; |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +float feat_t::dist( const feat_t& r ) const |
| 162 | +{ |
| 163 | + if( _use_l2_distance ) |
| 164 | + { |
| 165 | + float sum = 0.0f; |
| 166 | + auto it_r = r.desc.begin(); |
| 167 | + for( auto l : desc ) |
| 168 | + { |
| 169 | + float val = l - *it_r++; |
| 170 | + sum += ( val * val ); |
| 171 | + } |
| 172 | + return sqrtf( sum ); |
| 173 | + } |
| 174 | + else |
| 175 | + { |
| 176 | + float sum = 0.0f; |
| 177 | + auto it_r = r.desc.begin(); |
| 178 | + for( auto l : desc ) |
| 179 | + { |
| 180 | + float val = l - *it_r++; |
| 181 | + sum += fabsf( val ); |
| 182 | + } |
| 183 | + return sum / 128; |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +void feat_t::setL2Distance( bool onoff ) |
| 188 | +{ |
| 189 | + _use_l2_distance = onoff; |
| 190 | +} |
| 191 | + |
0 commit comments