ScanSegment.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 ScanSegment
  9. /**
  10. * Class implementing the F-DBSCAN (Accelerated superpixel image segmentation with a parallelized DBSCAN algorithm) superpixels
  11. * algorithm by Loke SC, et al. CITE: loke2021accelerated for original paper.
  12. *
  13. * The algorithm uses a parallelised DBSCAN cluster search that is resistant to noise, competitive in segmentation quality, and faster than
  14. * existing superpixel segmentation methods. When tested on the Berkeley Segmentation Dataset, the average processing speed is 175 frames/s
  15. * with a Boundary Recall of 0.797 and an Achievable Segmentation Accuracy of 0.944. The computational complexity is quadratic O(n2) and
  16. * more suited to smaller images, but can still process a 2MP colour image faster than the SEEDS algorithm in OpenCV. The output is deterministic
  17. * when the number of processing threads is fixed, and requires the source image to be in Lab colour format.
  18. */
  19. public class ScanSegment : 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_ScanSegment_delete(nativeObj);
  32. nativeObj = IntPtr.Zero;
  33. }
  34. }
  35. finally
  36. {
  37. base.Dispose(disposing);
  38. }
  39. }
  40. protected internal ScanSegment(IntPtr addr) : base(addr) { }
  41. // internal usage only
  42. public static new ScanSegment __fromPtr__(IntPtr addr) { return new ScanSegment(addr); }
  43. //
  44. // C++: int cv::ximgproc::ScanSegment::getNumberOfSuperpixels()
  45. //
  46. /**
  47. * Returns the actual superpixel segmentation from the last image processed using iterate.
  48. *
  49. * Returns zero if no image has been processed.
  50. * return automatically generated
  51. */
  52. public int getNumberOfSuperpixels()
  53. {
  54. ThrowIfDisposed();
  55. return ximgproc_ScanSegment_getNumberOfSuperpixels_10(nativeObj);
  56. }
  57. //
  58. // C++: void cv::ximgproc::ScanSegment::iterate(Mat img)
  59. //
  60. /**
  61. * Calculates the superpixel segmentation on a given image with the initialized
  62. * parameters in the ScanSegment object.
  63. *
  64. * This function can be called again for other images without the need of initializing the algorithm with createScanSegment().
  65. * This save the computational cost of allocating memory for all the structures of the algorithm.
  66. *
  67. * param img Input image. Supported format: CV_8UC3. Image size must match with the initialized
  68. * image size with the function createScanSegment(). It MUST be in Lab color space.
  69. */
  70. public void iterate(Mat img)
  71. {
  72. ThrowIfDisposed();
  73. if (img != null) img.ThrowIfDisposed();
  74. ximgproc_ScanSegment_iterate_10(nativeObj, img.nativeObj);
  75. }
  76. //
  77. // C++: void cv::ximgproc::ScanSegment::getLabels(Mat& labels_out)
  78. //
  79. /**
  80. * Returns the segmentation labeling of the image.
  81. *
  82. * Each label represents a superpixel, and each pixel is assigned to one superpixel label.
  83. *
  84. * param labels_out Return: A CV_32UC1 integer array containing the labels of the superpixel
  85. * segmentation. The labels are in the range [0, getNumberOfSuperpixels()].
  86. */
  87. public void getLabels(Mat labels_out)
  88. {
  89. ThrowIfDisposed();
  90. if (labels_out != null) labels_out.ThrowIfDisposed();
  91. ximgproc_ScanSegment_getLabels_10(nativeObj, labels_out.nativeObj);
  92. }
  93. //
  94. // C++: void cv::ximgproc::ScanSegment::getLabelContourMask(Mat& image, bool thick_line = false)
  95. //
  96. /**
  97. * Returns the mask of the superpixel segmentation stored in the ScanSegment object.
  98. *
  99. * The function return the boundaries of the superpixel segmentation.
  100. *
  101. * param image Return: CV_8UC1 image mask where -1 indicates that the pixel is a superpixel border, and 0 otherwise.
  102. * param thick_line If false, the border is only one pixel wide, otherwise all pixels at the border are masked.
  103. */
  104. public void getLabelContourMask(Mat image, bool thick_line)
  105. {
  106. ThrowIfDisposed();
  107. if (image != null) image.ThrowIfDisposed();
  108. ximgproc_ScanSegment_getLabelContourMask_10(nativeObj, image.nativeObj, thick_line);
  109. }
  110. /**
  111. * Returns the mask of the superpixel segmentation stored in the ScanSegment object.
  112. *
  113. * The function return the boundaries of the superpixel segmentation.
  114. *
  115. * param image Return: CV_8UC1 image mask where -1 indicates that the pixel is a superpixel border, and 0 otherwise.
  116. */
  117. public void getLabelContourMask(Mat image)
  118. {
  119. ThrowIfDisposed();
  120. if (image != null) image.ThrowIfDisposed();
  121. ximgproc_ScanSegment_getLabelContourMask_11(nativeObj, image.nativeObj);
  122. }
  123. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  124. const string LIBNAME = "__Internal";
  125. #else
  126. const string LIBNAME = "opencvforunity";
  127. #endif
  128. // C++: int cv::ximgproc::ScanSegment::getNumberOfSuperpixels()
  129. [DllImport(LIBNAME)]
  130. private static extern int ximgproc_ScanSegment_getNumberOfSuperpixels_10(IntPtr nativeObj);
  131. // C++: void cv::ximgproc::ScanSegment::iterate(Mat img)
  132. [DllImport(LIBNAME)]
  133. private static extern void ximgproc_ScanSegment_iterate_10(IntPtr nativeObj, IntPtr img_nativeObj);
  134. // C++: void cv::ximgproc::ScanSegment::getLabels(Mat& labels_out)
  135. [DllImport(LIBNAME)]
  136. private static extern void ximgproc_ScanSegment_getLabels_10(IntPtr nativeObj, IntPtr labels_out_nativeObj);
  137. // C++: void cv::ximgproc::ScanSegment::getLabelContourMask(Mat& image, bool thick_line = false)
  138. [DllImport(LIBNAME)]
  139. private static extern void ximgproc_ScanSegment_getLabelContourMask_10(IntPtr nativeObj, IntPtr image_nativeObj, [MarshalAs(UnmanagedType.U1)] bool thick_line);
  140. [DllImport(LIBNAME)]
  141. private static extern void ximgproc_ScanSegment_getLabelContourMask_11(IntPtr nativeObj, IntPtr image_nativeObj);
  142. // native support for java finalize()
  143. [DllImport(LIBNAME)]
  144. private static extern void ximgproc_ScanSegment_delete(IntPtr nativeObj);
  145. }
  146. }