-
Notifications
You must be signed in to change notification settings - Fork 9
/
SLIC.h
234 lines (224 loc) · 9.2 KB
/
SLIC.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// 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);
//============================================================================
// 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 );
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_)