Bjoern Froehlich 12 жил өмнө
parent
commit
76d362d8e6
4 өөрчлөгдсөн 238 нэмэгдсэн , 239 устгасан
  1. 24 30
      RSSlic.cpp
  2. 2 0
      RSSlic.h
  3. 5 4
      SLIC/SLIC.cpp
  4. 207 205
      SLIC/SLIC.h

+ 24 - 30
RSSlic.cpp

@@ -15,15 +15,18 @@ using namespace OBJREC;
 
 RSSlic::RSSlic()
 {
-  //spcount = 100000;
-  spcount = 200;
+  spcount = -1;
+  regionsize = 50;
   compactness = 10.0;
+  lab = true;
 }
 
 RSSlic::RSSlic(const Config *conf )
 {
-  spcount = conf->gI("RSSlic", "m_spcount", 2000);
+  spcount = conf->gI("RSSlic", "spcount", -1);
+  regionsize = conf->gI("RSSlic", "regionsize", 50);
   compactness = conf->gD("RSSlic", "compactness", 10.0);
+  lab = conf->gB("RSSlic", "useLAB", true);
 }
 
 RSSlic::~RSSlic()
@@ -43,57 +46,48 @@ int RSSlic::segRegions ( const NICE::ColorImage & img, NICE::Matrix & mask) cons
 
   // Kopieren der Werte aus dem ColorImage -> image<rgb>
 
-  uint *inputImage = new uint[3*imageWidth*imageHeight];
+  uint *inputImage = new uint[imageWidth*imageHeight];
 
-//#pragma omp parallel for
   unsigned long int counter = 0;
-  for (int c = 0; c < 3; c++)
-  {
-    for ( unsigned int y = 0; y < imageHeight; y++ )
+    for ( unsigned int y = 0; y < imageHeight; y++)
     {
       for ( unsigned int x = 0; x < imageWidth; x++, counter++)
       {
-        inputImage[counter] = img.getPixelQuick( x, y, c );
+        uint tmp = 255;
+        for (int c = 0; c < 3; c++)
+        {
+          tmp = tmp<<8;
+          tmp+=(unsigned char)img.getPixelQuick( x, y, c );
+        }
+        inputImage[counter] = tmp;
       }
     }
-  }
 
   int* labels = new int[imageWidth*imageHeight];
 
   // Eingabebild segmentieren
   SLIC slic;
   int numlabels = 0;
-  slic.DoSuperpixelSegmentation_ForGivenNumberOfSuperpixels(inputImage, imageWidth, imageHeight, labels, numlabels, spcount, compactness);
+  int superpixelsize = regionsize;
 
-  slic.DrawContoursAroundSegments(inputImage, labels, imageWidth, imageHeight, 0);
-
-  NICE::ColorImage resultImage( imageWidth, imageHeight );
-  /*
-  counter = 0;
-  for (int c = 0; c < 3; c++)
+  if(spcount > 0)
   {
-    for ( unsigned int y = 0; y < imageHeight; y++ )
-    {
-      for ( unsigned int x = 0; x < imageWidth; x++, counter++ )
-      {
-        resultImage.setPixelQuick( x, y, c, inputImage[counter] );
-      }
-    }
+    superpixelsize = 0.5+double(imageWidth*imageHeight)/double(spcount);
   }
-
-  resultImage.write("tmp.ppm");
-*/
+   
+  slic.DoSuperpixelSegmentation_ForGivenSuperpixelSize(inputImage, imageWidth, imageHeight, labels, numlabels, superpixelsize, compactness, lab);
 
   mask.resize(imageWidth, imageHeight);
   counter = 0;
-  for ( unsigned int y = 0; y < imageHeight; y++ )
+  for ( unsigned int y = 0; y < imageHeight; y++)
   {
     for ( unsigned int x = 0; x < imageWidth; x++, counter++ )
     {
       mask(x, y) = labels[counter];
     }
   }
