SLIC.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. //===========================================================================
  11. //////////////////////////////////////////////////////////////////////
  12. #if !defined(_SLIC_H_INCLUDED_)
  13. #define _SLIC_H_INCLUDED_
  14. #include <vector>
  15. #include <string>
  16. #include <algorithm>
  17. using namespace std;
  18. class SLIC
  19. {
  20. public:
  21. SLIC();
  22. virtual ~SLIC();
  23. //============================================================================
  24. // Superpixel segmentation for a given step size (superpixel size ~= step*step)
  25. //============================================================================
  26. void DoSuperpixelSegmentation_ForGivenSuperpixelSize(
  27. const unsigned int* ubuff,//Each 32 bit unsigned int contains ARGB pixel values.
  28. const int width,
  29. const int height,
  30. int*& klabels,
  31. int& numlabels,
  32. const int& superpixelsize,
  33. const double& compactness,
  34. bool lab = true);
  35. //============================================================================
  36. // Superpixel segmentation for a given number of superpixels
  37. //============================================================================
  38. void DoSuperpixelSegmentation_ForGivenNumberOfSuperpixels(
  39. const unsigned int* ubuff,
  40. const int width,
  41. const int height,
  42. int*& klabels,
  43. int& numlabels,
  44. const int& K,//required number of superpixels
  45. const double& compactness,
  46. bool lab = true);//10-20 is a good value for CIELAB space
  47. //============================================================================
  48. // Supervoxel segmentation for a given step size (supervoxel size ~= step*step*step)
  49. //============================================================================
  50. void DoSupervoxelSegmentation(
  51. unsigned int**& ubuffvec,
  52. const int& width,
  53. const int& height,
  54. const int& depth,
  55. int**& klabels,
  56. int& numlabels,
  57. const int& supervoxelsize,
  58. const double& compactness);
  59. //============================================================================
  60. // Save superpixel labels in a text file in raster scan order
  61. //============================================================================
  62. void SaveSuperpixelLabels(
  63. const int*& labels,
  64. const int& width,
  65. const int& height,
  66. const string& filename,
  67. const string& path);
  68. //============================================================================
  69. // Save supervoxel labels in a text file in raster scan, depth order
  70. //============================================================================
  71. void SaveSupervoxelLabels(
  72. const int**& labels,
  73. const int& width,
  74. const int& height,
  75. const int& depth,
  76. const string& filename,
  77. const string& path);
  78. //============================================================================
  79. // Function to draw boundaries around superpixels of a given 'color'.
  80. // Can also be used to draw boundaries around supervoxels, i.e layer by layer.
  81. //============================================================================
  82. void DrawContoursAroundSegments(
  83. unsigned int*& segmentedImage,
  84. int*& labels,
  85. const int& width,
  86. const int& height,
  87. const unsigned int& color );
  88. private:
  89. //============================================================================
  90. // The main SLIC algorithm for generating superpixels
  91. //============================================================================
  92. void PerformSuperpixelSLIC(
  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 vector<double>& edgemag,
  101. const double& m = 10.0);
  102. //============================================================================
  103. // The main SLIC algorithm for generating supervoxels
  104. //============================================================================
  105. void PerformSupervoxelSLIC(
  106. vector<double>& kseedsl,
  107. vector<double>& kseedsa,
  108. vector<double>& kseedsb,
  109. vector<double>& kseedsx,
  110. vector<double>& kseedsy,
  111. vector<double>& kseedsz,
  112. int**& klabels,
  113. const int& STEP,
  114. const double& compactness);
  115. //============================================================================
  116. // Pick seeds for superpixels when step size of superpixels is given.
  117. //============================================================================
  118. void GetLABXYSeeds_ForGivenStepSize(
  119. vector<double>& kseedsl,
  120. vector<double>& kseedsa,
  121. vector<double>& kseedsb,
  122. vector<double>& kseedsx,
  123. vector<double>& kseedsy,
  124. const int& STEP,
  125. const bool& perturbseeds,
  126. const vector<double>& edgemag);
  127. //============================================================================
  128. // Pick seeds for supervoxels
  129. //============================================================================
  130. void GetKValues_LABXYZ(
  131. vector<double>& kseedsl,
  132. vector<double>& kseedsa,
  133. vector<double>& kseedsb,
  134. vector<double>& kseedsx,
  135. vector<double>& kseedsy,
  136. vector<double>& kseedsz,
  137. const int& STEP);
  138. //============================================================================
  139. // Move the superpixel seeds to low gradient positions to avoid putting seeds
  140. // at region boundaries.
  141. //============================================================================
  142. void PerturbSeeds(
  143. vector<double>& kseedsl,
  144. vector<double>& kseedsa,
  145. vector<double>& kseedsb,
  146. vector<double>& kseedsx,
  147. vector<double>& kseedsy,
  148. const vector<double>& edges);
  149. //============================================================================
  150. // Detect color edges, to help PerturbSeeds()
  151. //============================================================================
  152. void DetectLabEdges(
  153. const double* lvec,
  154. const double* avec,
  155. const double* bvec,
  156. const int& width,
  157. const int& height,
  158. vector<double>& edges);
  159. //============================================================================
  160. // sRGB to XYZ conversion; helper for RGB2LAB()
  161. //============================================================================
  162. void RGB2XYZ(
  163. const int& sR,
  164. const int& sG,
  165. const int& sB,
  166. double& X,
  167. double& Y,
  168. double& Z);
  169. //============================================================================
  170. // sRGB to CIELAB conversion (uses RGB2XYZ function)
  171. //============================================================================
  172. void RGB2LAB(
  173. const int& sR,
  174. const int& sG,
  175. const int& sB,
  176. double& lval,
  177. double& aval,
  178. double& bval);
  179. //============================================================================
  180. // sRGB to CIELAB conversion for 2-D images
  181. //============================================================================
  182. void DoRGBtoLABConversion(
  183. const unsigned int*& ubuff,
  184. double*& lvec,
  185. double*& avec,
  186. double*& bvec);
  187. //============================================================================
  188. // sRGB to CIELAB conversion for 3-D volumes
  189. //============================================================================
  190. void DoRGBtoLABConversion(
  191. unsigned int**& ubuff,
  192. double**& lvec,
  193. double**& avec,
  194. double**& bvec);
  195. //============================================================================
  196. // Post-processing of SLIC segmentation, to avoid stray labels.
  197. //============================================================================
  198. void EnforceLabelConnectivity(
  199. const int* labels,
  200. const int width,
  201. const int height,
  202. int*& nlabels,//input labels that need to be corrected to remove stray labels
  203. int& numlabels,//the number of labels changes in the end if segments are removed
  204. const int& K); //the number of superpixels desired by the user
  205. //============================================================================
  206. // Post-processing of SLIC supervoxel segmentation, to avoid stray labels.
  207. //============================================================================
  208. void EnforceSupervoxelLabelConnectivity(
  209. int**& labels,//input - previous labels, output - new labels
  210. const int& width,
  211. const int& height,
  212. const int& depth,
  213. int& numlabels,
  214. const int& STEP);
  215. private:
  216. int m_width;
  217. int m_height;
  218. int m_depth;
  219. double* m_lvec;
  220. double* m_avec;
  221. double* m_bvec;
  222. double** m_lvecvec;
  223. double** m_avecvec;
  224. double** m_bvecvec;
  225. };
  226. #endif // !defined(_SLIC_H_INCLUDED_)