cgh_scene.h

Go to the documentation of this file.
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 //     ~cgh_scene()
00051 //     {
00052 //  for(cgh_wrapper_ptr_list::iterator it=cgh_list.begin();
00053 //        it!=cgh_list.end();++it)
00054 //  {
00055 //      cgh_wrapper *cw = (*it);
00056 
00057 //      delete cw;
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   //extract edges and corners,
00087   //orient the edge strings and,
00088   //create a cgh from all corners
00089 
00090         canny_tool<int>   canny;
00091         edge_string_ptr_list edge_strings;
00092 
00093   //(1) first run canny to extract a set of string_el strings
00094         edge_strings=canny.findEdgeStrings( im );
00095         edge_string_op::filterWiggles( edge_strings, 0.2, 1, 35.0 );
00096 
00097 //(1.5) contrast orient the strings?
00098 
00099   //(2) find the corners
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   //(2.5) calculate the contrast threshold value. we could use
00106   //contrast values directly here, but it's very hard to just come up with
00107   //a number as they vary a lot. therefore, assume the given threshold value is
00108   //as a percentage of the max val;
00109   
00110   //(2.5b) ... new algo by stumeikle Wed 25 Sep 2002 21:10:01 CEST
00111   //we'll contrast order the corners and then attempt to find at least
00112   //100 from each image. actually we need to be able to accept a large
00113   //number of high contrast features so we'll allocate enough for 500
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      // crnrs[i]=0;
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         memcpy( &contrast[i+1],
00145           &contrast[i],
00146           sizeof(double)*(500-i-1) );
00147         memcpy( &crnrs[i+1],
00148           &crnrs[i],
00149           sizeof(corner*)*(500-i-1));
00150         */
00151           
00152           contrast[i]=c;
00153           crnrs[i]=crnr;
00154 
00155           break;
00156       }
00157         }
00158     }
00159       }
00160   } 
00161 
00162 
00163 /*
00164   double  mv=0;
00165   int x,y,count=0,number=0;
00166         for(y=0;y<cim.getHeight();++y)
00167         {
00168       for(x=0;x<cim.getWidth();++x)
00169       {
00170     if (cim.getPixel(x,y))
00171     {
00172         corner  *crnr= ((corner*)(cim.getPixel(x,y)));
00173         if (crnr->getContrast() > mv)
00174         {
00175       mv = crnr->getContrast();
00176         }
00177     }
00178       }
00179   }
00180 
00181   mv*=contrast_thres;
00182 
00183   //----- this bit is purely for user info and can be removed entirely
00184   // if desired . stumeikle.
00185   if (info)
00186   {
00187         for(y=0;y<cim.getHeight();++y)
00188         {
00189       for(x=0;x<cim.getWidth();++x)
00190       {
00191     if (cim.getPixel(x,y))
00192     {
00193         corner  *crnr= ((corner*)(cim.getPixel(x,y)));
00194         if (crnr->getContrast() > mv)
00195         {
00196       number++;
00197         }
00198     }
00199       }
00200   }
00201   }
00202   //----- end of this bit
00203 
00204         //(3) find the corners and generate histograms
00205         for(y=0;y<cim.getHeight();++y)
00206         {
00207       for(x=0;x<cim.getWidth();++x)
00208       {
00209     if (cim.getPixel(x,y))
00210     {
00211         corner  *crnr= ((corner*)(cim.getPixel(x,y)));
00212 
00213         if (crnr->getContrast()<mv)
00214       continue;
00215 
00216         cgh *cgh = new cgh;
00217 
00218         cgh->setBinThreshold( cgh_bin_thres );
00219         cgh->createFromStrings( edge_strings,crnr->getPosition() );
00220 
00221         cgh_wrapper *cw = new cgh_wrapper;
00222         cw->cgh = cgh;
00223         cw->position = crnr->getPosition();
00224         cw->contrast = crnr->getContrast();
00225 
00226         cgh_list.push_back(cw);
00227 
00228         if (info)
00229       mm->Message() << "Created cgh number " << ++count << "("
00230               << number << ")" << std::endl;
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         //(4) delete the strings
00262   //edge_string_ptr_list::iterator  sit;
00263   // for(sit=edge_strings.begin();sit!=edge_strings.end();++sit)
00264    // delete (*sit);
00265     }
00266 
00267     /*
00268     void  dumpAsPGMs( string basename ) ///< dump all generated cghs as .pgm files so they can be checked
00269     {
00270   //primarily for visual inspection -> the histograms are scaled and
00271   //the rest of the wrappers are ignored.
00272       cgh_wrapper_ptr_list::iterator  it;
00273   int       num=0;
00274   char        opfn[1024];
00275 
00276   for(it=cgh_list.begin();it!=cgh_list.end();++it)
00277   {
00278       string  name;
00279       int   n1,n2,n3,n;
00280 
00281       std::ostringstream    ss(opfn,1024);
00282 
00283       n = num;
00284       n1 = n%10;  n/=10;
00285       n2 = n%10;  n/=10;
00286       n3 = n;
00287       ss << basename << n3 << n2 << n1 << ".pgm" << std::ends;
00288 
00289       cgh *cgh = (*it)->cgh;
00290       cgh->scale(0,255);
00291       cgh->writeToFile( opfn, "PGM" );
00292 
00293       num++;
00294   }
00295     }*/
00296 
00297     /* void dumpAll( string basename )
00298     {
00299   //scrap this ?? Mon 10 Jun 2002 19:25:56 CEST
00300   //
00301    cgh_wrapper_ptr_list::iterator it;
00302   int       num=0;
00303   char        opfn[1024];
00304 
00305   for(it=cgh_list.begin();it!=cgh_list.end();++it)
00306   {
00307       string  name;
00308       int   n1,n2,n3,n;
00309 
00310       std::ostringstream      ss(opfn,1024);
00311 
00312       n = num;
00313       n1 = n%10;  n/=10;
00314       n2 = n%10;  n/=10;
00315       n3 = n;
00316       ss << basename << n3 << n2 << n1 << ".pgm" << std::ends;
00317 
00318       cgh *cgh = (*it)->cgh;
00319       cgh->scale(0,255);
00320       cgh->writeToFile( opfn, "PGM" );
00321 
00322       //now write out the wrapper details
00323       std::ostringstream      ss2(opfn,1024);
00324       ss2 << basename << n3 << n2 << n1 << ".info" << std::ends;
00325 
00326       ofstream      opf( opfn );
00327 
00328 //      opf << "Name=" << (*it)->image_id << std::endl;
00329       opf << "Position=" << (*it)->position(0) << "," << (*it)->position(1) << std::endl;
00330       opf << "Contrast=" << (*it)->contrast << std::endl;
00331 
00332       opf.close();
00333 
00334       num++;
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   //extract edges and corners,
00397   //orient the edge strings and,
00398   //create a cgh from all corners
00399 
00400         canny_tool<int>   canny;
00401         edge_string_ptr_list edge_strings;
00402 
00403   //(1) first run canny to extract a set of string_el strings
00404         edge_strings=canny.findEdgeStrings( im );
00405  //       edge_string_op::filterWiggles( edge_strings, 0.2, 1, 35.0 );
00406 
00407 //(1.5) contrast orient the strings?
00408 
00409   //(2) find the corners (not now)
00410   //(2.5) calculate the contrast threshold value.(not now)
00411 
00412         //(3) generate histograms
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         //(4) delete the strings
00448         //edge_string_ptr_list::iterator  sit;
00449         //for(sit=edge_strings.begin();sit!=edge_strings.end();++sit)
00450         //delete (*sit);
00451     }
00452 };
00453 
00454 }
00455 
00456 #endif
00457 
00458 
00459 

[GNU/Linux] [Qt] [Mesa] [STL] [Lapack] [Boost] [Magick++] [Xalan-C and Xerces-C] [doxygen] [graphviz] [FFTW] [popt] [xine] [Gnuplot] [gnu-arch] [gcc] [gstreamer] [autoconf/automake/make] [freshmeat.net] [opensource.org] [sourceforge.net] [MMVL]
mimas 2.1 - Copyright Mon Oct 30 11:31:16 2006, Bala Amavasai, Stuart Meikle, Arul Selvan, Fabio Caparrelli, Jan Wedekind, Manuel Boissenin, ...