Przeglądaj źródła

persistent version of LocalFeatureColorWeijer

Alexander Freytag 11 lat temu
rodzic
commit
2facbc0bee

+ 115 - 14
features/localfeatures/LocalFeatureColorWeijer.cpp

@@ -32,12 +32,25 @@ const int colors[11][3] =
   {255, 255,   0}, // yellow
   {255, 255,   0}, // yellow
 };
 };
 
 
+OBJREC::LocalFeatureColorWeijer::LocalFeatureColorWeijer()
+{
+  this->tfile = "";
+}
 
 
-LocalFeatureColorWeijer::LocalFeatureColorWeijer( const Config *c )
+
+LocalFeatureColorWeijer::LocalFeatureColorWeijer( const NICE::Config *_conf )
 {
 {
-  this->conf = c;
+  this->initFromConfig ( _conf );
+}
 
 
-  this->tfile = this->conf->gS( "LocalFeatureColorWeijer", "table", "");
+LocalFeatureColorWeijer::~LocalFeatureColorWeijer()
+{
+}
+
+void LocalFeatureColorWeijer::initFromConfig ( const NICE::Config * _conf, const std::string & _confSection )
+{
+
+  this->tfile = _conf->gS( "LocalFeatureColorWeijer", "table", "");
   
   
   // If no destination  to the LUT was given, we try to use the file shipped with this library
   // If no destination  to the LUT was given, we try to use the file shipped with this library
   // Therefore, we try to catch the NICEHOME and if possible, append the location where the LUT file is stored
   // Therefore, we try to catch the NICEHOME and if possible, append the location where the LUT file is stored
@@ -54,11 +67,7 @@ LocalFeatureColorWeijer::LocalFeatureColorWeijer( const Config *c )
     }
     }
   }
   }
   
   
-  this->restore();
-}
-
-LocalFeatureColorWeijer::~LocalFeatureColorWeijer()
-{
+  this->restoreLUT();  
 }
 }
 
 
 int LocalFeatureColorWeijer::getDescSize() const
 int LocalFeatureColorWeijer::getDescSize() const
@@ -66,7 +75,7 @@ int LocalFeatureColorWeijer::getDescSize() const
   return LASTCOLOR;
   return LASTCOLOR;
 }
 }
 
 
-void LocalFeatureColorWeijer::restore()
+void LocalFeatureColorWeijer::restoreLUT()
 {
 {
   ifstream fin( this->tfile.c_str() );
   ifstream fin( this->tfile.c_str() );
   if( !fin.is_open() )
   if( !fin.is_open() )
@@ -74,7 +83,7 @@ void LocalFeatureColorWeijer::restore()
     fthrow(Exception,"ColorWeijer: could not find lookup table file. Specify the path in the config, e.g., YOURNICEHOME/vislearning/features/localfeatures/input/colorWeijer/w2c.txt");
     fthrow(Exception,"ColorWeijer: could not find lookup table file. Specify the path in the config, e.g., YOURNICEHOME/vislearning/features/localfeatures/input/colorWeijer/w2c.txt");
   }
   }
   
   
