Browse Source

LocalFeatureSelection fully persistent (throwing exceptions if not implemented)

Alexander Freytag 11 years ago
parent
commit
4dd9c3ee7e

+ 22 - 0
features/localfeatures/LFGenericLocal.cpp

@@ -52,3 +52,25 @@ void LFGenericLocal::visualizeFeatures ( NICE::Image & mark,
     lf->visualizeFeatures ( mark, positions, color );
 }
 
+///////////////////// INTERFACE PERSISTENT /////////////////////
+// interface specific methods for store and restore
+///////////////////// INTERFACE PERSISTENT ///////////////////// 
+
+void LFGenericLocal::restore ( std::istream & is, int format )
+{ 
+  fthrow ( Exception, "LFGenericLocal::restore not implemented yet." );
+  //TODO
+}
+
+void LFGenericLocal::store ( std::ostream & os, int format ) const
+{ 
+  fthrow ( Exception, "LFGenericLocal::store not implemented yet." );
+  //TODO  
+}
+
+void LFGenericLocal::clear ()
+{ 
+  fthrow ( Exception, "LFGenericLocal::clear not implemented yet." );
+  //TODO  
+}
+

+ 25 - 0
features/localfeatures/LFGenericLocal.h

@@ -48,6 +48,31 @@ namespace OBJREC {
     void visualizeFeatures ( NICE::Image & mark,
           const NICE::VVector & positions,
           size_t color ) const;
+          
+    ///////////////////// INTERFACE PERSISTENT /////////////////////
+    // interface specific methods for store and restore
+    ///////////////////// INTERFACE PERSISTENT /////////////////////   
+    
+    /** 
+     * @brief Load object from external file (stream)
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mmyyyy)
+     */     
+    virtual void restore ( std::istream & is, int format = 0 );
+    
+    /** 
+     * @brief Save object to external file (stream)
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mmyyyy)
+     */       
+    virtual void store( std::ostream & os, int format = 0 ) const;
+    
+    /** 
+     * @brief Clear  object
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mmyyyy)
+     */    
+    virtual void clear ();           
 
   };
 

+ 22 - 0
features/localfeatures/LFMikolajczyk.cpp

@@ -172,3 +172,25 @@ int LFMikolajczyk::extractFeatures ( const NICE::Image & img, VVector & features
 
     return 0;
 }
+
+///////////////////// INTERFACE PERSISTENT /////////////////////
+// interface specific methods for store and restore
+///////////////////// INTERFACE PERSISTENT ///////////////////// 
+
+void LFMikolajczyk::restore ( std::istream & is, int format )
+{ 
+  fthrow ( Exception, "LFMikolajczyk::restore not implemented yet." );
+  //TODO  
+}
+
+void LFMikolajczyk::store ( std::ostream & os, int format ) const
+{ 
+  fthrow ( Exception, "LFMikolajczyk::store not implemented yet." );
+  //TODO  
+}
+
+void LFMikolajczyk::clear ()
+{ 
+  fthrow ( Exception, "LFMikolajczyk::clear not implemented yet." );
+  //TODO  
+}

+ 25 - 0
features/localfeatures/LFMikolajczyk.h

@@ -42,6 +42,31 @@ class LFMikolajczyk : public LocalFeatureRepresentation
 
 	int extractFeatures ( const NICE::Image & img, NICE::VVector & features, 
 			      NICE::VVector & positions ) const;
+            
+    ///////////////////// INTERFACE PERSISTENT /////////////////////
+    // interface specific methods for store and restore
+    ///////////////////// INTERFACE PERSISTENT /////////////////////   
+    
+    /** 
+     * @brief Load object from external file (stream)
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mmyyyy)
+     */     
+    virtual void restore ( std::istream & is, int format = 0 );
+    
+    /** 
+     * @brief Save object to external file (stream)
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mmyyyy)
+     */       
+    virtual void store( std::ostream & os, int format = 0 ) const;
+    
+    /** 
+     * @brief Clear  object
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mmyyyy)
+     */    
+    virtual void clear ();              
      
 };
 

+ 21 - 0
features/localfeatures/LFPatches.cpp

@@ -296,3 +296,24 @@ void LFPatches::visualize(NICE::Image & img, const NICE::Vector & feature) const
 		}
 }
 
+///////////////////// INTERFACE PERSISTENT /////////////////////
+// interface specific methods for store and restore
+///////////////////// INTERFACE PERSISTENT ///////////////////// 
+
+void LFPatches::restore ( std::istream & is, int format )
+{ 
+  fthrow ( Exception, "LFPatches::restore not implemented yet." );
+  //TODO  
+}
+
+void LFPatches::store ( std::ostream & os, int format ) const
+{ 
+  fthrow ( Exception, "LFPatches::store not implemented yet." );
+  //TODO  
+}
+
+void LFPatches::clear ()
+{ 
+  fthrow ( Exception, "LFPatches::clear not implemented yet." );
+  //TODO  
+}

+ 25 - 0
features/localfeatures/LFPatches.h

@@ -65,6 +65,31 @@ public:
 //DIRTY for bofCreationSingleImage
 	int calcDescriptor(const ImagePyramid & imp, const NICE::Vector & position,
 				NICE::Vector & desc) const;
+        
+    ///////////////////// INTERFACE PERSISTENT /////////////////////
+    // interface specific methods for store and restore
+    ///////////////////// INTERFACE PERSISTENT /////////////////////   
+    
+    /** 
+     * @brief Load object from external file (stream)
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mmyyyy)
+     */     
+    virtual void restore ( std::istream & is, int format = 0 );
+    
+    /** 
+     * @brief Save object to external file (stream)
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mmyyyy)
+     */       
+    virtual void store( std::ostream & os, int format = 0 ) const;
+    
+    /** 
+     * @brief Clear  object
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mmyyyy)
+     */    
+    virtual void clear ();          
 
 };
 