-/*
+
+  /*
   counter = 0;
   for ( unsigned int y = 0; y < imageHeight; y++ )
   {
@@ -113,5 +107,5 @@ int RSSlic::segRegions ( const NICE::ColorImage & img, NICE::Matrix & mask) cons
   delete labels;
   labels = NULL;
 
-  return numlabels -1;
+  return numlabels;
 }

+ 2 - 0
RSSlic.h

@@ -18,6 +18,8 @@ class RSSlic: public RegionSegmentationMethod
 protected:
     int spcount; //number of regions
     double compactness; //value between 0 and 40
+    int regionsize; //average size of each region
+    bool lab; //use lab space or not
 
 public:
     /** simple constructor */

+ 5 - 4
SLIC/SLIC.cpp

@@ -1163,7 +1163,7 @@ void SLIC::DoSuperpixelSegmentation_ForGivenSuperpixelSize(
     int*&           klabels,
     int&            numlabels,
     const int&          superpixelsize,
-    const double&               compactness)
+    const double&               compactness, bool lab)
 {
     //------------------------------------------------
     const int STEP = sqrt(double(superpixelsize))+0.5;
@@ -1183,7 +1183,7 @@ void SLIC::DoSuperpixelSegmentation_ForGivenSuperpixelSize(
     klabels = new int[sz];
     for ( int s = 0; s < sz; s++ ) klabels[s] = -1;
     //--------------------------------------------------
-    if (1)//LAB, the default option
+    if (lab)//LAB, the default option
     {
         DoRGBtoLABConversion(ubuff, m_lvec, m_avec, m_bvec);
     }
@@ -1243,10 +1243,11 @@ void SLIC::DoSuperpixelSegmentation_ForGivenNumberOfSuperpixels(
     int*&           klabels,
     int&            numlabels,
     const int&          K,//required number of superpixels
-    const double&                                   compactness)//weight given to spatial distance
+    const double&                                   compactness,
+    bool lab)//weight given to spatial distance
 {
     const int superpixelsize = 0.5+double(width*height)/double(K);
-    DoSuperpixelSegmentation_ForGivenSuperpixelSize(ubuff,width,height,klabels,numlabels,superpixelsize,compactness);
+    DoSuperpixelSegmentation_ForGivenSuperpixelSize(ubuff,width,height,klabels,numlabels,superpixelsize,compactness, lab);
 }
 
 //===========================================================================

+ 207 - 205
SLIC/SLIC.h

@@ -6,7 +6,7 @@
 // "SLIC Superpixels",
 // EPFL Technical Report no. 149300, June 2010.
 //===========================================================================
-//	Copyright (c) 2012 Radhakrishna Achanta [EPFL]. All rights reserved.
+// Copyright (c) 2012 Radhakrishna Achanta [EPFL]. All rights reserved.
 //===========================================================================
 //////////////////////////////////////////////////////////////////////
 
@@ -20,215 +20,217 @@
 using namespace std;
 
 
-class SLIC  
+class SLIC
 {
-public:
-	SLIC();
-	virtual ~SLIC();
-	//============================================================================
-	// Superpixel segmentation for a given step size (superpixel size ~= step*step)
-	//============================================================================
-        void DoSuperpixelSegmentation_ForGivenSuperpixelSize(
-        const unsigned int*                            ubuff,//Each 32 bit unsigned int contains ARGB pixel values.
-		const int					width,
-		const int					height,
-		int*&						klabels,
-		int&						numlabels,
-                const int&					superpixelsize,
-                const double&                                   compactness);
-	//============================================================================
-	// Superpixel segmentation for a given number of superpixels
-	//============================================================================
-        void DoSuperpixelSegmentation_ForGivenNumberOfSuperpixels(
-        const unsigned int*                             ubuff,
-		const int					width,
-		const int					height,
-		int*&						klabels,
-		int&						numlabels,
-                const int&					K,//required number of superpixels
-                const double&                                   compactness);//10-20 is a good value for CIELAB space
-	//============================================================================
-	// Supervoxel segmentation for a given step size (supervoxel size ~= step*step*step)
-	//============================================================================
-	void DoSupervoxelSegmentation(
-		unsigned int**&		ubuffvec,
-		const int&					width,
-		const int&					height,
-		const int&					depth,
-		int**&						klabels,
-		int&						numlabels,
-                const int&					supervoxelsize,
-                const double&                                   compactness);
-	//============================================================================
-	// Save superpixel labels in a text file in raster scan order
-	//============================================================================
-	void SaveSuperpixelLabels(
-		const int*&					labels,
-		const int&					width,
-		const int&					height,
-		const string&				filename,
-		const string&				path);
-	//============================================================================
-	// Save supervoxel labels in a text file in raster scan, depth order
-	//============================================================================
-	void SaveSupervoxelLabels(
-		const int**&				labels,
-		const int&					width,
-		const int&					height,
-		const int&					depth,
-		const string&				filename,
-		const string&				path);
-	//============================================================================
-	// Function to draw boundaries around superpixels of a given 'color'.
-	// Can also be used to draw boundaries around supervoxels, i.e layer by layer.
-	//============================================================================
-	void DrawContoursAroundSegments(
-		unsigned int*&				segmentedImage,
-		int*&						labels,
-		const int&					width,
-		const int&					height,
-		const unsigned int&			color );
+  public:
+    SLIC();
+    virtual ~SLIC();
+    //============================================================================
+    // Superpixel segmentation for a given step size (superpixel size ~= step*step)
+    //============================================================================
+    void DoSuperpixelSegmentation_ForGivenSuperpixelSize(
+      const unsigned int*                            ubuff,//Each 32 bit unsigned int contains ARGB pixel values.
+      const int      width,
+      const int      height,
+      int*&          klabels,
+      int&           numlabels,
+      const int&     superpixelsize,
+      const double&  compactness,
+      bool          lab = true);
+    //============================================================================
+    // Superpixel segmentation for a given number of superpixels
+    //============================================================================
+    void DoSuperpixelSegmentation_ForGivenNumberOfSuperpixels(
+      const unsigned int*                             ubuff,
+      const int      width,
+      const int      height,
+      int*&          klabels,
+      int&           numlabels,
+      const int&     K,//required number of superpixels
+      const double&  compactness,
+      bool          lab = true);//10-20 is a good value for CIELAB space
+    //============================================================================
+    // Supervoxel segmentation for a given step size (supervoxel size ~= step*step*step)
+    //============================================================================
+    void DoSupervoxelSegmentation(
+      unsigned int**&  ubuffvec,
+      const int&     width,
+      const int&     height,
+      const int&     depth,
+      int**&      klabels,
+      int&      numlabels,
+      const int&     supervoxelsize,
+      const double&                                   compactness);
+    //============================================================================
+    // Save superpixel labels in a text file in raster scan order
+    //============================================================================
+    void SaveSuperpixelLabels(
+      const int*&     labels,
+      const int&     width,
+      const int&     height,
+      const string&    filename,
+      const string&    path);
+    //============================================================================
+    // Save supervoxel labels in a text file in raster scan, depth order
+    //============================================================================
+    void SaveSupervoxelLabels(
+      const int**&    labels,
+      const int&     width,
+      const int&     height,
+      const int&     depth,
+      const string&    filename,
+      const string&    path);
+    //============================================================================
+    // Function to draw boundaries around superpixels of a given 'color'.
+    // Can also be used to draw boundaries around supervoxels, i.e layer by layer.
+    //============================================================================
+    void DrawContoursAroundSegments(
+      unsigned int*&    segmentedImage,
+      int*&      labels,
+      const int&     width,
+      const int&     height,
+      const unsigned int&   color );
 
-private:
-	//============================================================================
-	// The main SLIC algorithm for generating superpixels
-	//============================================================================
-	void PerformSuperpixelSLIC(
-		vector<double>&				kseedsl,
-		vector<double>&				kseedsa,
-		vector<double>&				kseedsb,
-		vector<double>&				kseedsx,
-		vector<double>&				kseedsy,
-		int*&						klabels,
-		const int&					STEP,
-                const vector<double>&		edgemag,
-		const double&				m = 10.0);
-	//============================================================================
-	// The main SLIC algorithm for generating supervoxels
-	//============================================================================
-	void PerformSupervoxelSLIC(
-		vector<double>&				kseedsl,
-		vector<double>&				kseedsa,
-		vector<double>&				kseedsb,
-		vector<double>&				kseedsx,
-		vector<double>&				kseedsy,
-		vector<double>&				kseedsz,
-		int**&						klabels,
-		const int&					STEP,
-		const double&				compactness);
-	//============================================================================
-	// Pick seeds for superpixels when step size of superpixels is given.
-	//============================================================================
-	void GetLABXYSeeds_ForGivenStepSize(
-		vector<double>&				kseedsl,
-		vector<double>&				kseedsa,
-		vector<double>&				kseedsb,
-		vector<double>&				kseedsx,
-		vector<double>&				kseedsy,
-		const int&					STEP,
-		const bool&					perturbseeds,
-		const vector<double>&		edgemag);
-	//============================================================================
-	// Pick seeds for supervoxels
-	//============================================================================
-	void GetKValues_LABXYZ(
-		vector<double>&				kseedsl,
-		vector<double>&				kseedsa,
-		vector<double>&				kseedsb,
-		vector<double>&				kseedsx,
-		vector<double>&				kseedsy,
-		vector<double>&				kseedsz,
-		const int&					STEP);
-	//============================================================================
-	// Move the superpixel seeds to low gradient positions to avoid putting seeds
-	// at region boundaries.
-	//============================================================================
-	void PerturbSeeds(
-		vector<double>&				kseedsl,
-		vector<double>&				kseedsa,
-		vector<double>&				kseedsb,
-		vector<double>&				kseedsx,
-		vector<double>&				kseedsy,
-		const vector<double>&		edges);
-	//============================================================================
-	// Detect color edges, to help PerturbSeeds()
-	//============================================================================
-	void DetectLabEdges(
-		const double*				lvec,
-		const double*				avec,
-		const double*				bvec,
-		const int&					width,
-		const int&					height,
-		vector<double>&				edges);
-	//============================================================================
-	// sRGB to XYZ conversion; helper for RGB2LAB()
-	//============================================================================
-	void RGB2XYZ(
-		const int&					sR,
-		const int&					sG,
-		const int&					sB,
-		double&						X,
-		double&						Y,
-		double&						Z);
-	//============================================================================
-	// sRGB to CIELAB conversion (uses RGB2XYZ function)
-	//============================================================================
-	void RGB2LAB(
-		const int&					sR,
-		const int&					sG,
-		const int&					sB,
-		double&						lval,
-		double&						aval,
-		double&						bval);
-	//============================================================================
-	// sRGB to CIELAB conversion for 2-D images
-	//============================================================================
-	void DoRGBtoLABConversion(
-		const unsigned int*&		ubuff,
-		double*&					lvec,
-		double*&					avec,
-		double*&					bvec);
-	//============================================================================
-	// sRGB to CIELAB conversion for 3-D volumes
-	//============================================================================
-	void DoRGBtoLABConversion(
-		unsigned int**&				ubuff,
-		double**&					lvec,
-		double**&					avec,
-		double**&					bvec);
-	//============================================================================
-	// Post-processing of SLIC segmentation, to avoid stray labels.
-	//============================================================================
-	void EnforceLabelConnectivity(
-		const int*					labels,
-		const int					width,
-		const int					height,
-		int*&						nlabels,//input labels that need to be corrected to remove stray labels
-		int&						numlabels,//the number of labels changes in the end if segments are removed
-		const int&					K); //the number of superpixels desired by the user
-	//============================================================================
-	// Post-processing of SLIC supervoxel segmentation, to avoid stray labels.
-	//============================================================================
-	void EnforceSupervoxelLabelConnectivity(
-		int**&						labels,//input - previous labels, output - new labels
-		const int&					width,
-		const int&					height,
-		const int&					depth,
-		int&						numlabels,
-		const int&					STEP);
+  private:
+    //============================================================================
+    // The main SLIC algorithm for generating superpixels
+    //============================================================================
+    void PerformSuperpixelSLIC(
+      vector<double>&    kseedsl,
+      vector<double>&    kseedsa,
+      vector<double>&    kseedsb,
+      vector<double>&    kseedsx,
+      vector<double>&    kseedsy,
+      int*&      klabels,
+      const int&     STEP,
+      const vector<double>&  edgemag,
+      const double&    m = 10.0);
+    //============================================================================
+    // The main SLIC algorithm for generating supervoxels
+    //============================================================================
+    void PerformSupervoxelSLIC(
+      vector<double>&    kseedsl,
+      vector<double>&    kseedsa,
+      vector<double>&    kseedsb,
+      vector<double>&    kseedsx,
+      vector<double>&    kseedsy,
+      vector<double>&    kseedsz,
+      int**&      klabels,
+      const int&     STEP,
+      const double&    compactness);
+    //============================================================================
+    // Pick seeds for superpixels when step size of superpixels is given.
+    //============================================================================
+    void GetLABXYSeeds_ForGivenStepSize(
+      vector<double>&    kseedsl,
+      vector<double>&    kseedsa,
+      vector<double>&    kseedsb,
+      vector<double>&    kseedsx,
+      vector<double>&    kseedsy,
+      const int&     STEP,
+      const bool&     perturbseeds,
+      const vector<double>&  edgemag);
+    //============================================================================
+    // Pick seeds for supervoxels
+    //============================================================================
+    void GetKValues_LABXYZ(
+      vector<double>&    kseedsl,
+      vector<double>&    kseedsa,
+      vector<double>&    kseedsb,
+      vector<double>&    kseedsx,
+      vector<double>&    kseedsy,
+      vector<double>&    kseedsz,
+      const int&     STEP);
+    //============================================================================
+    // Move the superpixel seeds to low gradient positions to avoid putting seeds
+    // at region boundaries.
+    //============================================================================
+    void PerturbSeeds(
+      vector<double>&    kseedsl,
+      vector<double>&    kseedsa,
+      vector<double>&    kseedsb,
+      vector<double>&    kseedsx,
+      vector<double>&    kseedsy,
+      const vector<double>&  edges);
+    //============================================================================
+    // Detect color edges, to help PerturbSeeds()
+    //============================================================================
+    void DetectLabEdges(
+      const double*    lvec,
+      const double*    avec,
+      const double*    bvec,
+      const int&     width,
+      const int&     height,
+      vector<double>&    edges);
+    //============================================================================
+    // sRGB to XYZ conversion; helper for RGB2LAB()
+    //============================================================================
+    void RGB2XYZ(
+      const int&     sR,
+      const int&     sG,
+      const int&     sB,
+      double&      X,
+      double&      Y,
+      double&      Z);
+    //============================================================================
+    // sRGB to CIELAB conversion (uses RGB2XYZ function)
+    //============================================================================
+    void RGB2LAB(
+      const int&     sR,
+      const int&     sG,
+      const int&     sB,
+      double&      lval,
+      double&      aval,
+      double&      bval);
+    //============================================================================
+    // sRGB to CIELAB conversion for 2-D images
+    //============================================================================
+    void DoRGBtoLABConversion(
+      const unsigned int*&  ubuff,
+      double*&     lvec,
+      double*&     avec,
+      double*&     bvec);
+    //============================================================================
+    // sRGB to CIELAB conversion for 3-D volumes
+    //============================================================================
+    void DoRGBtoLABConversion(
+      unsigned int**&    ubuff,
+      double**&     lvec,
+      double**&     avec,
+      double**&     bvec);
+    //============================================================================
+    // Post-processing of SLIC segmentation, to avoid stray labels.
+    //============================================================================
+    void EnforceLabelConnectivity(
+      const int*     labels,
+      const int     width,
+      const int     height,
+      int*&      nlabels,//input labels that need to be corrected to remove stray labels
+      int&      numlabels,//the number of labels changes in the end if segments are removed
+      const int&     K); //the number of superpixels desired by the user
+    //============================================================================
+    // Post-processing of SLIC supervoxel segmentation, to avoid stray labels.
+    //============================================================================
+    void EnforceSupervoxelLabelConnectivity(
+      int**&      labels,//input - previous labels, output - new labels
+      const int&     width,
+      const int&     height,
+      const int&     depth,
+      int&      numlabels,
+      const int&     STEP);
 
-private:
-	int										m_width;
-	int										m_height;
-	int										m_depth;
+  private:
+    int          m_width;
+    int          m_height;
+    int          m_depth;
 
-	double*									m_lvec;
-	double*									m_avec;
-	double*									m_bvec;
+    double*         m_lvec;
+    double*         m_avec;
+    double*         m_bvec;
 
-	double**								m_lvecvec;
-	double**								m_avecvec;
-	double**								m_bvecvec;
+    double**        m_lvecvec;
+    double**        m_avecvec;
+    double**        m_bvecvec;
 };
 
 #endif // !defined(_SLIC_H_INCLUDED_)