-  while(!fin.eof())
+  while( !fin.eof() )
   {
   {
     double rd,gd,bd;
     double rd,gd,bd;
     int r,g,b;
     int r,g,b;
@@ -115,9 +124,6 @@ int LocalFeatureColorWeijer::getDescriptors( const NICE::Image & img, VVector &
 
 
 int LocalFeatureColorWeijer::getDescriptors( const NICE::ColorImage & img, VVector & positions, VVector & features ) const
 int LocalFeatureColorWeijer::getDescriptors( const NICE::ColorImage & img, VVector & positions, VVector & features ) const
 {
 {
-  int width = ( int )img.width();
-  int height = ( int )img.height();
-
   for ( int j = 0; j < ( int )positions.size(); j++ )
   for ( int j = 0; j < ( int )positions.size(); j++ )
   {
   {
     int x = positions[j][0];
     int x = positions[j][0];
@@ -126,7 +132,7 @@ int LocalFeatureColorWeijer::getDescriptors( const NICE::ColorImage & img, VVect
     int g = img(x,y,1)/8;
     int g = img(x,y,1)/8;
     int b = img(x,y,2)/8;
     int b = img(x,y,2)/8;
       
       
-    Vector feat( 11 );
+    NICE::Vector feat( 11 );
     for ( uint i = 0; i < 11; i++ )
     for ( uint i = 0; i < 11; i++ )
     {
     {
       feat[i] = hist[r][g][b][i];
       feat[i] = hist[r][g][b][i];
@@ -268,3 +274,98 @@ void LocalFeatureColorWeijer::getFeats( const ColorImage &img, MultiChannelImage
 
 
   return;
   return;
 }
 }
+
+///////////////////// INTERFACE PERSISTENT /////////////////////
+// interface specific methods for store and restore
+///////////////////// INTERFACE PERSISTENT ///////////////////// 
+
+void LocalFeatureColorWeijer::restore ( std::istream & is, int format )
+{
+  //delete everything we knew so far...
+  this->clear();
+  
+  bool b_restoreVerbose ( false );
+#ifdef B_RESTOREVERBOSE
+  b_restoreVerbose = true;
+#endif  
+  
+  if ( is.good() )
+  {
+    if ( b_restoreVerbose ) 
+      std::cerr << " restore LocalFeatureColorWeijer" << std::endl;
+    
+    std::string tmp;
+    is >> tmp; //class name 
+    
+    if ( ! this->isStartTag( tmp, "LocalFeatureColorWeijer" ) )
+    {
+      std::cerr << " WARNING - attempt to restore LocalFeatureColorWeijer, but start flag " << tmp << " does not match! Aborting... " << std::endl;
+      throw;
+    }   
+    
+    bool b_endOfBlock ( false ) ;
+    
+    while ( !b_endOfBlock )
+    {
+      is >> tmp; // start of block 
+      
+      if ( this->isEndTag( tmp, "LocalFeatureColorWeijer" ) )
+      {
+        b_endOfBlock = true;
+        continue;
+      }      
+      
+      tmp = this->removeStartTag ( tmp );
+      
+      if ( b_restoreVerbose )
+        std::cerr << " currently restore section " << tmp << " in LocalFeatureColorWeijer" << std::endl;
+           
+      if ( tmp.compare("tfile") == 0 )
+      {
+        is >> this->tfile;
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      } 
+      else
+      {
+      std::cerr << "WARNING -- unexpected LocalFeatureColorWeijer object -- " << tmp << " -- for restoration... aborting" << std::endl;
+      throw;
+      }
+    }
+  }
+  else
+  {
+    std::cerr << "LocalFeatureColorWeijer::restore -- InStream not initialized - restoring not possible!" << std::endl;
+    throw;
+  }
+  
+  // try to load the LUT from a separate external file
+  //NOTE we could also think about adding the LUT into the store/restore process. However, the table is BIG, so it would
+  // blow up whole outfile
+  this->restoreLUT ( ) ;
+}
+
+void LocalFeatureColorWeijer::store ( std::ostream & os, int format ) const
+{ 
+  if (os.good())
+  {
+    // show starting point
+    os << this->createStartTag( "LocalFeatureColorWeijer" ) << std::endl;    
+       
+    os << this->createStartTag( "tfile" ) << std::endl;
+    os << this->tfile << std::endl;
+    os << this->createEndTag( "tfile" ) << std::endl;     
+    
+    // done
+    os << this->createEndTag( "LocalFeatureColorWeijer" ) << std::endl;    
+  }
+  else
+  {
+    std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
+  }
+}
+
+void LocalFeatureColorWeijer::clear ()
+{
+ //TODO
+}

+ 54 - 10
features/localfeatures/LocalFeatureColorWeijer.h

@@ -9,6 +9,7 @@
 
 
 // nice-core includes
 // nice-core includes
 #include <core/basics/Config.h>
 #include <core/basics/Config.h>
+#include <core/basics/Persistent.h>
 // 
 // 
 #include <core/image/ImageT.h>
 #include <core/image/ImageT.h>
 #include <core/image/MultiChannelImageT.h>
 #include <core/image/MultiChannelImageT.h>
@@ -25,12 +26,12 @@
 namespace OBJREC {
 namespace OBJREC {
 
 
 /** interface to ColorSande implementation */
 /** interface to ColorSande implementation */
-class LocalFeatureColorWeijer : public LocalFeature
+class LocalFeatureColorWeijer : public LocalFeature, public NICE::Persistent //TODO move Persistent inheritence to LocalFeature
 {
 {
 
 
   protected:
   protected:
     //! enum for 11 main colors
     //! enum for 11 main colors
-    enum
+    enum NAMEBLECOLORS
     {
     {
       BLACK = 0,
       BLACK = 0,
       BLUE,
       BLUE,
@@ -52,16 +53,34 @@ class LocalFeatureColorWeijer : public LocalFeature
     //! destination of the precomputed lookuptable
     //! destination of the precomputed lookuptable
     std::string tfile;
     std::string tfile;
     
     
-    //! configuration file
-    const NICE::Config *conf;
-
   public:
   public:
 
 
-    /** simple constructor */
-    LocalFeatureColorWeijer ( const NICE::Config *c );
+    
+    /** 
+     * @brief default constructor 
+     * @author Alexander Freytag
+     * @date 06-02-2014 ( dd-mm-yyyy )
+     */
+    LocalFeatureColorWeijer ( );
+    
+    /** 
+     * @brief recommended constructor, calls initFromConfig
+     * @author Alexander Freytag
+     * @date 06-02-2014 ( dd-mm-yyyy )
+     */
+    LocalFeatureColorWeijer ( const NICE::Config * _conf );
 
 
-    /** simple destructor */
+    /** 
+     * @brief simple destructor
+     */
     virtual ~LocalFeatureColorWeijer();
     virtual ~LocalFeatureColorWeijer();
+    
+    /** 
+     * @brief Jobs previously performed in the config-version of the constructor, read settings etc.
+     * @author Alexander Freytag
+     * @date 06-02-2014 ( dd-mm-yyyy )
+     */    
+    void initFromConfig ( const NICE::Config * _conf, const std::string & _confSection = "LocalFeatureColorWeijer");
 
 
     /**
     /**
      * get the size of the descriptor
      * get the size of the descriptor
@@ -117,9 +136,9 @@ class LocalFeatureColorWeijer : public LocalFeature
     void visualizeFeatures ( const NICE::ColorImage & cimg, NICE::ColorImage & out ) const;
     void visualizeFeatures ( const NICE::ColorImage & cimg, NICE::ColorImage & out ) const;
 
 
     /**
     /**
-     * load parameters
+     * @brief load parameters (look up table for color correspondences) from external file (~3MB)
      */
      */
-    void restore();
+    void restoreLUT();
 
 
     /**
     /**
      * finds a colorname in a given string
      * finds a colorname in a given string
@@ -134,6 +153,31 @@ class LocalFeatureColorWeijer : public LocalFeature
      * @param feats feature vector for each pixel
      * @param feats feature vector for each pixel
      */
      */
     void getFeats ( const NICE::ColorImage &img, NICE::MultiChannelImageT<double> &feats );
     void getFeats ( const NICE::ColorImage &img, NICE::MultiChannelImageT<double> &feats );
+    
+    ///////////////////// INTERFACE PERSISTENT /////////////////////
+    // interface specific methods for store and restore
+    ///////////////////// INTERFACE PERSISTENT /////////////////////   
+    
+    /** 
+     * @brief Load object from external file (stream)
+     * @author Alexander Freytag
+     * @date 06-02-2014 ( dd-mmyyyy)
+     */     
+    virtual void restore ( std::istream & is, int format = 0 );
+    
+    /** 
+     * @brief Save object to external file (stream)
+     * @author Alexander Freytag
+     * @date 06-02-2014 ( dd-mmyyyy)
+     */       
+    virtual void store( std::ostream & os, int format = 0 ) const;
+    
+    /** 
+     * @brief Clear  object
+     * @author Alexander Freytag
+     * @date 06-02-2014 ( dd-mmyyyy)
+     */    
+    virtual void clear ();       
 };
 };