BackgroundSubtractor.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.UtilsModule;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace OpenCVForUnity.VideoModule
  7. {
  8. // C++: class BackgroundSubtractor
  9. /**
  10. * Base class for background/foreground segmentation. :
  11. *
  12. * The class is only used to define the common interface for the whole family of background/foreground
  13. * segmentation algorithms.
  14. */
  15. public class BackgroundSubtractor : Algorithm
  16. {
  17. protected override void Dispose(bool disposing)
  18. {
  19. try
  20. {
  21. if (disposing)
  22. {
  23. }
  24. if (IsEnabledDispose)
  25. {
  26. if (nativeObj != IntPtr.Zero)
  27. video_BackgroundSubtractor_delete(nativeObj);
  28. nativeObj = IntPtr.Zero;
  29. }
  30. }
  31. finally
  32. {
  33. base.Dispose(disposing);
  34. }
  35. }
  36. protected internal BackgroundSubtractor(IntPtr addr) : base(addr) { }
  37. // internal usage only
  38. public static new BackgroundSubtractor __fromPtr__(IntPtr addr) { return new BackgroundSubtractor(addr); }
  39. //
  40. // C++: void cv::BackgroundSubtractor::apply(Mat image, Mat& fgmask, double learningRate = -1)
  41. //
  42. /**
  43. * Computes a foreground mask.
  44. *
  45. * param image Next video frame.
  46. * param fgmask The output foreground mask as an 8-bit binary image.
  47. * param learningRate The value between 0 and 1 that indicates how fast the background model is
  48. * learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
  49. * rate. 0 means that the background model is not updated at all, 1 means that the background model
  50. * is completely reinitialized from the last frame.
  51. */
  52. public virtual void apply(Mat image, Mat fgmask, double learningRate)
  53. {
  54. ThrowIfDisposed();
  55. if (image != null) image.ThrowIfDisposed();
  56. if (fgmask != null) fgmask.ThrowIfDisposed();
  57. video_BackgroundSubtractor_apply_10(nativeObj, image.nativeObj, fgmask.nativeObj, learningRate);
  58. }
  59. /**
  60. * Computes a foreground mask.
  61. *
  62. * param image Next video frame.
  63. * param fgmask The output foreground mask as an 8-bit binary image.
  64. * learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
  65. * rate. 0 means that the background model is not updated at all, 1 means that the background model
  66. * is completely reinitialized from the last frame.
  67. */
  68. public virtual void apply(Mat image, Mat fgmask)
  69. {
  70. ThrowIfDisposed();
  71. if (image != null) image.ThrowIfDisposed();
  72. if (fgmask != null) fgmask.ThrowIfDisposed();
  73. video_BackgroundSubtractor_apply_11(nativeObj, image.nativeObj, fgmask.nativeObj);
  74. }
  75. //
  76. // C++: void cv::BackgroundSubtractor::getBackgroundImage(Mat& backgroundImage)
  77. //
  78. /**
  79. * Computes a background image.
  80. *
  81. * param backgroundImage The output background image.
  82. *
  83. * <b>Note:</b> Sometimes the background image can be very blurry, as it contain the average background
  84. * statistics.
  85. */
  86. public virtual void getBackgroundImage(Mat backgroundImage)
  87. {
  88. ThrowIfDisposed();
  89. if (backgroundImage != null) backgroundImage.ThrowIfDisposed();
  90. video_BackgroundSubtractor_getBackgroundImage_10(nativeObj, backgroundImage.nativeObj);
  91. }
  92. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  93. const string LIBNAME = "__Internal";
  94. #else
  95. const string LIBNAME = "opencvforunity";
  96. #endif
  97. // C++: void cv::BackgroundSubtractor::apply(Mat image, Mat& fgmask, double learningRate = -1)
  98. [DllImport(LIBNAME)]
  99. private static extern void video_BackgroundSubtractor_apply_10(IntPtr nativeObj, IntPtr image_nativeObj, IntPtr fgmask_nativeObj, double learningRate);
  100. [DllImport(LIBNAME)]
  101. private static extern void video_BackgroundSubtractor_apply_11(IntPtr nativeObj, IntPtr image_nativeObj, IntPtr fgmask_nativeObj);
  102. // C++: void cv::BackgroundSubtractor::getBackgroundImage(Mat& backgroundImage)
  103. [DllImport(LIBNAME)]
  104. private static extern void video_BackgroundSubtractor_getBackgroundImage_10(IntPtr nativeObj, IntPtr backgroundImage_nativeObj);
  105. // native support for java finalize()
  106. [DllImport(LIBNAME)]
  107. private static extern void video_BackgroundSubtractor_delete(IntPtr nativeObj);
  108. }
  109. }