+ 216 - 25
features/localfeatures/LFReadCache.cpp

@@ -1,56 +1,120 @@
 /**
 * @file LFReadCache.cpp
 * @brief read local features from file
-* @author Erik Rodner
+* @author Erik Rodner, Alexander Freytag
 * @date 02/14/2008
-
 */
+
+// STL includes
 #include <iostream>
 #include <fstream>
 
-#include "vislearning/baselib/Globals.h"
-#include "core/basics/StringTools.h"
-#include "core/basics/FileMgt.h"
+// nice-core includes
+#include <core/basics/StringTools.h>
+#include <core/basics/FileMgt.h>
 
+// nice-vislearning includes
+#include <vislearning/baselib/Globals.h>
+// 
+#include "vislearning/features/localfeatures/GenericLFSelection.h"
 #include "vislearning/features/localfeatures/LFReadCache.h"
 
 using namespace OBJREC;
 using namespace std;
 using namespace NICE;
 
-LFReadCache::LFReadCache ( const Config *conf,
-                           const LocalFeatureRepresentation *_lfrep,
+///////////////////// ///////////////////// /////////////////////
+//                   PROTECTED METHODS
+///////////////////// ///////////////////// ////////////////
+
+void LFReadCache::setDescFormat ( const std::string & _descFormat_s )
+{
+  if ( _descFormat_s == "binary_double" )
+    this->descFormat = VVector::FILEFORMAT_BINARY_DOUBLE;
+  else if ( _descFormat_s == "binary_uchar" )
+    this->descFormat = VVector::FILEFORMAT_BINARY_CHAR;
+  else if ( _descFormat_s == "text_line" )
+    this->descFormat = VVector::FILEFORMAT_LINE;
+  else if ( ( _descFormat_s == "text_nice" ) || ( _descFormat_s == "text_ice" ) )
+    this->descFormat = VVector::FILEFORMAT_NICE;
+  else
+    fthrow ( Exception, "Format " << _descFormat_s << " is unknown." );  
+}
+
+///////////////////// ///////////////////// /////////////////////
+//                   CONSTRUCTORS / DESTRUCTORS
+///////////////////// ///////////////////// /////////////////
+
+LFReadCache::LFReadCache ()
+{
+  this->numFeatures        = -1;
+  this->cachedir           = "";
+  this->cachemode          = Globals::getCacheMode ( "cat" );
+  std::string descFormat_s = "binary_double";
+  this->setDescFormat ( descFormat_s );
+}
+
+LFReadCache::LFReadCache ( const Config *_conf,
+                           const std::string & _confSection
+                         )
+{
+  this->initFromConfig( _conf, _confSection );  
+}
+
+LFReadCache::LFReadCache ( const Config *_conf,
+                           LocalFeatureRepresentation *_lfrep,
                            int _numFeatures,
-                           const std::string & _section ) : lfrep ( _lfrep )
+                           const std::string & _confSection ) : lfrep ( _lfrep )
 {
   //srand(time(NULL));
-  numFeatures = _numFeatures;
-  cachedir = conf->gS ( _section, "root" );
-  cachemode = Globals::getCacheMode ( conf->gS ( _section, "mode", "cat" ) );
-
-  std::string descFormat_s = conf->gS ( _section, "descriptor_format", "binary_double" );
-  if ( descFormat_s == "binary_double" )
-    descFormat = VVector::FILEFORMAT_BINARY_DOUBLE;
-  else if ( descFormat_s == "binary_uchar" )
-    descFormat = VVector::FILEFORMAT_BINARY_CHAR;
-  else if ( descFormat_s == "text_line" )
-    descFormat = VVector::FILEFORMAT_LINE;
-  else if ( ( descFormat_s == "text_nice" ) || ( descFormat_s == "text_ice" ) )
-    descFormat = VVector::FILEFORMAT_NICE;
-  else
-    fthrow ( Exception, "Format " << descFormat_s << " is unknown." );
+  this->numFeatures = _numFeatures;
+  this->cachedir = _conf->gS ( _confSection, "root" );
+  this->cachemode = Globals::getCacheMode ( _conf->gS ( _confSection, "mode", "cat" ) );
+
+  std::string descFormat_s = _conf->gS ( _confSection, "descriptor_format", "binary_double" );
+  this->setDescFormat ( descFormat_s );
 }
 
 LFReadCache::~LFReadCache()
 {
+  if ( this->lfrep != NULL )
+  {
+    delete this->lfrep;
+    this->lfrep = NULL;
+  }
+}
+
+void LFReadCache::initFromConfig(const NICE::Config* _conf, const std::string& _confSection)
+{
+  //srand(time(NULL));
+  this->numFeatures = _conf->gI ( _confSection, "numFeatures", -1 );
+  this->cachedir = _conf->gS ( _confSection, "root" );
+  this->cachemode = Globals::getCacheMode ( _conf->gS ( _confSection, "mode", "cat" ) );
+
+  std::string descFormat_s = _conf->gS ( _confSection, "descriptor_format", "binary_double" );
+  this->setDescFormat ( descFormat_s );
+  
+  if ( this->lfrep != NULL )
+  {
+    delete this->lfrep;
+    this->lfrep = NULL;
+  }  
+  this->lfrep = GenericLFSelection::selectLocalFeatureRep (  _conf, _confSection );  
 }
 
