SuperpixelSEEDS.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 SuperpixelSEEDS
  9. /**
  10. * Class implementing the SEEDS (Superpixels Extracted via Energy-Driven Sampling) superpixels
  11. * algorithm described in CITE: VBRV14 .
  12. *
  13. * The algorithm uses an efficient hill-climbing algorithm to optimize the superpixels' energy
  14. * function that is based on color histograms and a boundary term, which is optional. The energy
  15. * function encourages superpixels to be of the same color, and if the boundary term is activated, the
  16. * superpixels have smooth boundaries and are of similar shape. In practice it starts from a regular
  17. * grid of superpixels and moves the pixels or blocks of pixels at the boundaries to refine the
  18. * solution. The algorithm runs in real-time using a single CPU.
  19. */
  20. public class SuperpixelSEEDS : Algorithm
  21. {
  22. protected override void Dispose(bool disposing)
  23. {
  24. try
  25. {
  26. if (disposing)
  27. {
  28. }
  29. if (IsEnabledDispose)
  30. {
  31. if (nativeObj != IntPtr.Zero)
  32. ximgproc_SuperpixelSEEDS_delete(nativeObj);
  33. nativeObj = IntPtr.Zero;
  34. }
  35. }
  36. finally
  37. {
  38. base.Dispose(disposing);
  39. }
  40. }
  41. protected internal SuperpixelSEEDS(IntPtr addr) : base(addr) { }
  42. // internal usage only
  43. public static new SuperpixelSEEDS __fromPtr__(IntPtr addr) { return new SuperpixelSEEDS(addr); }
  44. //
  45. // C++: int cv::ximgproc::SuperpixelSEEDS::getNumberOfSuperpixels()
  46. //
  47. /**
  48. * Calculates the superpixel segmentation on a given image stored in SuperpixelSEEDS object.
  49. *
  50. * The function computes the superpixels segmentation of an image with the parameters initialized
  51. * with the function createSuperpixelSEEDS().
  52. * return automatically generated
  53. */
  54. public int getNumberOfSuperpixels()
  55. {
  56. ThrowIfDisposed();
  57. return ximgproc_SuperpixelSEEDS_getNumberOfSuperpixels_10(nativeObj);
  58. }
  59. //
  60. // C++: void cv::ximgproc::SuperpixelSEEDS::iterate(Mat img, int num_iterations = 4)
  61. //
  62. /**
  63. * Calculates the superpixel segmentation on a given image with the initialized
  64. * parameters in the SuperpixelSEEDS object.
  65. *
  66. * This function can be called again for other images without the need of initializing the
  67. * algorithm with createSuperpixelSEEDS(). This save the computational cost of allocating memory
  68. * for all the structures of the algorithm.
  69. *
  70. * param img Input image. Supported formats: CV_8U, CV_16U, CV_32F. Image size & number of
  71. * channels must match with the initialized image size & channels with the function
  72. * createSuperpixelSEEDS(). It should be in HSV or Lab color space. Lab is a bit better, but also
  73. * slower.
  74. *
  75. * param num_iterations Number of pixel level iterations. Higher number improves the result.
  76. *
  77. * The function computes the superpixels segmentation of an image with the parameters initialized
  78. * with the function createSuperpixelSEEDS(). The algorithms starts from a grid of superpixels and
  79. * then refines the boundaries by proposing updates of blocks of pixels that lie at the boundaries
  80. * from large to smaller size, finalizing with proposing pixel updates. An illustrative example
  81. * can be seen below.
  82. *
  83. * ![image](pics/superpixels_blocks2.png)
  84. */
  85. public void iterate(Mat img, int num_iterations)
  86. {
  87. ThrowIfDisposed();
  88. if (img != null) img.ThrowIfDisposed();
  89. ximgproc_SuperpixelSEEDS_iterate_10(nativeObj, img.nativeObj, num_iterations);
  90. }
  91. /**
  92. * Calculates the superpixel segmentation on a given image with the initialized
  93. * parameters in the SuperpixelSEEDS object.
  94. *
  95. * This function can be called again for other images without the need of initializing the
  96. * algorithm with createSuperpixelSEEDS(). This save the computational cost of allocating memory
  97. * for all the structures of the algorithm.
  98. *
  99. * param img Input image. Supported formats: CV_8U, CV_16U, CV_32F. Image size & number of
  100. * channels must match with the initialized image size & channels with the function
  101. * createSuperpixelSEEDS(). It should be in HSV or Lab color space. Lab is a bit better, but also
  102. * slower.
  103. *
  104. *
  105. * The function computes the superpixels segmentation of an image with the parameters initialized
  106. * with the function createSuperpixelSEEDS(). The algorithms starts from a grid of superpixels and
  107. * then refines the boundaries by proposing updates of blocks of pixels that lie at the boundaries
  108. * from large to smaller size, finalizing with proposing pixel updates. An illustrative example
  109. * can be seen below.
  110. *
  111. * ![image](pics/superpixels_blocks2.png)
  112. */
  113. public void iterate(Mat img)
  114. {
  115. ThrowIfDisposed();
  116. if (img != null) img.ThrowIfDisposed();
  117. ximgproc_SuperpixelSEEDS_iterate_11(nativeObj, img.nativeObj);
  118. }
  119. //
  120. // C++: void cv::ximgproc::SuperpixelSEEDS::getLabels(Mat& labels_out)
  121. //
  122. /**
  123. * Returns the segmentation labeling of the image.
  124. *
  125. * Each label represents a superpixel, and each pixel is assigned to one superpixel label.
  126. *
  127. * param labels_out Return: A CV_32UC1 integer array containing the labels of the superpixel
  128. * segmentation. The labels are in the range [0, getNumberOfSuperpixels()].
  129. *
  130. * The function returns an image with ssthe labels of the superpixel segmentation. The labels are in
  131. * the range [0, getNumberOfSuperpixels()].
  132. */
  133. public void getLabels(Mat labels_out)
  134. {
  135. ThrowIfDisposed();
  136. if (labels_out != null) labels_out.ThrowIfDisposed();
  137. ximgproc_SuperpixelSEEDS_getLabels_10(nativeObj, labels_out.nativeObj);
  138. }
  139. //
  140. // C++: void cv::ximgproc::SuperpixelSEEDS::getLabelContourMask(Mat& image, bool thick_line = false)
  141. //
  142. /**
  143. * Returns the mask of the superpixel segmentation stored in SuperpixelSEEDS object.
  144. *
  145. * param image Return: CV_8UC1 image mask where -1 indicates that the pixel is a superpixel border,
  146. * and 0 otherwise.
  147. *
  148. * param thick_line If false, the border is only one pixel wide, otherwise all pixels at the border
  149. * are masked.
  150. *
  151. * The function return the boundaries of the superpixel segmentation.
  152. *
  153. * <b>Note:</b>
  154. * <ul>
  155. * <li>
  156. * (Python) A demo on how to generate superpixels in images from the webcam can be found at
  157. * opencv_source_code/samples/python2/seeds.py
  158. * <ul>
  159. * <li>
  160. * (cpp) A demo on how to generate superpixels in images from the webcam can be found at
  161. * opencv_source_code/modules/ximgproc/samples/seeds.cpp. By adding a file image as a command
  162. * line argument, the static image will be used instead of the webcam.
  163. * </li>
  164. * <li>
  165. * It will show a window with the video from the webcam with the superpixel boundaries marked
  166. * in red (see below). Use Space to switch between different output modes. At the top of the
  167. * window there are 4 sliders, from which the user can change on-the-fly the number of
  168. * superpixels, the number of block levels, the strength of the boundary prior term to modify
  169. * the shape, and the number of iterations at pixel level. This is useful to play with the
  170. * parameters and set them to the user convenience. In the console the frame-rate of the
  171. * algorithm is indicated.
  172. * </li>
  173. * </ul>
  174. *
  175. * ![image](pics/superpixels_demo.png)
  176. * </li>
  177. * </ul>
  178. */
  179. public void getLabelContourMask(Mat image, bool thick_line)
  180. {
  181. ThrowIfDisposed();
  182. if (image != null) image.ThrowIfDisposed();
  183. ximgproc_SuperpixelSEEDS_getLabelContourMask_10(nativeObj, image.nativeObj, thick_line);
  184. }
  185. /**
  186. * Returns the mask of the superpixel segmentation stored in SuperpixelSEEDS object.
  187. *
  188. * param image Return: CV_8UC1 image mask where -1 indicates that the pixel is a superpixel border,
  189. * and 0 otherwise.
  190. *
  191. * are masked.
  192. *
  193. * The function return the boundaries of the superpixel segmentation.
  194. *
  195. * <b>Note:</b>
  196. * <ul>
  197. * <li>
  198. * (Python) A demo on how to generate superpixels in images from the webcam can be found at
  199. * opencv_source_code/samples/python2/seeds.py
  200. * <ul>
  201. * <li>
  202. * (cpp) A demo on how to generate superpixels in images from the webcam can be found at
  203. * opencv_source_code/modules/ximgproc/samples/seeds.cpp. By adding a file image as a command
  204. * line argument, the static image will be used instead of the webcam.
  205. * </li>
  206. * <li>
  207. * It will show a window with the video from the webcam with the superpixel boundaries marked
  208. * in red (see below). Use Space to switch between different output modes. At the top of the
  209. * window there are 4 sliders, from which the user can change on-the-fly the number of
  210. * superpixels, the number of block levels, the strength of the boundary prior term to modify
  211. * the shape, and the number of iterations at pixel level. This is useful to play with the
  212. * parameters and set them to the user convenience. In the console the frame-rate of the
  213. * algorithm is indicated.
  214. * </li>
  215. * </ul>
  216. *
  217. * ![image](pics/superpixels_demo.png)
  218. * </li>
  219. * </ul>
  220. */
  221. public void getLabelContourMask(Mat image)
  222. {
  223. ThrowIfDisposed();
  224. if (image != null) image.ThrowIfDisposed();
  225. ximgproc_SuperpixelSEEDS_getLabelContourMask_11(nativeObj, image.nativeObj);
  226. }
  227. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  228. const string LIBNAME = "__Internal";
  229. #else
  230. const string LIBNAME = "opencvforunity";
  231. #endif
  232. // C++: int cv::ximgproc::SuperpixelSEEDS::getNumberOfSuperpixels()
  233. [DllImport(LIBNAME)]
  234. private static extern int ximgproc_SuperpixelSEEDS_getNumberOfSuperpixels_10(IntPtr nativeObj);
  235. // C++: void cv::ximgproc::SuperpixelSEEDS::iterate(Mat img, int num_iterations = 4)
  236. [DllImport(LIBNAME)]
  237. private static extern void ximgproc_SuperpixelSEEDS_iterate_10(IntPtr nativeObj, IntPtr img_nativeObj, int num_iterations);
  238. [DllImport(LIBNAME)]
  239. private static extern void ximgproc_SuperpixelSEEDS_iterate_11(IntPtr nativeObj, IntPtr img_nativeObj);
  240. // C++: void cv::ximgproc::SuperpixelSEEDS::getLabels(Mat& labels_out)
  241. [DllImport(LIBNAME)]
  242. private static extern void ximgproc_SuperpixelSEEDS_getLabels_10(IntPtr nativeObj, IntPtr labels_out_nativeObj);
  243. // C++: void cv::ximgproc::SuperpixelSEEDS::getLabelContourMask(Mat& image, bool thick_line = false)
  244. [DllImport(LIBNAME)]
  245. private static extern void ximgproc_SuperpixelSEEDS_getLabelContourMask_10(IntPtr nativeObj, IntPtr image_nativeObj, [MarshalAs(UnmanagedType.U1)] bool thick_line);
  246. [DllImport(LIBNAME)]
  247. private static extern void ximgproc_SuperpixelSEEDS_getLabelContourMask_11(IntPtr nativeObj, IntPtr image_nativeObj);
  248. // native support for java finalize()
  249. [DllImport(LIBNAME)]
  250. private static extern void ximgproc_SuperpixelSEEDS_delete(IntPtr nativeObj);
  251. }
  252. }