VAT  3.0
Video Analysis Tool
SLICO.h
1 // SLICO.h: interface for the SLICO class.
2 //===========================================================================
3 // This code implements the zero parameter superpixel segmentation technique
4 // described in:
5 //
6 //
7 //
8 // "SLIC Superpixels Compared to State-of-the-art Superpixel Methods"
9 //
10 // Radhakrishna Achanta, Appu Shaji, Kevin Smith, Aurelien Lucchi, Pascal Fua,
11 // and Sabine Susstrunk,
12 //
13 // IEEE TPAMI, Volume 34, Issue 11, Pages 2274-2282, November 2012.
14 //
15 //
16 //===========================================================================
17 // Copyright (c) 2013 Radhakrishna Achanta.
18 //
19 // For commercial use please contact the author:
20 //
21 // Email: firstname.lastname@epfl.ch
22 //===========================================================================
23 
24 #ifndef _SLICO_H_INCLUDED_
25 #define _SLICO_H_INCLUDED_
26 
27 
28 #include <vector>
29 #include <string>
30 #include <algorithm>
31 using namespace std;
32 
33 
34 class SLICO
35 {
36 public:
37  SLICO();
38  virtual ~SLICO();
39  //============================================================================
40  // Superpixel segmentation for a given step size (superpixel size ~= step*step)
41  //============================================================================
42  void PerformSLICO_ForGivenStepSize(
43  const unsigned char* ubuff,//Each 32 bit unsigned int contains ARGB pixel values.
44  const int width,
45  const int height,
46  int* klabels,
47  int& numlabels,
48  const int& STEP,
49  const double& m);
50  //============================================================================
51  // Superpixel segmentation for a given number of superpixels
52  //============================================================================
53  void PerformSLICO_ForGivenK(
54  const unsigned char* ubuff,//Each 32 bit unsigned int contains ARGB pixel values.
55  const int width,
56  const int height,
57  int* klabels,
58  int& numlabels,
59  const int& K);
60 
61  //============================================================================
62  // Save superpixel labels in a text file in raster scan order
63  //============================================================================
64  /*void SaveSuperpixelLabels(
65  const int* labels,
66  const int& width,
67  const int& height,
68  const string& filename,
69  const string& path);*/
70  //============================================================================
71  // Function to draw boundaries around superpixels of a given 'color'.
72  // Can also be used to draw boundaries around supervoxels, i.e layer by layer.
73  //============================================================================
74  void DrawContoursAroundSegments(
75  unsigned char* segmentedImage,
76  const int* labels,
77  const int& width,
78  const int& height);
79 
80  void DrawContoursAroundSegmentsTwoColors(
81  unsigned char* ubuff,
82  const int* labels,
83  const int& width,
84  const int& height);
85 
86 private:
87 
88  //============================================================================
89  // Magic SLIC. No need to set M (compactness factor) and S (step size).
90  // SLICO (SLIC Zero) varies only M dynamicaly, not S.
91  //============================================================================
92  void PerformSuperpixelSegmentation_VariableSandM(
93  vector<double>& kseedsl,
94  vector<double>& kseedsa,
95  vector<double>& kseedsb,
96  vector<double>& kseedsx,
97  vector<double>& kseedsy,
98  int* klabels,
99  const int& STEP,
100  const int& NUMITR);
101  //============================================================================
102  // Pick seeds for superpixels when step size of superpixels is given.
103  //============================================================================
104  void GetLABXYSeeds_ForGivenStepSize(
105  vector<double>& kseedsl,
106  vector<double>& kseedsa,
107  vector<double>& kseedsb,
108  vector<double>& kseedsx,
109  vector<double>& kseedsy,
110  const int& STEP,
111  const bool& perturbseeds,
112  const vector<double>& edgemag);
113  //============================================================================
114  // Pick seeds for superpixels when number of superpixels is input.
115  //============================================================================
116  void GetLABXYSeeds_ForGivenK(
117  vector<double>& kseedsl,
118  vector<double>& kseedsa,
119  vector<double>& kseedsb,
120  vector<double>& kseedsx,
121  vector<double>& kseedsy,
122  const int& STEP,
123  const bool& perturbseeds,
124  const vector<double>& edges);
125 
126  //============================================================================
127  // Move the seeds to low gradient positions to avoid putting seeds at region boundaries.
128  //============================================================================
129  void PerturbSeeds(
130  vector<double>& kseedsl,
131  vector<double>& kseedsa,
132  vector<double>& kseedsb,
133  vector<double>& kseedsx,
134  vector<double>& kseedsy,
135  const vector<double>& edges);
136  //============================================================================
137  // Detect color edges, to help PerturbSeeds()
138  //============================================================================
139  void DetectLabEdges(
140  const double* lvec,
141  const double* avec,
142  const double* bvec,
143  const int& width,
144  const int& height,
145  vector<double>& edges);
146  //============================================================================
147  // xRGB to XYZ conversion; helper for RGB2LAB()
148  //============================================================================
149  void RGB2XYZ(
150  const unsigned char& sR,
151  const unsigned char& sG,
152  const unsigned char& sB,
153  double& X,
154  double& Y,
155  double& Z);
156  //============================================================================
157  // sRGB to CIELAB conversion
158  //============================================================================
159  void RGB2LAB(
160  const unsigned char& sR,
161  const unsigned char& sG,
162  const unsigned char& sB,
163  double& lval,
164  double& aval,
165  double& bval);
166  //============================================================================
167  // sRGB to CIELAB conversion for 2-D images
168  //============================================================================
169  void DoRGBtoLABConversion(
170  const unsigned char*& ubuff,
171  double*& lvec,
172  double*& avec,
173  double*& bvec);
174  //============================================================================
175  // sRGB to CIELAB conversion for 3-D volumes
176  //============================================================================
177  void DoRGBtoLABConversion(
178  const unsigned char**& ubuff,
179  double**& lvec,
180  double**& avec,
181  double**& bvec);
182 
183  //============================================================================
184  // Post-processing of SLIC segmentation, to avoid stray labels.
185  //============================================================================
186  void EnforceLabelConnectivity(
187  const int* labels,
188  const int& width,
189  const int& height,
190  int* nlabels,//input labels that need to be corrected to remove stray labels
191  int& numlabels,//the number of labels changes in the end if segments are removed
192  const int& K); //the number of superpixels desired by the user
193 
194 
195 private:
196  int m_width;
197  int m_height;
198  int m_depth;
199 
200  double* m_lvec;
201  double* m_avec;
202  double* m_bvec;
203 
204  double** m_lvecvec;
205  double** m_avecvec;
206  double** m_bvecvec;
207 };
208 
209 #endif // _SLICO_H_INCLUDED_
Definition: SLICO.h:34