+
+///////////////////// ///////////////////// /////////////////////
+//                      FEATURE STUFF
+///////////////////// ///////////////////// /////////////////
+
 int LFReadCache::getDescSize () const
 {
-  if ( lfrep == NULL ) {
+  if ( this->lfrep == NULL )
+  {
     fthrow ( Exception, "Unable to get descriptor size! (cache mode)" );
     return -1;
-  } else {
+  }
+  else {
     return lfrep->getDescSize();
   }
 }
@@ -83,3 +147,130 @@ void LFReadCache::visualizeFeatures ( NICE::Image & mark,
   lfrep->visualizeFeatures ( mark, positions, color );
 }
 
+///////////////////// INTERFACE PERSISTENT /////////////////////
+// interface specific methods for store and restore
+///////////////////// INTERFACE PERSISTENT ///////////////////// 
+
+void LFReadCache::restore ( std::istream & is, int format )
+{
+  //delete everything we knew so far...
+  this->clear();
+  
+  
+  if ( is.good() )
+  {
+    
+    std::string tmp;
+    is >> tmp; //class name 
+    
+    if ( ! this->isStartTag( tmp, "LFReadCache" ) )
+    {
+      std::cerr << " WARNING - attempt to restore LFReadCache, 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, "LFReadCache" ) )
+      {
+        b_endOfBlock = true;
+        continue;
+      }      
+      
+      tmp = this->removeStartTag ( tmp );   
+
+      
+      if ( tmp.compare("numFeatures") == 0 )
+      {
+        is >> this->numFeatures;
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }
+      else if ( tmp.compare("lfrep") == 0 )
+      {
+        //TODO generic restore function        
+        this->lfrep->restore ( is );
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }       
+      else if ( tmp.compare("cachedir") == 0 )
+      {
+        is >> this->cachedir;
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }      
+      else if ( tmp.compare("descFormat") == 0 )
+      {
+        is >> this->descFormat;
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }
+      else if ( tmp.compare("cachemode") == 0 )
+      {
+        is >> this->cachemode;
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }   
+      else
+      {
+      std::cerr << "WARNING -- unexpected LFReadCache object -- " << tmp << " -- for restoration... aborting" << std::endl;
+      throw;
+      }
+    }
+  }
+  else
+  {
+    std::cerr << "LFReadCache::restore -- InStream not initialized - restoring not possible!" << std::endl;
+    throw;
+  }
+}
+
+void LFReadCache::store ( std::ostream & os, int format ) const
+{ 
+  if (os.good())
+  {
+    // show starting point
+    os << this->createStartTag( "LFReadCache" ) << std::endl; 
+    
+    os << this->createStartTag( "numFeatures" ) << std::endl;
+    os << this->numFeatures << std::endl;
+    os << this->createEndTag( "numFeatures" ) << std::endl;
+    
+    os << this->createStartTag( "lfrep" ) << std::endl;
+    this->lfrep->store ( os );
+    os << this->createEndTag( "lfrep" ) << std::endl;     
+
+    os << this->createStartTag( "cachedir" ) << std::endl;
+    os << this->cachedir << std::endl;
+    os << this->createEndTag( "cachedir" ) << std::endl;
+    
+    os << this->createStartTag( "descFormat" ) << std::endl;
+    os << this->descFormat << std::endl;
+    os << this->createEndTag( "descFormat" ) << std::endl;
+    
+    os << this->createStartTag( "cachemode" ) << std::endl;
+    os << this->cachemode << std::endl;
+    os << this->createEndTag( "cachemode" ) << std::endl;
+    
+    // done
+    os << this->createEndTag( "LFReadCache" ) << std::endl;    
+  }
+  else
+  {
+    std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
+  }
+}
+
+void LFReadCache::clear ()
+{
+  if ( this->lfrep != NULL )
+  {
+    delete this->lfrep;
+    this->lfrep = NULL;
+  }  
+}
+

+ 89 - 25
features/localfeatures/LFReadCache.h

@@ -19,27 +19,55 @@
 
 namespace OBJREC {
 
-  /** @class LFReadCache
-  * @brief Read local features from file
-  *
-  */   
-  class LFReadCache : public LocalFeatureRepresentation
-  {
-
-      protected:
-      int   numFeatures;
-      const LocalFeatureRepresentation *lfrep;
-      std::string cachedir;
-
-      int descFormat;
-      int cachemode;
-
-      template <class ImageClass>
-      int extractFeaturesTemplate ( const ImageClass & img, 
-            NICE::VVector & features, 
-            NICE::VVector & positions) const;
-
-      public:
+/** @class LFReadCache
+* @brief Read local features from file
+*
+*/   
+class LFReadCache : public LocalFeatureRepresentation
+{
+
+  protected:
+    
+    int   numFeatures;
+    
+    LocalFeatureRepresentation *lfrep;
+    
+    std::string cachedir;
+
+    int descFormat;
+    
+    int cachemode;
+
+    template <class ImageClass>
+    int extractFeaturesTemplate ( const ImageClass & img, 
+          NICE::VVector & features, 
+          NICE::VVector & positions) const;
+          
+    void setDescFormat ( const std::string & _descFormat_s );
+
+  public:
+    
+    ///////////////////// ///////////////////// /////////////////////
+    //                   CONSTRUCTORS / DESTRUCTORS
+    ///////////////////// ///////////////////// /////////////////////    
+    
+    /** 
+     * @brief default constructor
+     * @date 10-02-2014 (dd-mm-yyyy )
+     * @author Alexander Freytag
+     */
+    LFReadCache ( );
+    
+    /** 
+     * @brief standard constructor 
+     * @date 10-02-2014 (dd-mm-yyyy )
+     * @author Alexander Freytag
+     * @param conf Configuration
+     * @param _section specify the block in the config object we read 
+     */
+    LFReadCache( const NICE::Config * _conf, 
+          const std::string & _confSection = "LFReadCache" /*NOTE previous default: "cache"*/          
+              );    
     
     /** standard constructor 
     * @param conf Configuration
@@ -47,14 +75,25 @@ namespace OBJREC {
     * @param _numfeatures how many features should be returned (-1 for all)
     * @param _section specify the block in the config object we read 
     */
-    LFReadCache( const NICE::Config *conf, 
-          const LocalFeatureRepresentation *lfrep,
+    LFReadCache( const NICE::Config * _conf, 
+          LocalFeatureRepresentation *lfrep,
           int _numFeatures = -1,
-          const std::string & _section = "cache"          
-               );
+          const std::string & _confSection = "LFReadCache" /*NOTE previous default: "cache"*/          
+              ); 
         
     /** simple destructor */
     virtual ~LFReadCache();
+        
+    /** 
+     * @brief Jobs previously performed in the config-version of the constructor, read settings etc.
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mm-yyyy )
+     */    
+    void initFromConfig ( const NICE::Config * _conf, const std::string & _confSection = "LFReadCache"); 
+    
+    ///////////////////// ///////////////////// /////////////////////
+    //                      FEATURE STUFF
+    ///////////////////// ///////////////////// //////////////////    
 
     int getDescSize () const;
 
@@ -86,6 +125,31 @@ namespace OBJREC {
     void visualizeFeatures ( NICE::Image & mark,
           const NICE::VVector & positions,
           size_t color ) const;
+          
+    ///////////////////// INTERFACE PERSISTENT /////////////////////
+    // interface specific methods for store and restore
+    ///////////////////// INTERFACE PERSISTENT /////////////////////   
+    
+    /** 
+     * @brief Load object from external file (stream)
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mmyyyy)
+     */     
+    virtual void restore ( std::istream & is, int format = 0 );
+    
+    /** 
+     * @brief Save object to external file (stream)
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mmyyyy)
+     */       
+    virtual void store( std::ostream & os, int format = 0 ) const;
+    
+    /** 
+     * @brief Clear  object
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mmyyyy)
+     */    
+    virtual void clear ();           
   };
 
 } // namespace

+ 193 - 19
features/localfeatures/LFWriteCache.cpp

@@ -1,24 +1,29 @@
 /**
 * @file LFWriteCache.cpp
 * @brief read local features from file
-* @author Erik Rodner
+* @author Erik Rodner, Alexander Freytag
 * @date 02/14/2008
-
 */
 
 #include "core/vector/VectorT.h"
 #include "core/vector/MatrixT.h"
 #include "core/image/ImageT.h"
 
+// STL includes
 #include <iostream>
 #include <fstream>
-
+// 
 #include <sys/stat.h>
 #include <sys/types.h>
 
-#include "vislearning/baselib/Globals.h"
-#include "core/basics/StringTools.h"
+// nice-core includes
+#include <core/basics/StringTools.h>
+
+// nice-vislearning includes
+#include <vislearning/baselib/Globals.h>
 
+// 
+#include "vislearning/features/localfeatures/GenericLFSelection.h"
 #include "vislearning/features/localfeatures/LFWriteCache.h"
 
 using namespace OBJREC;
@@ -26,31 +31,84 @@ using namespace OBJREC;
 using namespace std;
 using namespace NICE;
 
+///////////////////// ///////////////////// /////////////////////
+//                   PROTECTED METHODS
+///////////////////// ///////////////////// ////////////////
 
+void LFWriteCache::setDescFormat ( const std::string & _descFormat_s )
+{
+  if ( _descFormat_s == "binary_double" )
+    this->descFormat = VVector::FILEFORMAT_BINARY_DOUBLE;
+  else if ( _descFormat_s == "binary_uchar" )
+    this->descFormat = VVector::FILEFORMAT_BINARY_CHAR;
+  else if ( _descFormat_s == "text_line" )
+    this->descFormat = VVector::FILEFORMAT_LINE;
+  else if ( ( _descFormat_s == "text_nice" ) || ( _descFormat_s == "text_ice" ) )
+    this->descFormat = VVector::FILEFORMAT_NICE;
+  else
+    fthrow ( Exception, "Format " << _descFormat_s << " is unknown." );  
+}
+
+///////////////////// ///////////////////// /////////////////////
+//                   CONSTRUCTORS / DESTRUCTORS
+///////////////////// ///////////////////// /////////////////
+
+LFWriteCache::LFWriteCache ()
+{
+  this->cachedir           = "";
+  this->cachemode          = Globals::getCacheMode ( "cat" );
+  std::string descFormat_s = "binary_double";
+  this->setDescFormat ( descFormat_s );
+}
+
+LFWriteCache::LFWriteCache ( const Config *_conf,
+                             const std::string & _confSection
+                           )
+{
+  this->initFromConfig( _conf, _confSection );  
+}
 
 LFWriteCache::LFWriteCache ( const Config *conf,
-                             const LocalFeatureRepresentation *_lfrep,
+                             LocalFeatureRepresentation *_lfrep,
                              const std::string & _section ) : lfrep ( _lfrep )
 {
-  cachedir = conf->gS ( _section, "root" );
-  cachemode = Globals::getCacheMode ( conf->gS ( _section, "mode", "cat" ) );
+  this->cachedir = conf->gS ( _section, "root" );
+  this->cachemode = Globals::getCacheMode ( conf->gS ( _section, "mode", "cat" ) );
 
   std::string descFormat_s = conf->gS ( _section, "descriptor_format", "binary_double" );
-  if ( descFormat_s == "binary_double" )
-    descFormat = VVector::FILEFORMAT_BINARY_DOUBLE;
-  else if ( descFormat_s == "binary_uchar" )
-    descFormat = VVector::FILEFORMAT_BINARY_CHAR;
-  else if ( descFormat_s == "text_line" )
-    descFormat = VVector::FILEFORMAT_LINE;
-  else if ( descFormat_s == "text_ice" )
-    descFormat = VVector::FILEFORMAT_NICE;
-
+  this->setDescFormat ( descFormat_s );
 }
 
 LFWriteCache::~LFWriteCache()
 {
+  if ( this->lfrep != NULL )
+  {
+    delete this->lfrep;
+    this->lfrep = NULL;
+  }  
+}
+
+void LFWriteCache::initFromConfig(const NICE::Config* _conf, const std::string& _confSection)
+{
+  //srand(time(NULL));
+  this->cachedir = _conf->gS ( _confSection, "root" );
+  this->cachemode = Globals::getCacheMode ( _conf->gS ( _confSection, "mode", "cat" ) );
+
+  std::string descFormat_s = _conf->gS ( _confSection, "descriptor_format", "binary_double" );
+  this->setDescFormat ( descFormat_s );
+  
+  if ( this->lfrep != NULL )
+  {
+    delete this->lfrep;
+    this->lfrep = NULL;
+  }  
+  this->lfrep = GenericLFSelection::selectLocalFeatureRep (  _conf, _confSection );  
 }
 
+///////////////////// ///////////////////// /////////////////////
+//                      FEATURE STUFF
+///////////////////// ///////////////////// /////////////////
+
 int LFWriteCache::getDescSize () const
 {
   return lfrep->getDescSize();
@@ -116,13 +174,129 @@ int LFWriteCache::extractFeatures ( const NICE::ColorImage & img,
 void LFWriteCache::visualize ( NICE::Image & img,
                                const NICE::Vector & feature ) const
 {
-  lfrep->visualize ( img, feature );
+  this->lfrep->visualize ( img, feature );
 }
 
 void LFWriteCache::visualizeFeatures ( NICE::Image & mark,
                                        const VVector & positions,
                                        size_t color ) const
 {
-  lfrep->visualizeFeatures ( mark, positions, color );
+  this->lfrep->visualizeFeatures ( mark, positions, color );
 }
 
+///////////////////// INTERFACE PERSISTENT /////////////////////
+// interface specific methods for store and restore
+///////////////////// INTERFACE PERSISTENT ///////////////////// 
+
+void LFWriteCache::restore ( std::istream & is, int format )
+{
+  //delete everything we knew so far...
+  this->clear();
+  
+  
+  if ( is.good() )
+  {
+    
+    std::string tmp;
+    is >> tmp; //class name 
+    
+    if ( ! this->isStartTag( tmp, "LFWriteCache" ) )
+    {
+      std::cerr << " WARNING - attempt to restore LFWriteCache, 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, "LFWriteCache" ) )
+      {
+        b_endOfBlock = true;
+        continue;
+      }      
+      
+      tmp = this->removeStartTag ( tmp );   
+
+      
+      if ( tmp.compare("lfrep") == 0 )
+      {
+        //TODO generic restore function        
+        this->lfrep->restore ( is );
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }       
+      else if ( tmp.compare("cachedir") == 0 )
+      {
+        is >> this->cachedir;
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }      
+      else if ( tmp.compare("descFormat") == 0 )
+      {
+        is >> this->descFormat;
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }
+      else if ( tmp.compare("cachemode") == 0 )
+      {
+        is >> this->cachemode;
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }   
+      else
+      {
+      std::cerr << "WARNING -- unexpected LFWriteCache object -- " << tmp << " -- for restoration... aborting" << std::endl;
+      throw;
+      }
+    }
+  }
+  else
+  {
+    std::cerr << "LFWriteCache::restore -- InStream not initialized - restoring not possible!" << std::endl;
+    throw;
+  }
+}
+
+void LFWriteCache::store ( std::ostream & os, int format ) const
+{ 
+  if (os.good())
+  {
+    // show starting point
+    os << this->createStartTag( "LFWriteCache" ) << std::endl; 
+    
+    os << this->createStartTag( "lfrep" ) << std::endl;
+    this->lfrep->store ( os );
+    os << this->createEndTag( "lfrep" ) << std::endl;     
+
+    os << this->createStartTag( "cachedir" ) << std::endl;
+    os << this->cachedir << std::endl;
+    os << this->createEndTag( "cachedir" ) << std::endl;
+    
+    os << this->createStartTag( "descFormat" ) << std::endl;
+    os << this->descFormat << std::endl;
+    os << this->createEndTag( "descFormat" ) << std::endl;
+    
+    os << this->createStartTag( "cachemode" ) << std::endl;
+    os << this->cachemode << std::endl;
+    os << this->createEndTag( "cachemode" ) << std::endl;
+    
+    // done
+    os << this->createEndTag( "LFWriteCache" ) << std::endl;    
+  }
+  else
+  {
+    std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
+  }
+}
+
+void LFWriteCache::clear ()
+{
+  if ( this->lfrep != NULL )
+  {
+    delete this->lfrep;
+    this->lfrep = NULL;
+  }  
+}

+ 114 - 52
features/localfeatures/LFWriteCache.h

@@ -18,64 +18,126 @@
 
 namespace OBJREC {
 
-  /** @class LFWriteCache
-  * @brief Write local features to file (whenever a descriptor is computed, it will be checked whether a corresponding file already exists. If not, we save the descriptor)
-  *
-  */  
-  class LFWriteCache : public LocalFeatureRepresentation
-  {
+/** @class LFWriteCache
+* @brief Write local features to file (whenever a descriptor is computed, it will be checked whether a corresponding file already exists. If not, we save the descriptor)
+*
+*/  
+class LFWriteCache : public LocalFeatureRepresentation
+{
 
-      protected:
-        const LocalFeatureRepresentation *lfrep;
+  protected:
+    
+    LocalFeatureRepresentation *lfrep;
 
-        std::string cachedir;
+    std::string cachedir;
 
-        int descFormat;
-        int cachemode;
+    int descFormat;
+    
+    int cachemode;
+    
+    void setDescFormat ( const std::string & _descFormat_s );    
 
 
-      public:
+  public:
+    
+    ///////////////////// ///////////////////// /////////////////////
+    //                   CONSTRUCTORS / DESTRUCTORS
+    ///////////////////// ///////////////////// /////////////////////
+    
+    /**     
+     * @brief default constructor
+     * @date 10-02-2014 (dd-mm-yyyy )
+     * @author Alexander Freytag
+     */
+    LFWriteCache ( );
+    
+    /** 
+     * @brief standard constructor 
+     * @date 10-02-2014 (dd-mm-yyyy )
+     * @author Alexander Freytag
+     * @param conf Configuration
+     * @param _section specify the block in the config object we read 
+     */
+    LFWriteCache( const NICE::Config * _conf, 
+          const std::string & _confSection = "LFWriteCache" /*NOTE previous default: "cache"*/          
+              );       
+  
+    /** simple constructor */
+    LFWriteCache( const NICE::Config *conf, 
+        LocalFeatureRepresentation *lfrep,
+        const std::string & _section = "cache"
+                ); 
+    
+    /** simple destructor */
+    virtual ~LFWriteCache();
+    
+    /** 
+     * @brief Jobs previously performed in the config-version of the constructor, read settings etc.
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mm-yyyy )
+     */    
+    void initFromConfig ( const NICE::Config * _conf, const std::string & _confSection = "LFWriteCache");     
+    
+    ///////////////////// ///////////////////// /////////////////////
+    //                      FEATURE STUFF
+    ///////////////////// ///////////////////// //////////////////       
+  
+    int getDescSize () const;
+  
+    /**
+    * extract features for gray images
+    * @param img input image
+    * @param features output features
+    * @param positions position of the features
+    * @return 
+    */
+    int extractFeatures ( const NICE::Image & img, 
+          NICE::VVector & features, 
+          NICE::VVector & positions ) const;
+    
+    /**
+    * extract features for color images
+    * @param img input image
+    * @param features output features
+    * @param positions position of the features
+    * @return 
+    */
+    int extractFeatures ( const NICE::ColorImage & img, 
+          NICE::VVector & features, 
+          NICE::VVector & positions ) const;
+  
+    void visualize ( NICE::Image & img, 
+      const NICE::Vector & feature ) const;
+  
+    void visualizeFeatures ( NICE::Image & mark,
+          const NICE::VVector & positions,
+          size_t color ) const;
+              
+    ///////////////////// INTERFACE PERSISTENT /////////////////////
+    // interface specific methods for store and restore
+    ///////////////////// INTERFACE PERSISTENT /////////////////////   
+    
+    /** 
+     * @brief Load object from external file (stream)
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mmyyyy)
+     */     
+    virtual void restore ( std::istream & is, int format = 0 );
+    
+    /** 
+     * @brief Save object to external file (stream)
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mmyyyy)
+     */       
+    virtual void store( std::ostream & os, int format = 0 ) const;
     
-        /** simple constructor */
-        LFWriteCache( const NICE::Config *conf, 
-            const LocalFeatureRepresentation *lfrep,
-            const std::string & _section = "cache"
-                    ); 
-        
-        /** simple destructor */
-        virtual ~LFWriteCache();
-      
-        int getDescSize () const;
-      
-        /**
-        * extract features for gray images
-        * @param img input image
-        * @param features output features
-        * @param positions position of the features
-        * @return 
-        */
-        int extractFeatures ( const NICE::Image & img, 
-              NICE::VVector & features, 
-              NICE::VVector & positions ) const;
-        
-        /**
-        * extract features for color images
-        * @param img input image
-        * @param features output features
-        * @param positions position of the features
-        * @return 
-        */
-        int extractFeatures ( const NICE::ColorImage & img, 
-              NICE::VVector & features, 
-              NICE::VVector & positions ) const;
-      
-        void visualize ( NICE::Image & img, 
-          const NICE::Vector & feature ) const;
-      
-        void visualizeFeatures ( NICE::Image & mark,
-              const NICE::VVector & positions,
-              size_t color ) const;
-  };
+    /** 
+     * @brief Clear  object
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mmyyyy)
+     */    
+    virtual void clear ();               
+};
 
 
 } // namespace

+ 193 - 31
features/localfeatures/LFonHSG.cpp

@@ -1,7 +1,7 @@
 /**
 * @file LFonHSG.cpp
 * @brief Implementation of the LocalFeatureHSG.h. See description there.
-* @author Eric Bach
+* @author Eric Bach, Alexander Freytag
 * @date 26.10.2011
 */
 
@@ -16,40 +16,30 @@ using namespace OBJREC;
 using namespace boost;
 #endif
 
-LFonHSG::LFonHSG (const Config *conf, const string section)
-{
-  /** initialization **/
-  this->lf = NULL;
-
-  /** get parameters for the grid **/
-  this->sampleScaling = conf->gI (section, "sample_scaling", 50);
-  this->scales = conf->gS (section, "scales"        , "1");
-  // the scales are seperated by '+', like in the Van de Sande implementation
-  char separator = '+';
-
-  /** get debuginformation **/
-  this->debug = conf->gB ("debug", "show_log_HSG", false);
-
-  /** generate the descriptor-instanz **/
-  lf = GenericLocalFeatureSelection::selectLocalFeature (conf, section);
+///////////////////// ///////////////////// /////////////////////
+//                   PROTECTED METHODS
+///////////////////// ///////////////////// /////////////////
 
-  /** parse scales string **/
+void LFonHSG::convertScalesStringToScaleList ( )
+{
   if ( this->debug )
   {
     clog << "[log] LocalFeatureHSG::LocalFeatureHSG" << std::endl;
     clog << "[log] try to parse the 'scales-string': " << this->scales << " -> ";
-  }
-
+  }  
+  
+  // the scales are seperated by '+', like in the Van de Sande implementation
+  char separator = '+';
 #ifdef NICE_USELIB_BOOST
   typedef tokenizer<boost::char_separator<char> > tokenizer;
   char_separator<char> sep (separator);
-  tokenizer tokens (scales, sep);		// parse the string into tokens
+  tokenizer tokens (scales, sep);   // parse the string into tokens
   for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter)
   {
     if ( this->debug )
       clog << *tok_iter << " ";
     
-    scalesV.push_back(StringTools::convert<float>(*tok_iter));
+    this->scalesV.push_back(StringTools::convert<float>(*tok_iter));
   }
 #else // standard
   std::vector<std::string> temp;
@@ -59,11 +49,35 @@ LFonHSG::LFonHSG (const Config *conf, const string section)
     if ( this->debug )
       clog << *it << " ";
     
-    scalesV.push_back(StringTools::convert<float>(*it));
+    this->scalesV.push_back(StringTools::convert<float>(*it));
   }
 #endif
   if ( this->debug )
+  {
     clog << std::endl;
+  }
+}
+
+
+///////////////////// ///////////////////// /////////////////////
+//                   CONSTRUCTORS / DESTRUCTORS
+///////////////////// ///////////////////// /////////////////
+
+LFonHSG::LFonHSG () : LocalFeatureRepresentation ()
+{
+  this->debug = false;
+
+  this->sampleScaling = 50;
+  this->scales = "1";
+  /** parse scales string **/
+  this->convertScalesStringToScaleList();
+  
+  this->lf = NULL;  
+}
+
+LFonHSG::LFonHSG ( const NICE::Config * _conf, const std::string _confSection)
+{
+  this->initFromConfig( _conf, _confSection );
 }
 
 LFonHSG::~LFonHSG()
@@ -78,6 +92,26 @@ LFonHSG::~LFonHSG()
   }
 }
 
+void OBJREC::LFonHSG::initFromConfig(const NICE::Config * _conf, const std::string & _confSection)
+{
+  this->debug         = _conf->gB (_confSection, "debug",          false);
+
+  this->sampleScaling = _conf->gI (_confSection, "sample_scaling", 50);
+  this->scales        = _conf->gS (_confSection, "scales"        , "1");
+  
+  this->lf            = NULL;
+  this->lf            = GenericLocalFeatureSelection::selectLocalFeature (_conf, _confSection /*TODO check whether it is useful to hand over the confSection string here*/);
+
+  /** parse scales string **/
+  this->convertScalesStringToScaleList();
+}
+
+
+///////////////////// ///////////////////// /////////////////////
+//                      FEATURE STUFF
+///////////////////// ///////////////////// /////////////////
+
+
 int LFonHSG::getDescSize() const
 {
   return lf->getDescSize();
@@ -146,15 +180,143 @@ void LFonHSG::visualizeFeatures (NICE::Image & mark, const VVector & positions,
 }
 
 
+///////////////////// INTERFACE PERSISTENT /////////////////////
+// interface specific methods for store and restore
+///////////////////// INTERFACE PERSISTENT ///////////////////// 
 
+void LFonHSG::restore ( std::istream & is, int format )
+{
+  //delete everything we knew so far...
+  this->clear();
+  
+  
+  if ( is.good() )
+  {
+    
+    std::string tmp;
+    is >> tmp; //class name 
+    
+    if ( ! this->isStartTag( tmp, "LFonHSG" ) )
+    {
+      std::cerr << " WARNING - attempt to restore LFonHSG, 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, "LFonHSG" ) )
+      {
+        b_endOfBlock = true;
+        continue;
+      }      
+      
+      tmp = this->removeStartTag ( tmp );   
+
+      
+      if ( tmp.compare("debug") == 0 )
+      {
+        is >> this->debug;
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }
+      else if ( tmp.compare("sampleScaling") == 0 )
+      {
+        is >> this->sampleScaling;
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }      
+      else if ( tmp.compare("scales") == 0 )
+      {
+        is >> this->scales;
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }
+      else if ( tmp.compare("scalesV") == 0 )
+      {
+        unsigned int ui_scalesVSize;
+        is >> ui_scalesVSize;
+        this->scalesV.clear();
+        //allocate enough memory
+        this->scalesV.resize( ui_scalesVSize );
+        
+        std::vector< float >::iterator itScalesV = this->scalesV.begin();
+        for ( uint tmpIdx = 0; tmpIdx < ui_scalesVSize; tmpIdx++, itScalesV++ )
+        {
+          is >> *itScalesV;
+        }
+
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      } 
+      else if ( tmp.compare("lf") == 0 )
+      {
+        this->lf->restore ( is );
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }    
+      else
+      {
+      std::cerr << "WARNING -- unexpected LFonHSG object -- " << tmp << " -- for restoration... aborting" << std::endl;
+      throw;
+      }
+    }
+  }
+  else
+  {
+    std::cerr << "LFonHSG::restore -- InStream not initialized - restoring not possible!" << std::endl;
+    throw;
+  }
+}
 
+void LFonHSG::store ( std::ostream & os, int format ) const
+{ 
+  if (os.good())
+  {
+    // show starting point
+    os << this->createStartTag( "LFonHSG" ) << std::endl; 
+    
+    os << this->createStartTag( "debug" ) << std::endl;
+    os << this->debug << std::endl;
+    os << this->createEndTag( "debug" ) << std::endl;  
 
+    os << this->createStartTag( "sampleScaling" ) << std::endl;
+    os << this->sampleScaling << std::endl;
+    os << this->createEndTag( "sampleScaling" ) << std::endl;
+    
+    os << this->createStartTag( "scalesV" ) << std::endl;
+    os << "size: " << this->scalesV.size() << std::endl;
+    for ( std::vector< float >::const_iterator itScalesV = this->scalesV.begin();
+          itScalesV != this->scalesV.end();
+          itScalesV++ 
+         )
+    {
+      os << *itScalesV << " ";
+    }            
+    os << std::endl;
+    os << this->createEndTag( "scalesV" ) << std::endl;  
+
+    os << this->createStartTag( "lf" ) << std::endl;
+    this->lf->store ( os );
+    os << this->createEndTag( "lf" ) << std::endl; 
+    
+    // done
+    os << this->createEndTag( "LFonHSG" ) << std::endl;    
+  }
+  else
+  {
+    std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
+  }
+}
 
-
-
-
-
-
-
-
-
+void LFonHSG::clear ()
+{
+  if ( this->lf != NULL )
+  {
+    delete this->lf;
+    this->lf = NULL;
+  }  
+}

+ 56 - 5
features/localfeatures/LFonHSG.h

@@ -43,6 +43,10 @@ class LFonHSG : public LocalFeatureRepresentation
 {
     /* private member */
   private:
+    
+    //! if true, print some log-output
+    bool debug;
+    
     /* parameter for the grid */
     //! stepwidhts of the grid
     int sampleScaling;
@@ -51,9 +55,6 @@ class LFonHSG : public LocalFeatureRepresentation
     //! vector which save the different scales
     std::vector <float> scalesV;
 
-    //! if true, print some log-output
-    bool debug;
-
     //! the descriptor instance
     OBJREC::LocalFeature* lf;    
     
@@ -67,17 +68,42 @@ class LFonHSG : public LocalFeatureRepresentation
     void getPositionsOnHSG ( const unsigned int imageWidth,
                              const unsigned int imageHeight,
                              NICE::VVector& positions ) const;
+    
+    protected:
+    void convertScalesStringToScaleList ( );
 
 
     /* public member */
   public:
     
+    ///////////////////// ///////////////////// /////////////////////
+    //                   CONSTRUCTORS / DESTRUCTORS
+    ///////////////////// ///////////////////// /////////////////////    
+    
+    /** 
+     * @brief default constructor
+     * @date 10-02-2014 (dd-mm-yyyy )
+     * @author Alexander Freytag
+     */
+    LFonHSG ( );     
+    
     //! simple contructor
-    LFonHSG ( const NICE::Config *conf, const std::string section = "HSG" );
+    LFonHSG ( const NICE::Config * _conf, const std::string _confSection = "LFonHSG" /*NOTE previous default: "HSG"*/);
     
     //! simple desctructor
-    ~LFonHSG();
+    virtual ~LFonHSG();
+    
+    /** 
+     * @brief Jobs previously performed in the config-version of the constructor, read settings etc.
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mm-yyyy )
+     */    
+    void initFromConfig ( const NICE::Config * _conf, const std::string & _confSection = "LFonHSG");     
 
+    ///////////////////// ///////////////////// /////////////////////
+    //                      FEATURE STUFF
+    ///////////////////// ///////////////////// //////////////////           
+    
     //! Returns the descriptorsize
     int getDescSize() const;
 
@@ -89,6 +115,31 @@ class LFonHSG : public LocalFeatureRepresentation
 
     //! Visualisierung
     void visualizeFeatures ( NICE::Image & mark, const NICE::VVector & positions, size_t color ) const;
+    
+    ///////////////////// INTERFACE PERSISTENT /////////////////////
+    // interface specific methods for store and restore
+    ///////////////////// INTERFACE PERSISTENT /////////////////////   
+    
+    /** 
+     * @brief Load object from external file (stream)
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mmyyyy)
+     */     
+    virtual void restore ( std::istream & is, int format = 0 );
+    
+    /** 
+     * @brief Save object to external file (stream)
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mmyyyy)
+     */       
+    virtual void store( std::ostream & os, int format = 0 ) const;
+    
+    /** 
+     * @brief Clear  object
+     * @author Alexander Freytag
+     * @date 10-02-2014 ( dd-mmyyyy)
+     */    
+    virtual void clear ();     
 };
 }
 #endif