SuperpixelLSC.cs 10.0 KB

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