Преглед на файлове

GenericLocalFeatureSelection - allows for generic restoring, added Test Unit, added separate TestUnit for persistent LFonHSG

Alexander Freytag преди 11 години
родител
ревизия
3d0b9c4926

+ 65 - 8
features/localfeatures/GenericLocalFeatureSelection.h

@@ -38,7 +38,7 @@ class GenericLocalFeatureSelection
     * @param[in] string  - This string defines the value for "section" in the configfile.
     * @return LocalFeature* - The LocalFeature which contains the selected LocalFeature-Type.
     */
-    static LocalFeature *selectLocalFeature ( const NICE::Config *conf, std::string section = "Features" )
+    static OBJREC::LocalFeature *selectLocalFeature ( const NICE::Config *conf, std::string section = "Features" )
     {
       // return Value
       LocalFeature *lf = NULL;
@@ -48,23 +48,23 @@ class GenericLocalFeatureSelection
       
       // The prefix NICE_* indicates that this implementation comes from a NICE-Developer.
       // Extern implementations can be added without NICE_* in this selection-class.
-      if ( localfeature_type == "NICE_SIFT" )
+      if ( ( localfeature_type == "NICE_SIFT" ) || ( localfeature_type == "LocalFeatureSift" ) )
       {
         lf = new OBJREC::LocalFeatureSift ( conf );
       }
-      else if ( localfeature_type == "NICE_RGBSIFT" )
+      else if ( ( localfeature_type == "NICE_RGBSIFT" ) || ( localfeature_type == "LocalFeatureRGBSift" ) )
       {
         lf = new OBJREC::LocalFeatureRGBSift ( conf );
       }
-      else if ( localfeature_type == "NICE_OPPSIFT" )
+      else if ( ( localfeature_type == "NICE_OPPSIFT" ) || ( localfeature_type == "LocalFeatureOpponnentSift" ) )
       {
         lf = new OBJREC::LocalFeatureOpponnentSift ( conf );
       }
-      else if ( localfeature_type == "colornames" )
+      else if ( ( localfeature_type == "colornames" ) || ( localfeature_type == "LocalFeatureColorWeijer" ) )
       {
         lf = new OBJREC::LocalFeatureColorWeijer ( conf );
       }     
-      else if ( localfeature_type == "centrist" )
+      else if ( ( localfeature_type == "centrist" ) || ( localfeature_type == "LocalFeatureCentrist" ) )
       {
         lf = new OBJREC::LocalFeatureCentrist ( conf );
       }      
@@ -72,10 +72,67 @@ class GenericLocalFeatureSelection
 
       // no correct localfeature_type was given
       if ( lf == NULL )
-        fthrow ( NICE::Exception, "Local feature type not found: " << localfeature_type );
+        fthrow ( NICE::Exception, "Local feature type not found: " << localfeature_type << " for section " << section);
 
       return lf;
-    }
+    };
+    
+      static
+      void restoreLocalFeature ( OBJREC::LocalFeature * _lf, std::istream & is, int format = 0 )
+      {
+                
+        if ( is.good() )
+        {
+          if ( _lf != NULL )
+            delete _lf;
+          
+          
+          std::string className;
+          is >> className; //class name
+                     
+                    
+          if ( className == "<LocalFeatureSift>" )
+          {
+            _lf = new OBJREC::LocalFeatureSift();
+          }
+          else if ( className == "<LocalFeatureRGBSift>" )
+          {
+            _lf = new OBJREC::LocalFeatureRGBSift();            
+          }
+          else if ( className == "<LocalFeatureOpponnentSift>" )
+          {
+            _lf = new OBJREC::LocalFeatureOpponnentSift();
+          }
+          else if ( className == "<LocalFeatureColorWeijer>" )
+          {
+            _lf = new OBJREC::LocalFeatureColorWeijer();
+          }
+          else if ( className == "<LocalFeatureCentrist>" )
+          {
+            _lf = new OBJREC::LocalFeatureCentrist();
+          } 
+          else
+          {
+            fthrow ( NICE::Exception, "GenericLocalFeatureSelection::restoreLocalFeatureRep -- class name " << className << "unknown. Aborting." );
+          }
+          
+          //undo reading of class name
+          
+          for ( uint i = 0; i < className.size(); i++)
+          {
+            is.unget();
+          }
+          
+          //now, call the restore method of the underlying object
+          //NOTE this could be also done externally, leaving only the actual instantiation of the derived objects here
+          _lf->restore ( is );
+            
+        }
+        else
+        {
+          fthrow ( NICE::Exception, "GenericLocalFeatureSelection::restoreLocalFeatureRep -- InStream not initialized - restoring not possible!" );
+        }      
+    };    
 };
 }
 

