SimpleBlobDetector.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.UtilsModule;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace OpenCVForUnity.Features2dModule
  7. {
  8. // C++: class SimpleBlobDetector
  9. /**
  10. * Class for extracting blobs from an image. :
  11. *
  12. * The class implements a simple algorithm for extracting blobs from an image:
  13. *
  14. * 1. Convert the source image to binary images by applying thresholding with several thresholds from
  15. * minThreshold (inclusive) to maxThreshold (exclusive) with distance thresholdStep between
  16. * neighboring thresholds.
  17. * 2. Extract connected components from every binary image by findContours and calculate their
  18. * centers.
  19. * 3. Group centers from several binary images by their coordinates. Close centers form one group that
  20. * corresponds to one blob, which is controlled by the minDistBetweenBlobs parameter.
  21. * 4. From the groups, estimate final centers of blobs and their radiuses and return as locations and
  22. * sizes of keypoints.
  23. *
  24. * This class performs several filtrations of returned blobs. You should set filterBy\* to true/false
  25. * to turn on/off corresponding filtration. Available filtrations:
  26. *
  27. * <ul>
  28. * <li>
  29. * <b>By color</b>. This filter compares the intensity of a binary image at the center of a blob to
  30. * blobColor. If they differ, the blob is filtered out. Use blobColor = 0 to extract dark blobs
  31. * and blobColor = 255 to extract light blobs.
  32. * </li>
  33. * <li>
  34. * <b>By area</b>. Extracted blobs have an area between minArea (inclusive) and maxArea (exclusive).
  35. * </li>
  36. * <li>
  37. * <b>By circularity</b>. Extracted blobs have circularity
  38. * (\(\frac{4*\pi*Area}{perimeter * perimeter}\)) between minCircularity (inclusive) and
  39. * maxCircularity (exclusive).
  40. * </li>
  41. * <li>
  42. * <b>By ratio of the minimum inertia to maximum inertia</b>. Extracted blobs have this ratio
  43. * between minInertiaRatio (inclusive) and maxInertiaRatio (exclusive).
  44. * </li>
  45. * <li>
  46. * <b>By convexity</b>. Extracted blobs have convexity (area / area of blob convex hull) between
  47. * minConvexity (inclusive) and maxConvexity (exclusive).
  48. * </li>
  49. * </ul>
  50. *
  51. * Default values of parameters are tuned to extract dark circular blobs.
  52. */
  53. public class SimpleBlobDetector : Feature2D
  54. {
  55. protected override void Dispose(bool disposing)
  56. {
  57. try
  58. {
  59. if (disposing)
  60. {
  61. }
  62. if (IsEnabledDispose)
  63. {
  64. if (nativeObj != IntPtr.Zero)
  65. features2d_SimpleBlobDetector_delete(nativeObj);
  66. nativeObj = IntPtr.Zero;
  67. }
  68. }
  69. finally
  70. {
  71. base.Dispose(disposing);
  72. }
  73. }
  74. protected internal SimpleBlobDetector(IntPtr addr) : base(addr) { }
  75. // internal usage only
  76. public static new SimpleBlobDetector __fromPtr__(IntPtr addr) { return new SimpleBlobDetector(addr); }
  77. //
  78. // C++: static Ptr_SimpleBlobDetector cv::SimpleBlobDetector::create(SimpleBlobDetector_Params parameters = SimpleBlobDetector::Params())
  79. //
  80. public static SimpleBlobDetector create(SimpleBlobDetector_Params parameters)
  81. {
  82. if (parameters != null) parameters.ThrowIfDisposed();
  83. return SimpleBlobDetector.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(features2d_SimpleBlobDetector_create_10(parameters.nativeObj)));
  84. }
  85. public static SimpleBlobDetector create()
  86. {
  87. return SimpleBlobDetector.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(features2d_SimpleBlobDetector_create_11()));
  88. }
  89. //
  90. // C++: void cv::SimpleBlobDetector::setParams(SimpleBlobDetector_Params _params)
  91. //
  92. public void setParams(SimpleBlobDetector_Params _params)
  93. {
  94. ThrowIfDisposed();
  95. if (_params != null) _params.ThrowIfDisposed();
  96. features2d_SimpleBlobDetector_setParams_10(nativeObj, _params.nativeObj);
  97. }
  98. //
  99. // C++: SimpleBlobDetector_Params cv::SimpleBlobDetector::getParams()
  100. //
  101. public SimpleBlobDetector_Params getParams()
  102. {
  103. ThrowIfDisposed();
  104. return new SimpleBlobDetector_Params(DisposableObject.ThrowIfNullIntPtr(features2d_SimpleBlobDetector_getParams_10(nativeObj)));
  105. }
  106. //
  107. // C++: String cv::SimpleBlobDetector::getDefaultName()
  108. //
  109. public override string getDefaultName()
  110. {
  111. ThrowIfDisposed();
  112. string retVal = Marshal.PtrToStringAnsi(DisposableObject.ThrowIfNullIntPtr(features2d_SimpleBlobDetector_getDefaultName_10(nativeObj)));
  113. return retVal;
  114. }
  115. //
  116. // C++: vector_vector_Point cv::SimpleBlobDetector::getBlobContours()
  117. //
  118. public List<MatOfPoint> getBlobContours()
  119. {
  120. ThrowIfDisposed();
  121. List<MatOfPoint> retVal = new List<MatOfPoint>();
  122. Mat retValMat = new Mat(DisposableObject.ThrowIfNullIntPtr(features2d_SimpleBlobDetector_getBlobContours_10(nativeObj)));
  123. Converters.Mat_to_vector_vector_Point(retValMat, retVal);
  124. return retVal;
  125. }
  126. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  127. const string LIBNAME = "__Internal";
  128. #else
  129. const string LIBNAME = "opencvforunity";
  130. #endif
  131. // C++: static Ptr_SimpleBlobDetector cv::SimpleBlobDetector::create(SimpleBlobDetector_Params parameters = SimpleBlobDetector::Params())
  132. [DllImport(LIBNAME)]
  133. private static extern IntPtr features2d_SimpleBlobDetector_create_10(IntPtr parameters_nativeObj);
  134. [DllImport(LIBNAME)]
  135. private static extern IntPtr features2d_SimpleBlobDetector_create_11();
  136. // C++: void cv::SimpleBlobDetector::setParams(SimpleBlobDetector_Params _params)
  137. [DllImport(LIBNAME)]
  138. private static extern void features2d_SimpleBlobDetector_setParams_10(IntPtr nativeObj, IntPtr _params_nativeObj);
  139. // C++: SimpleBlobDetector_Params cv::SimpleBlobDetector::getParams()
  140. [DllImport(LIBNAME)]
  141. private static extern IntPtr features2d_SimpleBlobDetector_getParams_10(IntPtr nativeObj);
  142. // C++: String cv::SimpleBlobDetector::getDefaultName()
  143. [DllImport(LIBNAME)]
  144. private static extern IntPtr features2d_SimpleBlobDetector_getDefaultName_10(IntPtr nativeObj);
  145. // C++: vector_vector_Point cv::SimpleBlobDetector::getBlobContours()
  146. [DllImport(LIBNAME)]
  147. private static extern IntPtr features2d_SimpleBlobDetector_getBlobContours_10(IntPtr nativeObj);
  148. // native support for java finalize()
  149. [DllImport(LIBNAME)]
  150. private static extern void features2d_SimpleBlobDetector_delete(IntPtr nativeObj);
  151. }
  152. }