Browse Source

speeding up localization result

Erik Rodner 13 years ago
parent
commit
cd5c13306b

+ 1 - 1
cbaselib/ClassNames.cpp

@@ -391,7 +391,7 @@ void ClassNames::getRGBColor ( int classno, int & r, int & g, int & b ) const
 void ClassNames::getClassnoFromColor ( int & classno, int r, int g, int b ) const
 {
   long color = 256 * ( 256 * r + g ) + b;
-  map<long, int>::const_iterator i = tbl_color_classno.find ( color );
+  __gnu_cxx::hash_map<long, int>::const_iterator i = tbl_color_classno.find ( color );
 
   if ( i == tbl_color_classno.end() )
   {

+ 2 - 1
cbaselib/ClassNames.h

@@ -14,6 +14,7 @@
 
 #include <string>
 #include <map>
+#include <ext/hash_map>
 
 #include "core/basics/Config.h"
 #include "core/basics/Persistent.h"
@@ -31,7 +32,7 @@ class ClassNames : public NICE::Persistent
     std::map<std::string, std::string> tbl_text_code;
     std::map<int, std::string> tbl_classno_code;
     std::map<std::string, int> tbl_code_classno;
-    std::map<long, int> tbl_color_classno;
+    __gnu_cxx::hash_map<long, int> tbl_color_classno;
     std::map<int, long> tbl_classno_color;
 
     int maxClassNo;

+ 65 - 24
cbaselib/LocalizationResult.cpp

@@ -16,6 +16,13 @@
 #include "vislearning/cbaselib/LocalizationResult.h"
 #include "core/basics/StringTools.h"
 
+// use this macro to show labeled images
+#undef DEBUG_LOCALIZATIONREAD
+
+#ifdef DEBUG_LOCALIZATIONREAD
+#include <core/imagedisplay/ImageDisplay.h>
+#endif
+
 using namespace OBJREC;
 
 using namespace std;
@@ -141,10 +148,10 @@ LocalizationResult::~LocalizationResult ()
 	delete slr;
     }
 }
-#undef DEBUG_LOCALIZATIONREAD
 LocalizationResult::LocalizationResult ( const ClassNames *_cn, const NICE::Image & img, int classno ) : cn(_cn)
 {
-     const int t = 200; // FIXME
+     // FIXME: just a bad predefined threshold !
+     const int t = 200; 
      NICE::Region reg;
 
 #ifdef DEBUG_LOCALIZATIONREAD
@@ -158,8 +165,6 @@ LocalizationResult::LocalizationResult ( const ClassNames *_cn, const NICE::Imag
      for ( int y = 0 ; y < img.height(); y++ )
 	 for ( int x = 0 ; x < img.width(); x++ )
 	 {
-	     // refactor-nice.pl: check this substitution
-	     // old: if ( GetVal(img, x, y) < t )
 	     if ( img.getPixel(x,y) < t )
 	     {
 #ifdef DEBUG_LOCALIZATIONREAD
@@ -191,33 +196,69 @@ LocalizationResult::LocalizationResult ( const ClassNames *_cn, const NICE::Colo
      NICE::Image imgo (xsize,ysize);
      imgo.set(0);
 #endif
-     
-     for ( int y = 0 ; y < ysize ; y++ )
-		 for ( int x = 0 ; x < xsize ; x++ )
-		 {
-			 int r = img.getPixel(x,y,0);
-			 int g = img.getPixel(x,y,1);
-			 int b = img.getPixel(x,y,2);
 
-			 int classno;
-
-			 _cn->getClassnoFromColor ( classno, r, g, b );
-
-			 if ( classno >= 0 )
-			regions[classno].add(x,y);
-			#ifdef DEBUG_LOCALIZATIONREAD
-			imgo.setPixel(x,y,classno);
-			#endif
-		 }
+    
+     for ( int y = 0 ; y < ysize ; y++ )
+     {
+       int xstart = 0;   
+       // RGB values of the current pixel
+       int r = img.getPixel(0,y,0);
+       int g = img.getPixel(0,y,1);
+       int b = img.getPixel(0,y,2);
+
+       for ( int x = 0 ; x < xsize ; x++ )
+       {
+         int r_next, g_next, b_next;
+         if ( x != xsize - 1 ) {
+           r_next = img.getPixel(x,y,0);
+           g_next = img.getPixel(x,y,1);
+           b_next = img.getPixel(x,y,2);
+         } else {
+           // at the border of the image, we should
+           // always have a color change to add the last
+           // line segment
+           r_next = -1;
+           g_next = -1;
+           b_next = -1;
+         }
+
+         // now the RGB color changes and we have an object boundary
+         // therefore we have to add a line segment
+         if ( r != r_next || g != g_next || b != b_next )
+         {
+           int classno;
+           // look up class number for the label color
+           _cn->getClassnoFromColor ( classno, r, g, b );
+           
+           if ( classno >= 0 ) {
+             // add line segment as an rectangular region
+             regions[classno].add( xstart, y, x, y );
+             
+        #ifdef DEBUG_LOCALIZATIONREAD
+             for ( int z = xstart ; z <= x ; z++ )
+               imgo.setPixel(z,y,classno);
+        #endif
+             xstart = x+1;
+           }
+         }
+
+        r = r_next;
+        g = g_next;
+        b = b_next;
+       }
+     }
 
+     #ifdef DEBUG_LOCALIZATIONREAD
+     showImageOverlay(imgo, imgo);
+     #endif 
 
      for ( map<int, NICE::Region>::const_iterator j  = regions.begin();
 					    j != regions.end();
 					    j++ )
      {
-	 int classno = j->first;
-	 ClassificationResult *r = new ClassificationResult (classno, 1.0, _cn->getMaxClassno());
-	 push_back ( new SingleLocalizationResult ( r, j->second ) );
+    	 int classno = j->first;
+    	 ClassificationResult *r = new ClassificationResult (classno, 1.0, _cn->getMaxClassno());
+    	 push_back ( new SingleLocalizationResult ( r, j->second ) );
      }
 
      hasLabeledImage = false;

+ 4 - 0
cbaselib/LocalizationResult.h

@@ -71,6 +71,10 @@ class LocalizationResult : public std::vector<SingleLocalizationResult *>, publi
 	NICE::Image *labeledImage;
 	
     public:
+
+  typedef std::vector<SingleLocalizationResult *>::iterator iterator;
+  typedef std::vector<SingleLocalizationResult *>::const_iterator const_iterator;
+
 	bool hasLabeledImage;
 	int xsize;
 	int ysize;

+ 4 - 1
cbaselib/MultiDataset.h

@@ -46,8 +46,11 @@ class MultiDataset
 	/** simple destructor */
 	virtual ~MultiDataset();
     
+  /** access class information, e.g., md.getClassNames("train") */
 	const ClassNames & getClassNames ( const std::string & key ) const;
-	
+
+  /** access stored dataset by name, e.g., md["train"], if you have a [train] section
+    * in your config file */
 	const LabeledSet * operator[] ( const std::string & key ) const;
 
 	const LabeledSet * at ( const std::string & key ) const;