VAT  3.0
Video Analysis Tool
SLIC.h
1 // SLIC.h: interface for the SLIC class.
2 //===========================================================================
3 // This code implements the superpixel method described in:
4 //
5 // Radhakrishna Achanta, Appu Shaji, Kevin Smith, Aurelien Lucchi, Pascal Fua, and Sabine Susstrunk,
6 // "SLIC Superpixels",
7 // EPFL Technical Report no. 149300, June 2010.
8 //===========================================================================
9 // Copyright (c) 2012 Radhakrishna Achanta [EPFL]. All rights reserved.
10 //===========================================================================
12 
13 #ifndef _SLIC_H_INCLUDED_
14 #define _SLIC_H_INCLUDED_
15 
16 
17 #include <vector>
18 #include <string>
19 #include <algorithm>
20 using namespace std;
21 
22 
23 class SLIC
24 {
25 public:
26  SLIC();
27  virtual ~SLIC();
28  //============================================================================
29  // Superpixel segmentation for a given step size (superpixel size ~= step*step)
30  //============================================================================
31  void DoSuperpixelSegmentation_ForGivenSuperpixelSize(
32  const unsigned char* ubuff,//Each 32 bit unsigned int contains ARGB pixel values.
33  const int width,
34  const int height,
35  int*& klabels,
36  int& numlabels,
37  const int& superpixelsize,
38  const double& compactness);
39  //============================================================================
40  // Superpixel segmentation for a given number of superpixels
41  //============================================================================
42  void DoSuperpixelSegmentation_ForGivenNumberOfSuperpixels(
43  const unsigned char* ubuff,
44  const int width,
45  const int height,
46  int*& klabels,
47  int& numlabels,
48  const int& K,//required number of superpixels
49  const double& compactness);//10-20 is a good value for CIELAB space
50  //============================================================================
51  // Supervoxel segmentation for a given step size (supervoxel size ~= step*step*step)
52  //============================================================================
53  void DoSupervoxelSegmentation(
54  unsigned int**& ubuffvec,
55  const int& width,
56  const int& height,
57  const int& depth,
58  int**& klabels,
59  int& numlabels,
60  const int& supervoxelsize,
61  const double& compactness);
62 private:
63  //============================================================================
64  // The main SLIC algorithm for generating superpixels
65  //============================================================================
66  void PerformSuperpixelSLIC(
67  vector<double>& kseedsl,
68  vector<double>& kseedsa,
69  vector<double>& kseedsb,
70  vector<double>& kseedsx,
71  vector<double>& kseedsy,
72  int*& klabels,
73  const int& STEP,
74  const vector<double>& edgemag,
75  const double& m = 10.0);
76  //============================================================================
77  // The main SLIC algorithm for generating supervoxels
78  //============================================================================
79  void PerformSupervoxelSLIC(
80  vector<double>& kseedsl,
81  vector<double>& kseedsa,
82  vector<double>& kseedsb,
83  vector<double>& kseedsx,
84  vector<double>& kseedsy,
85  vector<double>& kseedsz,
86  int**& klabels,
87  const int& STEP,
88  const double& compactness);
89  //============================================================================
90  // Pick seeds for superpixels when step size of superpixels is given.
91  //============================================================================
92  void GetLABXYSeeds_ForGivenStepSize(
93  vector<double>& kseedsl,
94  vector<double>& kseedsa,
95  vector<double>& kseedsb,
96  vector<double>& kseedsx,
97  vector<double>& kseedsy,
98  const int& STEP,
99  const bool& perturbseeds,
100  const vector<double>& edgemag);
101  //============================================================================
102  // Pick seeds for supervoxels
103  //============================================================================
104  void GetKValues_LABXYZ(
105  vector<double>& kseedsl,
106  vector<double>& kseedsa,
107  vector<double>& kseedsb,
108  vector<double>& kseedsx,
109  vector<double>& kseedsy,
110  vector<double>& kseedsz,
111  const int& STEP);
112  //============================================================================
113  // Move the superpixel seeds to low gradient positions to avoid putting seeds
114  // at region boundaries.
115  //============================================================================
116  void PerturbSeeds(
117  vector<double>& kseedsl,
118  vector<double>& kseedsa,
119  vector<double>& kseedsb,
120  vector<double>& kseedsx,
121  vector<double>& kseedsy,
122  const vector<double>& edges);
123  //============================================================================
124  // Detect color edges, to help PerturbSeeds()
125  //============================================================================
126  void DetectLabEdges(
127  const double* lvec,
128  const double* avec,
129  const double* bvec,
130  const int& width,
131  const int& height,
132  vector<double>& edges);
133  //============================================================================
134  // sRGB to XYZ conversion; helper for RGB2LAB()
135  //============================================================================
136  void RGB2XYZ(
137  const int& sR,
138  const int& sG,
139  const int& sB,
140  double& X,
141  double& Y,
142  double& Z);
143  //============================================================================
144  // sRGB to CIELAB conversion (uses RGB2XYZ function)
145  //============================================================================
146  void RGB2LAB(
147  const int& sR,
148  const int& sG,
149  const int& sB,
150  double& lval,
151  double& aval,
152  double& bval);
153  //============================================================================
154  // sRGB to CIELAB conversion for 2-D images
155  //============================================================================
156  void DoRGBtoLABConversion(
157  const unsigned char*& ubuff,
158  double*& lvec,
159  double*& avec,
160  double*& bvec);
161  //============================================================================
162  // sRGB to CIELAB conversion for 3-D volumes
163  //============================================================================
164  void DoRGBtoLABConversion(
165  unsigned int**& ubuff,
166  double**& lvec,
167  double**& avec,
168  double**& bvec);
169  //============================================================================
170  // Post-processing of SLIC segmentation, to avoid stray labels.
171  //============================================================================
172  void EnforceLabelConnectivity(
173  const int* labels,
174  const int width,
175  const int height,
176  int*& nlabels,//input labels that need to be corrected to remove stray labels
177  int& numlabels,//the number of labels changes in the end if segments are removed
178  const int& K); //the number of superpixels desired by the user
179  //============================================================================
180  // Post-processing of SLIC supervoxel segmentation, to avoid stray labels.
181  //============================================================================
182  void EnforceSupervoxelLabelConnectivity(
183  int**& labels,//input - previous labels, output - new labels
184  const int& width,
185  const int& height,
186  const int& depth,
187  int& numlabels,
188  const int& STEP);
189 
190 public:
191  void DrawContoursAroundSegments(
192  unsigned char *&ubuff,
193  int*& labels,
194  const int& width,
195  const int& height);
196 
197 
198 
199 private:
200  int m_width;
201  int m_height;
202  int m_depth;
203 
204  double* m_lvec;
205  double* m_avec;
206  double* m_bvec;
207 
208  double** m_lvecvec;
209  double** m_avecvec;
210  double** m_bvecvec;
211 };
212 
213 #endif // !defined(_SLIC_H_INCLUDED_)
Definition: SLIC.h:23