+ 84 - 0
features/localfeatures/tests/TestGenericLocalFeatureSelectionPersistent.cpp

@@ -0,0 +1,84 @@
+/** 
+ * @file TestGenericLocalFeatureSelectionPersistent.cpp
+ * @brief CppUnit-Testcase to create a LocalFeatureRepresentation object via GenericLocalFeatureSelection, to store it, and to restore it again.
+ * @author Alexander Freytag
+ * @date 12-02-2014 ( dd-mm-yyyy )
+*/
+
+#ifdef NICE_USELIB_CPPUNIT
+
+// STL includes
+
+// NICE-core includes
+
+// gp-hik-core includes
+
+#include "TestGenericLocalFeatureSelectionPersistent.h"
+
+using namespace std; //C basics
+using namespace NICE;  // nice-core
+
+const bool verboseStartEnd = true;
+const bool verbose = true;
+
+
+CPPUNIT_TEST_SUITE_REGISTRATION( TestGenericLocalFeatureSelectionPersistent );
+
+void TestGenericLocalFeatureSelectionPersistent::setUp() {
+}
+
+void TestGenericLocalFeatureSelectionPersistent::tearDown() {
+}
+void TestGenericLocalFeatureSelectionPersistent::testPersistentMethods()
+{
+  
+  if (verboseStartEnd)
+    std::cerr << "================== TestGenericLocalFeatureSelectionPersistent::testPersistentMethods ===================== " << std::endl;  
+  
+  NICE::Config conf;
+  conf.sS( "GenericLocalFeatureSelection", "localfeature_type", "LocalFeatureSift" );
+  OBJREC::LocalFeature  * lf;  
+  
+  lf = OBJREC::GenericLocalFeatureSelection::selectLocalFeature ( &conf, "GenericLocalFeatureSelection");
+  
+  // TEST STORING ABILITIES
+  if ( verbose )
+    std::cerr << " TEST STORING ABILITIES FOR STANDARD LOCALFEATURE" << std::endl;
+  
+  std::string s_destination_save ( "myLocalFeatureSelection.txt" );
+  
+  std::filebuf fbOut;
+  fbOut.open ( s_destination_save.c_str(), ios::out );
+  std::ostream os (&fbOut);
+  //
+  lf->store( os );
+  //   
+  fbOut.close();
+  
+  // TEST RESTORING ABILITIES
+  if ( verbose )
+    std::cerr << " TEST RESTORING ABILITIES FOR STANDARD LOCALFEATURE" << std::endl;
+    
+  OBJREC::LocalFeature * lfRestore = NULL;  
+      
+  std::string s_destination_load ( "myLocalFeatureSelection.txt" );
+  
+  std::filebuf fbIn;
+  fbIn.open ( s_destination_load.c_str(), ios::in );
+  std::istream is (&fbIn);
+  //
+  OBJREC::GenericLocalFeatureSelection::restoreLocalFeature ( lfRestore, is );
+  //   
+  fbIn.close(); 
+  
+  // currently, we have no possibility to actually verify that the restore-operation was successfull, i.e.,
+  // it returned an object identical to the stored one.
+  // However, if we reached this point, at least something went right, so we should be happy...
+  
+    
+  if (verboseStartEnd)
+    std::cerr << "================== TestGenericLocalFeatureSelectionPersistent::testPersistentMethods done ===================== " << std::endl;  
+  
+}
+
+#endif

+ 31 - 0
features/localfeatures/tests/TestGenericLocalFeatureSelectionPersistent.h

@@ -0,0 +1,31 @@
+#ifndef _TESTGENERICLOCALFEATURESELECTIONPERSISTENT_H
+#define _TESTGENERICLOCALFEATURESELECTIONPERSISTENT_H
+
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <vislearning/features/localfeatures/GenericLocalFeatureSelection.h>
+
+/**
+ * CppUnit-Testcase. 
+ * @brief CppUnit-Testcase to create a LocalFeatureRepresentation object via GenericLocalFeatureSelection, to store it, and to restore it again.
+ * @author Alexander Freytag
+ * @date 12-02-2014 ( dd-mm-yyyy )
+ */
+class TestGenericLocalFeatureSelectionPersistent : public CppUnit::TestFixture {
+
+    CPPUNIT_TEST_SUITE( TestGenericLocalFeatureSelectionPersistent );
+	 CPPUNIT_TEST(testPersistentMethods);
+      
+    CPPUNIT_TEST_SUITE_END();
+  
+ private:
+ 
+ public:
+    void setUp();
+    void tearDown();
+
+
+    void testPersistentMethods();
+};
+
+#endif // _TESTGENERICLOCALFEATURESELECTIONPERSISTENT_H

