Sfoglia il codice sorgente

Merge branch 'master' of /home/dbv/git/nice/vislearning

Bjoern Froehlich 13 anni fa
parent
commit
3e46f602b6

+ 72 - 76
baselib/ProgressBarQt.cpp

@@ -54,36 +54,32 @@ ProgressBarQt::~ProgressBarQt()
 {
 }
 
-void ProgressBarQt::timediff2str ( char *text, long time )
+void ProgressBarQt::timediff2str(char *text, double dtime)
 {
-  int seconds;
-  int minutes;
-  int hours;
-  int milliseconds;
-
-  milliseconds = time % 100;
-  time /= 100;
-  seconds = time % 60;
-  time /= 60;
-  minutes = time % 60;
-  time /= 60;
-  hours = time;
-
-  if ( hours != 0 )
-  {
-    sprintf ( text, "%dh %dm %d.%d s", hours, minutes, seconds, milliseconds );
-  }
-  else if ( minutes != 0 )
-  {
-    sprintf ( text, "%dm %d.%d s", minutes, seconds, milliseconds );
-  }
-  else
-  {
-    sprintf ( text, "%d.%d s", seconds, milliseconds );
-  }
+  int time = int (dtime / 60);
+	double seconds = dtime - time*60;
+	int minutes;
+	int hours;
+
+	minutes = time % 60;
+	time /= 60;
+	hours = time;
+
+	if (hours != 0)
+	{
+		sprintf(text, "%dh %dm %5.2fs", hours, minutes, seconds);
+	}
+	else if (minutes != 0)
+	{
+		sprintf(text, "%dm %5.2fs", minutes, seconds);
+	}
+	else
+	{
+		sprintf(text, "%fs", seconds);
+	}
 }
 
-void ProgressBarQt::displayTimeStat ( int posy, char *text, long time )
+void ProgressBarQt::displayTimeStat(int posy, char *text, double time)
 {
   // Text (char *str,int x0,int y0,int val,int exp,Image img);
   char disptext[200];
@@ -100,7 +96,8 @@ double ProgressBarQt::getCurrentTime()
 
   gettimeofday ( &curtime, NULL );
 
-  return curtime.tv_sec * 100.0 + curtime.tv_usec / 10000.0;
+	//return curtime.tv_sec * 100.0 + curtime.tv_usec / 10000.0;
+	return curtime.tv_sec + curtime.tv_usec * 1e-6;
 }
 
 void ProgressBarQt::show()
@@ -122,15 +119,14 @@ void ProgressBarQt::show()
 void ProgressBarQt::hide()
 {
 
-  if ( display_on )
-  {
-    //  Show ( OFF, display, name );
-    display_on = false;
-  }
-  if ( graphicIsAvailable() )
-  {
-    dialogwindow->hide();
-  }
+	if (display_on)
+	{
+		display_on = false;
+	}
+	if (graphicIsAvailable())
+	{
+		dialogwindow->hide();
+	}
 }
 
 void ProgressBarQt::stop()
@@ -150,45 +146,45 @@ void ProgressBarQt::reset ( const std::string & _name )
 
 void ProgressBarQt::update ( int count )
 {
-  step++;
-
-  double progress = step / ( double ) count;
-
-  elapsed_time = getCurrentTime() - start_time;
-  avg_time_step = elapsed_time / step;
-  estimated_time = ( count - step ) * avg_time_step;
-
-  size_t mod;
-  if ( avg_time_step > 50.0 )
-    mod = 1;
-  else
-    mod = ( size_t ) ( 50.0 / avg_time_step ) + 1;
-
-  if ( ( mod <= 1 ) || ( step % mod == 0 ) )
-  {
-    char percent_text[10];
-    sprintf ( percent_text, "%4.2f %%", step * 100.0 / count );
-
-    displayTimeStat ( 0, "Elapsed Time : ", ( long int ) elapsed_time );
-    displayTimeStat ( 1, "Estimated Time : ", ( long int ) estimated_time );
-    displayTimeStat ( 2, "Avg. Time per step : ", ( long int ) avg_time_step );
-
-    char eltime[200];
-    char estime[200];
-    char avgtime[200];
-    timediff2str ( eltime, ( long int ) elapsed_time );
-    timediff2str ( estime, ( long int ) estimated_time );
-    timediff2str ( avgtime, ( long int ) avg_time_step );
-    fprintf (
-      stderr,
-      "[PROGRESS] %s %s (elapsed time %s, estimated time %s, avg %s), \n",
-      name.c_str(), percent_text, eltime, estime, avgtime );
-    //set progress value.
-    if ( graphicIsAvailable() )
-    {
-      progressdialog->setValue ( progress * 100 );
-    }
-  }
+	step++;
+
+	double progress = step / (double) count;
+
+	elapsed_time = getCurrentTime() - start_time;
+	avg_time_step = elapsed_time / step;
+	estimated_time = (count - step) * avg_time_step;
+
+	size_t mod;
+	if (avg_time_step > 1.0)
+		mod = 1;
+	else
+		mod = (size_t) (1.0 / avg_time_step) + 1;
+
+	if ((mod <= 1) || (step % mod == 0))
+	{
+		char percent_text[10];
+		sprintf(percent_text, "%4.2f %%", step * 100.0 / count);
+
+		displayTimeStat(0, "Elapsed Time : ", elapsed_time);
+		displayTimeStat(1, "Estimated Time : ", estimated_time);
+		displayTimeStat(2, "Avg. Time per step : ", avg_time_step);
+
+		char eltime[200];
+		char estime[200];
+		char avgtime[200];
+		timediff2str(eltime, elapsed_time);
+		timediff2str(estime, estimated_time);
+		timediff2str(avgtime, avg_time_step);
+		fprintf(
+				stderr,
+				"[PROGRESS] %s %s (elapsed time %s, estimated time %s, avg %s), \n",
+				name.c_str(), percent_text, eltime, estime, avgtime);
+		//set progress value.
+		if (graphicIsAvailable())
+		{
+			progressdialog->setValue(progress * 100);
+		}
+	}
 }
 
 bool ProgressBarQt::graphicIsAvailable()

+ 4 - 4
baselib/ProgressBarQt.h

@@ -49,10 +49,10 @@ class ProgressBarQt
 
     bool useGraphics;
 
-    void timediff2str ( char *text, long time );
-    void displayTimeStat ( int posy, char *text, long time );
-    double getCurrentTime();
-    bool graphicIsAvailable();
+	void timediff2str(char *text, double time);
+	void displayTimeStat(int posy, char *text, double time);
+	double getCurrentTime();
+	bool graphicIsAvailable();
 
   public:
 

+ 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() )
   {

+ 5 - 1
cbaselib/ClassNames.h

@@ -15,6 +15,10 @@
 #include <string>
 #include <map>
 
+// in future we should replace this with 
+// unordered_map, but it is still experimental but standard c++
+#include <ext/hash_map>
+
 #include "core/basics/Config.h"
 #include "core/basics/Persistent.h"
 
@@ -31,7 +35,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;