00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
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';
00057 static const char DarkerFlag = 'd';
00058 static const char SimilarFlag = 's';
00059
00060
00061
00062
00063
00064
00065
00066
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
00092
00093
00094 int main(int argc, char** argv)
00095 {
00096
00097 GUI.LoadFile("extract_features.cfg");
00098 int lastarg = GUI.parseArguments(argc, argv);
00099
00100 create_offsets();
00101
00102
00103 map<string, uint64_t> corners, non_corners;
00104
00105
00106 string scratch(num_offsets, '.');
00107
00108
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
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
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
00145 bool is_corner = faster_detector->detect_corner(im, pos, threshold);
00146
00147
00148
00149
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 }