Browse Source

selectable color mode

Sven Sickert 10 năm trước cách đây
mục cha
commit
788ae2cd8d
2 tập tin đã thay đổi với 40 bổ sung11 xóa
  1. 31 10
      semseg/SemSegConvolutionalTree.cpp
  2. 9 1
      semseg/SemSegConvolutionalTree.h

+ 31 - 10
semseg/SemSegConvolutionalTree.cpp

@@ -28,6 +28,7 @@ SemSegConvolutionalTree::SemSegConvolutionalTree () : SemanticSegmentation ()
 
     saveLoadData = false;
     fileLocation = "classifier.data";
+    colorMode = 0;
 
     fpc = new FPCRandomForests ();
 }
@@ -50,7 +51,7 @@ SemSegConvolutionalTree::~SemSegConvolutionalTree ()
 
 //#################### MEMBER FUNCTIONS #######################//
 
-void SemSegConvolutionalTree::convertRGBToHSV (
+void SemSegConvolutionalTree::preprocessChannels (
         CachedExample *ce,
         bool isColor ) const
 {
@@ -69,22 +70,41 @@ void SemSegConvolutionalTree::convertRGBToHSV (
         for ( int y = 0; y < img->height(); y++ )
             for ( int x = 0; x < img->width(); x++ )
             {
-                double h,s,v;
                 double r = (double)img->get( x, y, 0);
                 double g = (double)img->get( x, y, 1);
                 double b = (double)img->get( x, y, 2);
 
-                ColorConversion::ccRGBtoHSV(r, g, b, &h, &s, &v);
-
-                imgD->set( x, y, h, 0);
-                imgD->set( x, y, s, 1);
-                imgD->set( x, y, v, 2);
+                if ( colorMode == 1 )
+                {
+                    double h,s,v;
+                    ColorConversion::ccRGBtoHSV(r, g, b, &h, &s, &v);
+                    imgD->set( x, y, h, 0);
+                    imgD->set( x, y, s, 1);
+                    imgD->set( x, y, v, 2);
+                }
+                else if ( colorMode == 2 )
+                {
+                    double cX, cY, cZ, cL, ca, cb;
+                    r /= 255.0;
+                    g /= 255.0;
+                    b /= 255.0;
+                    ColorConversion::ccRGBtoXYZ( r, g, b, &cX, &cY, &cZ, 0 );
+                    ColorConversion::ccXYZtoCIE_Lab( cX, cY, cZ, &cL, &ca, &cb, 0 );
+                    imgD->set( x, y, cL, 0);
+                    imgD->set( x, y, ca, 1);
+                    imgD->set( x, y, cb, 2);
+                }
+                else
+                {
+                    imgD->set( x, y, r/255.0, 0 );
+                    imgD->set( x, y, g/255.0, 1 );
+                    imgD->set( x, y, b/255.0, 2 );
+                }
             }
 
         // remove integer channels
         img->freeData();
     }
-    // FIXME: never true because of CachedExample implementation of getXChannel
     else
     {
         img = & ce->getIChannel( CachedExample::I_GRAYVALUES );
@@ -114,6 +134,7 @@ void SemSegConvolutionalTree::initFromConfig( const Config *_conf,
 {
     conf = _conf;
     saveLoadData = conf->gB ( s_confSection, "save_load_data", false );
+    colorMode = conf->gI ( s_confSection, "color_mode", 0 );
     fileLocation = conf->gS ( s_confSection, "datafile", "classifier.data" );
 
     fpc = new FPCRandomForests ( _conf, "FPCRandomForests" );
@@ -151,7 +172,7 @@ void SemSegConvolutionalTree::train ( const MultiDataset *md )
 
         for ( vector<CachedExample *>::iterator cei = imgexamples.begin();
               cei != imgexamples.end(); cei++ )
-            convertRGBToHSV ( *cei, cf.isColorMode() );
+            preprocessChannels ( *cei, cf.isColorMode() );
 
         // start training using random forests
         fpc->train( fp, examples);
@@ -187,7 +208,7 @@ void SemSegConvolutionalTree::semanticseg(
     DecisionNode *root = forest[0]->getRoot ();
     ConvolutionFeature* cf = dynamic_cast<ConvolutionFeature*> (root->f);
 
-    convertRGBToHSV( ce, cf->isColorMode() ); //FIXME!!!
+    preprocessChannels( ce, cf->isColorMode() ); //FIXME!!!
 
     Example pce ( ce, 0, 0 );
     for ( int y = 0 ; y < ysize ; y++ )

+ 9 - 1
semseg/SemSegConvolutionalTree.h

@@ -30,13 +30,21 @@ class SemSegConvolutionalTree : public SemanticSegmentation
         /** save / load trained classifier */
         bool saveLoadData;
 
+        /** 0 - RGB, 1 - HSV, 2 - CIELab */
+        int colorMode;
+
         /** file location of trained classifier */
         std::string fileLocation;
 
         /** classifier for categorization */
         FeaturePoolClassifier *fpc;
 
-        void convertRGBToHSV ( CachedExample *ce, bool isColor ) const;
+        /**
+         * @brief pre-processing: resulting channels with values in the range [0,1] for RGB, HSV or GREY
+         * @param ce current cached example
+         * @param isColor color mode
+         */
+        void preprocessChannels ( CachedExample *ce, bool isColor ) const;
 
     public: