123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- // SLIC.h: interface for the SLIC class.
- //===========================================================================
- // This code implements the superpixel method described in:
- //
- // Radhakrishna Achanta, Appu Shaji, Kevin Smith, Aurelien Lucchi, Pascal Fua, and Sabine Susstrunk,
- // "SLIC Superpixels",
- // EPFL Technical Report no. 149300, June 2010.
- //===========================================================================
- // Copyright (c) 2012 Radhakrishna Achanta [EPFL]. All rights reserved.
- //===========================================================================
- //////////////////////////////////////////////////////////////////////
- #if !defined(_SLIC_H_INCLUDED_)
- #define _SLIC_H_INCLUDED_
- #include <vector>
- #include <string>
- #include <algorithm>
- using namespace std;
- 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,
- 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:
- int m_width;
- int m_height;
- int m_depth;
- double* m_lvec;
- double* m_avec;
- double* m_bvec;
- double** m_lvecvec;
- double** m_avecvec;
- double** m_bvecvec;
- };
- #endif // !defined(_SLIC_H_INCLUDED_)
|