SLIC.h 9.4 KB

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