00001 #ifndef CGH_SCENE_H
00002 #define CGH_SCENE_H
00003
00004 #include <boost/numeric/ublas/vector.hpp>
00005 #include <list>
00006 #include <string>
00007 #include <iostream>
00008 #include <sstream>
00009 #include "cgh.h"
00010 #include "corner.h"
00011 #include "property_image.h"
00012 #include "canny_tool.h"
00013 #include "corner_tool.h"
00014 #include "cgh_wrapper.h"
00015 #include "image_funcs.h"
00016
00017 namespace mimas {
00018
00030 template< typename T >
00031 class cgh_scene: public object
00032 {
00033 public:
00034 typedef boost::numeric::ublas::vector< double > Vector;
00035 typedef std::string string;
00036 typedef std::ostringstream ostringstream;
00037 typedef std::ofstream ofstream;
00038 private:
00039 double comparison_thres;
00040 cgh_wrapper_ptr_list cgh_list;
00041 double cgh_bin_thres;
00042
00043 public:
00044 cgh_scene()
00045 {
00046 comparison_thres=0.75;
00047 cgh_bin_thres = 0.1;
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 void setComparisonThreshold( double d )
00063 { comparison_thres = d; }
00064
00065 void setCGHBinThreshold( double d )
00066 { cgh_bin_thres = d; }
00067
00068
00069 cgh_wrapper_ptr_list getCGHList()
00070 {
00071 return &cgh_list;
00072 }
00073
00074 void createFromImage( image<T>& im )
00075 {
00076 createFromImage( im, 0 );
00077 }
00078
00084 void createFromImage( image<T>& im, double contrast_thres )
00085 {
00086
00087
00088
00089
00090 canny_tool<int> canny;
00091 edge_string_ptr_list edge_strings;
00092
00093
00094 edge_strings=canny.findEdgeStrings( im );
00095 edge_string_op::filterWiggles( edge_strings, 0.2, 1, 35.0 );
00096
00097
00098
00099
00100 corner_image cim;
00101 cim.init( im.getWidth(), im.getHeight() );
00102 corner_tool<int> ctool(0.1);
00103 ctool.filterImage( im, cim );
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 double contrast[500];
00115 corner_ptr crnrs[500];
00116 int i,x,y;
00117
00118 for(i=0;i<500;++i) {
00119 contrast[i]=0;
00120
00121 }
00122
00123 for(y=0;y<cim.getHeight();++y)
00124 {
00125 for(x=0;x<cim.getWidth();++x)
00126 {
00127 if (cim.pixel(x,y))
00128 {
00129 double c;
00130 corner_ptr crnr( boost::dynamic_pointer_cast< corner >
00131 (cim.pixel(x,y) ) );
00132
00133 c=crnr->getContrast();
00134 for (i=0;i<500;++i)
00135 {
00136 if (contrast[i]< c)
00137 {
00138 for(int j=499;j>i;j--)
00139 {
00140 contrast[j]=contrast[j-1];
00141 crnrs[j]=crnrs[j-1];
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 contrast[i]=c;
00153 crnrs[i]=crnr;
00154
00155 break;
00156 }
00157 }
00158 }
00159 }
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 #ifndef NDEBUG
00236 int count=0;
00237 #endif
00238 for(i=0;i<500;++i)
00239 {
00240 if (crnrs[i]==0) break;
00241
00242 cgh_ptr c( new cgh );
00243 c->setBinThreshold( cgh_bin_thres );
00244 c->createFromStrings( edge_strings,crnrs[i]->getPosition() );
00245
00246 cgh_wrapper_ptr cw( new cgh_wrapper );
00247 cw->cgh = c;
00248 cw->position = crnrs[i]->getPosition();
00249 cw->contrast = crnrs[i]->getContrast();
00250
00251 cgh_list.push_back(cw);
00252
00253 #ifndef NDEBUG
00254 std::cerr << "Created cgh number " << ++count
00255 << " contrast=" << cw->contrast
00256 << " position=" << cw->position(0) << " "
00257 << cw->position(1) << std::endl;
00258 #endif
00259 }
00260
00261
00262
00263
00264
00265 }
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00345 cgh_wrapper_ptr_list crossCompare( cgh_scene& other )
00346 {
00347 cgh_wrapper_ptr_list::iterator it;
00348 cgh_wrapper_ptr_list::iterator oit;
00349 cgh_wrapper_ptr_list output;
00350
00351 for(it=cgh_list.begin();it!=cgh_list.end(); ++it)
00352 {
00353 cgh_wrapper_ptr cw = (*it);
00354
00355 if (!cw) continue;
00356
00357 for(oit=other.cgh_list.begin(); oit!=other.cgh_list.end(); ++oit)
00358 {
00359 cgh_wrapper_ptr ocw = (*oit);
00360
00361 if (!ocw) continue;
00362
00363 double olap = dotProduct( *cw->cgh, *ocw->cgh );
00364
00365 if (olap>comparison_thres)
00366 {
00367 if (olap> cw->match_score)
00368 {
00369 cw->match_score = olap;
00370 cw->best_match = ocw;
00371 }
00372 }
00373 }
00374
00375 #ifdef DEBUG_CGH
00376 if (cw->match_score>comparison_thres)
00377 {
00378 mmout << "Feature at " << cw->position(0) << "x"
00379 << cw->position(1) << " matches at "
00380 << cw->best_match->position(0) << "x"
00381 << cw->best_match->position(1) << ", match=" << cw->match_score
00382 << std::endl;
00383 }
00384 #endif
00385 if (cw->match_score>comparison_thres)
00386 {
00387 output.push_back(cw);
00388 }
00389 }
00390
00391 return output;
00392 }
00393
00394 void createFromImageFixedPoints( image<T>& im )
00395 {
00396
00397
00398
00399
00400 canny_tool<int> canny;
00401 edge_string_ptr_list edge_strings;
00402
00403
00404 edge_strings=canny.findEdgeStrings( im );
00405
00406
00407
00408
00409
00410
00411
00412
00413 int lx,ly,ux,uy,stepx,stepy,x,y;
00414 lx = im.getWidth()/4;
00415 ux = lx+im.getWidth()/2;
00416 ly = im.getHeight()/4;
00417 uy = ly+im.getHeight()/2;
00418 stepx = (ux-lx)/10;
00419 stepy = (uy-ly)/10;
00420 int number = 10*10,count=0;
00421
00422 for(y=ly;y<uy;y+=stepy)
00423 {
00424 for(x=lx;x<ux;x+=stepx)
00425 {
00426 cgh_ptr c( new cgh );
00427 Vector pos(2);
00428 pos(0) = (double)x+0.5;
00429 pos(1) = (double)y+0.5;
00430
00431 c->createFromStrings( edge_strings, pos);
00432
00433 cgh_wrapper_ptr cw( new cgh_wrapper );
00434 cw->cgh = c;
00435 cw->position = pos;
00436 cw->contrast = 1.0;
00437
00438 cgh_list.push_back(cw);
00439
00440 #ifndef NDEBUG
00441 std::cerr << "Created cgh number " << ++count << "("
00442 << number << ")" << std::endl;
00443 #endif
00444 }
00445 }
00446
00447
00448
00449
00450
00451 }
00452 };
00453
00454 }
00455
00456 #endif
00457
00458
00459