瀏覽代碼

more efficient code

Sven Sickert 10 年之前
父節點
當前提交
ea486498f3
共有 2 個文件被更改,包括 22 次插入44 次删除
  1. 21 44
      features/fpfeatures/ConvolutionFeature.cpp
  2. 1 0
      features/fpfeatures/ConvolutionFeature.h

+ 21 - 44
features/fpfeatures/ConvolutionFeature.cpp

@@ -60,6 +60,7 @@ ConvolutionFeature::ConvolutionFeature ( const ConvolutionFeature *confFeat )
     window_size_y = confFeat->window_size_y;
     betaLength = confFeat->betaLength;
     isColor = confFeat->isColor;
+    numChannels = confFeat->numChannels;
     beta = new NICE::Vector( betaLength, 0.0 );
 
     int i = 0;
@@ -83,9 +84,11 @@ void ConvolutionFeature::initializeParameterVector()
     if (window_size_x > 0 && window_size_y > 0)
     {
         if (isColor)
-            betaLength = 3*window_size_x*window_size_y + 1;
+            numChannels = 3;
         else
-            betaLength = window_size_x*window_size_y + 1;
+            numChannels = 1;
+
+        betaLength = numChannels*window_size_x*window_size_y + 1;
 
         beta = new NICE::Vector( betaLength, (1.0/(double)(betaLength-1) ) );
         beta[0] = 1;
@@ -107,12 +110,8 @@ NICE::Vector ConvolutionFeature::getFeatureVector( const Example *example ) cons
 {
     NICE::Vector vec(betaLength, 1.0);
 
-    NICE::MultiChannelImageT<int> * img = NULL;
     NICE::MultiChannelImageT<double> * imgD = NULL;
-    if (isColor)
-        imgD = & example->ce->getDChannel( CachedExample::D_EOH );
-    else
-        img = & example->ce->getIChannel( CachedExample::I_GRAYVALUES );
+    imgD = & example->ce->getDChannel( CachedExample::D_EOH );
 
     int xsize, ysize, x, y;
 
@@ -120,9 +119,9 @@ NICE::Vector ConvolutionFeature::getFeatureVector( const Example *example ) cons
     x = example->x;
     y = example->y;
 
-    int halfwsx = std::floor ( window_size_x / 2 );
-    int halfwsy = std::floor ( window_size_y / 2 );
-    int step = window_size_x*window_size_y;
+    const int halfwsx = std::floor ( window_size_x / 2 );
+    const int halfwsy = std::floor ( window_size_y / 2 );
+    const int step = window_size_x*window_size_y;
 
     int k = 1;
     for ( int v = -halfwsy; v <= halfwsy; v++ )
@@ -139,17 +138,8 @@ NICE::Vector ConvolutionFeature::getFeatureVector( const Example *example ) cons
                  && y+vv < ysize
                  && k < vec.size() )
             {
-                if (isColor)
-                {
-                    // using HSV color space
-                    vec[k]           = imgD->get(x+uu,y+vv,0);
-                    vec[k+step]      = imgD->get(x+uu,y+vv,1);
-                    vec[k+step+step] = imgD->get(x+uu,y+vv,2);
-                }
-                else
-                {
-                    vec[k] = (double)img->get(x+uu,y+vv);
-                }
+                for ( int c = 0; c < numChannels; c++)
+                    vec[k+c*step] = imgD->get(x+uu,y+vv,c);
             }
         }
 
@@ -196,12 +186,8 @@ double ConvolutionFeature::val ( const Example *example ) const
         return val1;
     }
 
-    NICE::MultiChannelImageT<int> * img = NULL;
     NICE::MultiChannelImageT<double> * imgD = NULL;
-    if (isColor)
-        imgD = & example->ce->getDChannel( CachedExample::D_EOH );
-    else
-        img = & example->ce->getIChannel( CachedExample::I_GRAYVALUES );
+    imgD = & example->ce->getDChannel( CachedExample::D_EOH );
 
     int xsize, ysize, x, y;
 
@@ -209,9 +195,9 @@ double ConvolutionFeature::val ( const Example *example ) const
     x = example->x;
     y = example->y;
 
-    int halfwsx = std::floor ( window_size_x / 2 );
-    int halfwsy = std::floor ( window_size_y / 2 );
-    int step = window_size_x*window_size_y;
+    const int halfwsx = std::floor ( window_size_x / 2 );
+    const int halfwsy = std::floor ( window_size_y / 2 );
+    const int step = window_size_x*window_size_y;
 
     int k = 1;
     for ( int v = -halfwsy; v <= halfwsy; v++ )
@@ -227,21 +213,8 @@ double ConvolutionFeature::val ( const Example *example ) const
                  && y+vv < ysize
                  && k < beta->size() )
             {
-                if (isColor)
-                {
-                    // using HSV color space
-                    double h = imgD->get(x+uu,y+vv,0);
-                    double s = imgD->get(x+uu,y+vv,1);
-                    double v = imgD->get(x+uu,y+vv,2);
-
-                    val1 += h * beta->operator [](k);
-                    val1 += s * beta->operator [](k+step);
-                    val1 += v * beta->operator [](k+step+step);
-                }
-                else
-                {
-                    val1 += (double)img->get(x+uu,y+vv) * beta->operator [](k);
-                }
+                for ( int c = 0; c < numChannels; c++ )
+                    val1 += imgD->get(x+uu,y+vv,c) * beta->operator [](k+c*step);
 
             }
         }
@@ -281,8 +254,12 @@ void ConvolutionFeature::restore ( std::istream & is, int format )
     is >> betaLength;
 
     isColor = false;
+    numChannels = 1;
     if ( betaLength > (window_size_x*window_size_y+1) )
+    {
         isColor = true;
+        numChannels = 3;
+    }
 
     beta = new NICE::Vector( betaLength, 1.0 );
     for ( NICE::Vector::iterator it = beta->begin();

+ 1 - 0
features/fpfeatures/ConvolutionFeature.h

@@ -33,6 +33,7 @@ class ConvolutionFeature : public Feature
     int window_size_x;
     int window_size_y;
     int betaLength;
+    int numChannels;
     bool isColor;
 
     NICE::Vector *beta;