SuperpixelSLIC.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.UtilsModule;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace OpenCVForUnity.XimgprocModule
  7. {
  8. // C++: class SuperpixelSLIC
  9. /**
  10. * Class implementing the SLIC (Simple Linear Iterative Clustering) superpixels
  11. * algorithm described in CITE: Achanta2012.
  12. *
  13. * SLIC (Simple Linear Iterative Clustering) clusters pixels using pixel channels and image plane space
  14. * to efficiently generate compact, nearly uniform superpixels. The simplicity of approach makes it
  15. * extremely easy to use a lone parameter specifies the number of superpixels and the efficiency of
  16. * the algorithm makes it very practical.
  17. * Several optimizations are available for SLIC class:
  18. * SLICO stands for "Zero parameter SLIC" and it is an optimization of baseline SLIC described in CITE: Achanta2012.
  19. * MSLIC stands for "Manifold SLIC" and it is an optimization of baseline SLIC described in CITE: Liu_2017_IEEE.
  20. */
  21. public class SuperpixelSLIC : Algorithm
  22. {
  23. protected override void Dispose(bool disposing)
  24. {
  25. try
  26. {
  27. if (disposing)
  28. {
  29. }
  30. if (IsEnabledDispose)
  31. {
  32. if (nativeObj != IntPtr.Zero)
  33. ximgproc_SuperpixelSLIC_delete(nativeObj);
  34. nativeObj = IntPtr.Zero;
  35. }
  36. }
  37. finally
  38. {
  39. base.Dispose(disposing);
  40. }
  41. }
  42. protected internal SuperpixelSLIC(IntPtr addr) : base(addr) { }
  43. // internal usage only
  44. public static new SuperpixelSLIC __fromPtr__(IntPtr addr) { return new SuperpixelSLIC(addr); }
  45. //
  46. // C++: int cv::ximgproc::SuperpixelSLIC::getNumberOfSuperpixels()
  47. //
  48. /**
  49. * Calculates the actual amount of superpixels on a given segmentation computed
  50. * and stored in SuperpixelSLIC object.
  51. * return automatically generated
  52. */
  53. public int getNumberOfSuperpixels()
  54. {
  55. ThrowIfDisposed();
  56. return ximgproc_SuperpixelSLIC_getNumberOfSuperpixels_10(nativeObj);
  57. }
  58. //
  59. // C++: void cv::ximgproc::SuperpixelSLIC::iterate(int num_iterations = 10)
  60. //
  61. /**
  62. * Calculates the superpixel segmentation on a given image with the initialized
  63. * parameters in the SuperpixelSLIC object.
  64. *
  65. * This function can be called again without the need of initializing the algorithm with
  66. * createSuperpixelSLIC(). This save the computational cost of allocating memory for all the
  67. * structures of the algorithm.
  68. *
  69. * param num_iterations Number of iterations. Higher number improves the result.
  70. *
  71. * The function computes the superpixels segmentation of an image with the parameters initialized
  72. * with the function createSuperpixelSLIC(). The algorithms starts from a grid of superpixels and
  73. * then refines the boundaries by proposing updates of edges boundaries.
  74. */
  75. public void iterate(int num_iterations)
  76. {
  77. ThrowIfDisposed();
  78. ximgproc_SuperpixelSLIC_iterate_10(nativeObj, num_iterations);
  79. }
  80. /**
  81. * Calculates the superpixel segmentation on a given image with the initialized
  82. * parameters in the SuperpixelSLIC object.
  83. *
  84. * This function can be called again without the need of initializing the algorithm with
  85. * createSuperpixelSLIC(). This save the computational cost of allocating memory for all the
  86. * structures of the algorithm.
  87. *
  88. *
  89. * The function computes the superpixels segmentation of an image with the parameters initialized
  90. * with the function createSuperpixelSLIC(). The algorithms starts from a grid of superpixels and
  91. * then refines the boundaries by proposing updates of edges boundaries.
  92. */
  93. public void iterate()
  94. {
  95. ThrowIfDisposed();
  96. ximgproc_SuperpixelSLIC_iterate_11(nativeObj);
  97. }
  98. //
  99. // C++: void cv::ximgproc::SuperpixelSLIC::getLabels(Mat& labels_out)
  100. //
  101. /**
  102. * Returns the segmentation labeling of the image.
  103. *
  104. * Each label represents a superpixel, and each pixel is assigned to one superpixel label.
  105. *
  106. * param labels_out Return: A CV_32SC1 integer array containing the labels of the superpixel
  107. * segmentation. The labels are in the range [0, getNumberOfSuperpixels()].
  108. *
  109. * The function returns an image with the labels of the superpixel segmentation. The labels are in
  110. * the range [0, getNumberOfSuperpixels()].
  111. */
  112. public void getLabels(Mat labels_out)
  113. {
  114. ThrowIfDisposed();
  115. if (labels_out != null) labels_out.ThrowIfDisposed();
  116. ximgproc_SuperpixelSLIC_getLabels_10(nativeObj, labels_out.nativeObj);
  117. }
  118. //
  119. // C++: void cv::ximgproc::SuperpixelSLIC::getLabelContourMask(Mat& image, bool thick_line = true)
  120. //
  121. /**
  122. * Returns the mask of the superpixel segmentation stored in SuperpixelSLIC object.
  123. *
  124. * param image Return: CV_8U1 image mask where -1 indicates that the pixel is a superpixel border,
  125. * and 0 otherwise.
  126. *
  127. * param thick_line If false, the border is only one pixel wide, otherwise all pixels at the border
  128. * are masked.
  129. *
  130. * The function return the boundaries of the superpixel segmentation.
  131. */
  132. public void getLabelContourMask(Mat image, bool thick_line)
  133. {
  134. ThrowIfDisposed();
  135. if (image != null) image.ThrowIfDisposed();
  136. ximgproc_SuperpixelSLIC_getLabelContourMask_10(nativeObj, image.nativeObj, thick_line);
  137. }
  138. /**
  139. * Returns the mask of the superpixel segmentation stored in SuperpixelSLIC object.
  140. *
  141. * param image Return: CV_8U1 image mask where -1 indicates that the pixel is a superpixel border,
  142. * and 0 otherwise.
  143. *
  144. * are masked.
  145. *
  146. * The function return the boundaries of the superpixel segmentation.
  147. */
  148. public void getLabelContourMask(Mat image)
  149. {
  150. ThrowIfDisposed();
  151. if (image != null) image.ThrowIfDisposed();
  152. ximgproc_SuperpixelSLIC_getLabelContourMask_11(nativeObj, image.nativeObj);
  153. }
  154. //
  155. // C++: void cv::ximgproc::SuperpixelSLIC::enforceLabelConnectivity(int min_element_size = 25)
  156. //
  157. /**
  158. * Enforce label connectivity.
  159. *
  160. * param min_element_size The minimum element size in percents that should be absorbed into a bigger
  161. * superpixel. Given resulted average superpixel size valid value should be in 0-100 range, 25 means
  162. * that less then a quarter sized superpixel should be absorbed, this is default.
  163. *
  164. * The function merge component that is too small, assigning the previously found adjacent label
  165. * to this component. Calling this function may change the final number of superpixels.
  166. */
  167. public void enforceLabelConnectivity(int min_element_size)
  168. {
  169. ThrowIfDisposed();
  170. ximgproc_SuperpixelSLIC_enforceLabelConnectivity_10(nativeObj, min_element_size);
  171. }
  172. /**
  173. * Enforce label connectivity.
  174. *
  175. * superpixel. Given resulted average superpixel size valid value should be in 0-100 range, 25 means
  176. * that less then a quarter sized superpixel should be absorbed, this is default.
  177. *
  178. * The function merge component that is too small, assigning the previously found adjacent label
  179. * to this component. Calling this function may change the final number of superpixels.
  180. */
  181. public void enforceLabelConnectivity()
  182. {
  183. ThrowIfDisposed();
  184. ximgproc_SuperpixelSLIC_enforceLabelConnectivity_11(nativeObj);
  185. }
  186. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  187. const string LIBNAME = "__Internal";
  188. #else
  189. const string LIBNAME = "opencvforunity";
  190. #endif
  191. // C++: int cv::ximgproc::SuperpixelSLIC::getNumberOfSuperpixels()
  192. [DllImport(LIBNAME)]
  193. private static extern int ximgproc_SuperpixelSLIC_getNumberOfSuperpixels_10(IntPtr nativeObj);
  194. // C++: void cv::ximgproc::SuperpixelSLIC::iterate(int num_iterations = 10)
  195. [DllImport(LIBNAME)]
  196. private static extern void ximgproc_SuperpixelSLIC_iterate_10(IntPtr nativeObj, int num_iterations);
  197. [DllImport(LIBNAME)]
  198. private static extern void ximgproc_SuperpixelSLIC_iterate_11(IntPtr nativeObj);
  199. // C++: void cv::ximgproc::SuperpixelSLIC::getLabels(Mat& labels_out)
  200. [DllImport(LIBNAME)]
  201. private static extern void ximgproc_SuperpixelSLIC_getLabels_10(IntPtr nativeObj, IntPtr labels_out_nativeObj);
  202. // C++: void cv::ximgproc::SuperpixelSLIC::getLabelContourMask(Mat& image, bool thick_line = true)
  203. [DllImport(LIBNAME)]
  204. private static extern void ximgproc_SuperpixelSLIC_getLabelContourMask_10(IntPtr nativeObj, IntPtr image_nativeObj, [MarshalAs(UnmanagedType.U1)] bool thick_line);
  205. [DllImport(LIBNAME)]
  206. private static extern void ximgproc_SuperpixelSLIC_getLabelContourMask_11(IntPtr nativeObj, IntPtr image_nativeObj);
  207. // C++: void cv::ximgproc::SuperpixelSLIC::enforceLabelConnectivity(int min_element_size = 25)
  208. [DllImport(LIBNAME)]
  209. private static extern void ximgproc_SuperpixelSLIC_enforceLabelConnectivity_10(IntPtr nativeObj, int min_element_size);
  210. [DllImport(LIBNAME)]
  211. private static extern void ximgproc_SuperpixelSLIC_enforceLabelConnectivity_11(IntPtr nativeObj);
  212. // native support for java finalize()
  213. [DllImport(LIBNAME)]
  214. private static extern void ximgproc_SuperpixelSLIC_delete(IntPtr nativeObj);
  215. }
  216. }