+ 86 - 0
features/localfeatures/tests/TestLFonHSGPersistent.cpp

@@ -0,0 +1,86 @@
+/** 
+ * @file TestLFonHSGPersistent.cpp
+ * @brief CppUnit-Testcase to create a LFonHSG-Wrapper around an LocalFeature-object. Checks whether this is storable and restorable again.
+ * @author Alexander Freytag
+ * @date 12-02-2014 ( dd-mm-yyyy )
+*/
+
+#ifdef NICE_USELIB_CPPUNIT
+
+// STL includes
+
+// NICE-core includes
+
+// gp-hik-core includes
+
+#include "TestLFonHSGPersistent.h"
+
+using namespace std; //C basics
+using namespace NICE;  // nice-core
+
+const bool verboseStartEnd = true;
+const bool verbose = true;
+
+
+CPPUNIT_TEST_SUITE_REGISTRATION( TestLFonHSGPersistent );
+
+void TestLFonHSGPersistent::setUp() {
+}
+
+void TestLFonHSGPersistent::tearDown() {
+}
+
+
+void TestLFonHSGPersistent::testPersistentMethodsForLFonHSG()
+{
+  
+  if (verboseStartEnd)
+    std::cerr << "================== TestLFonHSGPersistent::testPersistentMethodsForLFonHSG ===================== " << std::endl;  
+  
+  NICE::Config conf;
+  conf.sS( "GenericLFSelection", "localfeature_type", "LFonHSG" );
+  conf.sS( "LFonHSG", "localfeature_type", "LocalFeatureSift" );
+  OBJREC::LocalFeatureRepresentation  * lfrep;  
+  
+  lfrep = OBJREC::GenericLFSelection::selectLocalFeatureRep ( &conf, "GenericLFSelection");
+  
+  // TEST STORING ABILITIES
+  if ( verbose )
+    std::cerr << " TEST STORING ABILITIES FOR LFonHSG Wrapper" << std::endl;
+  
+  std::string s_destination_save ( "myLFonHSG.txt" );
+  
+  std::filebuf fbOut;
+  fbOut.open ( s_destination_save.c_str(), ios::out );
+  std::ostream os (&fbOut);
+  //
+  lfrep->store( os );
+  //   
+  fbOut.close();
+  
+  // TEST RESTORING ABILITIES
+  if ( verbose )
+    std::cerr << " TEST RESTORING ABILITIES FOR LFonHSG Wrapper" << std::endl;
+    
+  OBJREC::LocalFeatureRepresentation * lfrepRestore = NULL;  
+      
+  std::string s_destination_load ( "myLFonHSG.txt" );
+  
+  std::filebuf fbIn;
+  fbIn.open ( s_destination_load.c_str(), ios::in );
+  std::istream is (&fbIn);
+  //
+  OBJREC::GenericLFSelection::restoreLocalFeatureRep ( lfrepRestore, is );
+  //   
+  fbIn.close(); 
+  
+  // currently, we have no possibility to actually verify that the restore-operation was successfull, i.e.,
+  // it returned an object identical to the stored one.
+  // However, if we reached this point, at least something went right, so we should be happy...
+  
+    
+  if (verboseStartEnd)
+    std::cerr << "================== TestLFonHSGPersistent::testPersistentMethodsForLFonHSG done ===================== " << std::endl;  
+  
+}
+#endif

+ 32 - 0
features/localfeatures/tests/TestLFonHSGPersistent.h

@@ -0,0 +1,32 @@
+#ifndef _TESTLFONHSGPERSISTENT_H
+#define _TESTLFONHSGPERSISTENT_H
+
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <vislearning/features/localfeatures/LFonHSG.h>
+#include <vislearning/features/localfeatures/GenericLFSelection.h>
+
+/**
+ * CppUnit-Testcase. 
+ * @brief CppUnit-Testcase to create a LFonHSG-Wrapper around an LocalFeature-object. Checks whether this is storable and restorable again.
+ * @author Alexander Freytag
+ * @date 12-02-2014 ( dd-mm-yyyy )
+ */
+class TestLFonHSGPersistent : public CppUnit::TestFixture {
+
+    CPPUNIT_TEST_SUITE( TestLFonHSGPersistent );
+         CPPUNIT_TEST(testPersistentMethodsForLFonHSG);
+      
+    CPPUNIT_TEST_SUITE_END();
+  
+ private:
+ 
+ public:
+    void setUp();
+    void tearDown();
+
+
+    void testPersistentMethodsForLFonHSG();
+};
+
+#endif // _TESTLFONHSGPERSISTENT_H