BackgroundSubtractorMOG2.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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 BackgroundSubtractorMOG2
  9. /**
  10. * Gaussian Mixture-based Background/Foreground Segmentation Algorithm.
  11. *
  12. * The class implements the Gaussian mixture model background subtraction described in CITE: Zivkovic2004
  13. * and CITE: Zivkovic2006 .
  14. */
  15. public class BackgroundSubtractorMOG2 : BackgroundSubtractor
  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_BackgroundSubtractorMOG2_delete(nativeObj);
  28. nativeObj = IntPtr.Zero;
  29. }
  30. }
  31. finally
  32. {
  33. base.Dispose(disposing);
  34. }
  35. }
  36. protected internal BackgroundSubtractorMOG2(IntPtr addr) : base(addr) { }
  37. // internal usage only
  38. public static new BackgroundSubtractorMOG2 __fromPtr__(IntPtr addr) { return new BackgroundSubtractorMOG2(addr); }
  39. //
  40. // C++: int cv::BackgroundSubtractorMOG2::getHistory()
  41. //
  42. /**
  43. * Returns the number of last frames that affect the background model
  44. * return automatically generated
  45. */
  46. public int getHistory()
  47. {
  48. ThrowIfDisposed();
  49. return video_BackgroundSubtractorMOG2_getHistory_10(nativeObj);
  50. }
  51. //
  52. // C++: void cv::BackgroundSubtractorMOG2::setHistory(int history)
  53. //
  54. /**
  55. * Sets the number of last frames that affect the background model
  56. * param history automatically generated
  57. */
  58. public void setHistory(int history)
  59. {
  60. ThrowIfDisposed();
  61. video_BackgroundSubtractorMOG2_setHistory_10(nativeObj, history);
  62. }
  63. //
  64. // C++: int cv::BackgroundSubtractorMOG2::getNMixtures()
  65. //
  66. /**
  67. * Returns the number of gaussian components in the background model
  68. * return automatically generated
  69. */
  70. public int getNMixtures()
  71. {
  72. ThrowIfDisposed();
  73. return video_BackgroundSubtractorMOG2_getNMixtures_10(nativeObj);
  74. }
  75. //
  76. // C++: void cv::BackgroundSubtractorMOG2::setNMixtures(int nmixtures)
  77. //
  78. /**
  79. * Sets the number of gaussian components in the background model.
  80. *
  81. * The model needs to be reinitalized to reserve memory.
  82. * param nmixtures automatically generated
  83. */
  84. public void setNMixtures(int nmixtures)
  85. {
  86. ThrowIfDisposed();
  87. video_BackgroundSubtractorMOG2_setNMixtures_10(nativeObj, nmixtures);
  88. }
  89. //
  90. // C++: double cv::BackgroundSubtractorMOG2::getBackgroundRatio()
  91. //
  92. /**
  93. * Returns the "background ratio" parameter of the algorithm
  94. *
  95. * If a foreground pixel keeps semi-constant value for about backgroundRatio\*history frames, it's
  96. * considered background and added to the model as a center of a new component. It corresponds to TB
  97. * parameter in the paper.
  98. * return automatically generated
  99. */
  100. public double getBackgroundRatio()
  101. {
  102. ThrowIfDisposed();
  103. return video_BackgroundSubtractorMOG2_getBackgroundRatio_10(nativeObj);
  104. }
  105. //
  106. // C++: void cv::BackgroundSubtractorMOG2::setBackgroundRatio(double ratio)
  107. //
  108. /**
  109. * Sets the "background ratio" parameter of the algorithm
  110. * param ratio automatically generated
  111. */
  112. public void setBackgroundRatio(double ratio)
  113. {
  114. ThrowIfDisposed();
  115. video_BackgroundSubtractorMOG2_setBackgroundRatio_10(nativeObj, ratio);
  116. }
  117. //
  118. // C++: double cv::BackgroundSubtractorMOG2::getVarThreshold()
  119. //
  120. /**
  121. * Returns the variance threshold for the pixel-model match
  122. *
  123. * The main threshold on the squared Mahalanobis distance to decide if the sample is well described by
  124. * the background model or not. Related to Cthr from the paper.
  125. * return automatically generated
  126. */
  127. public double getVarThreshold()
  128. {
  129. ThrowIfDisposed();
  130. return video_BackgroundSubtractorMOG2_getVarThreshold_10(nativeObj);
  131. }
  132. //
  133. // C++: void cv::BackgroundSubtractorMOG2::setVarThreshold(double varThreshold)
  134. //
  135. /**
  136. * Sets the variance threshold for the pixel-model match
  137. * param varThreshold automatically generated
  138. */
  139. public void setVarThreshold(double varThreshold)
  140. {
  141. ThrowIfDisposed();
  142. video_BackgroundSubtractorMOG2_setVarThreshold_10(nativeObj, varThreshold);
  143. }
  144. //
  145. // C++: double cv::BackgroundSubtractorMOG2::getVarThresholdGen()
  146. //
  147. /**
  148. * Returns the variance threshold for the pixel-model match used for new mixture component generation
  149. *
  150. * Threshold for the squared Mahalanobis distance that helps decide when a sample is close to the
  151. * existing components (corresponds to Tg in the paper). If a pixel is not close to any component, it
  152. * is considered foreground or added as a new component. 3 sigma => Tg=3\*3=9 is default. A smaller Tg
  153. * value generates more components. A higher Tg value may result in a small number of components but
  154. * they can grow too large.
  155. * return automatically generated
  156. */
  157. public double getVarThresholdGen()
  158. {
  159. ThrowIfDisposed();
  160. return video_BackgroundSubtractorMOG2_getVarThresholdGen_10(nativeObj);
  161. }
  162. //
  163. // C++: void cv::BackgroundSubtractorMOG2::setVarThresholdGen(double varThresholdGen)
  164. //
  165. /**
  166. * Sets the variance threshold for the pixel-model match used for new mixture component generation
  167. * param varThresholdGen automatically generated
  168. */
  169. public void setVarThresholdGen(double varThresholdGen)
  170. {
  171. ThrowIfDisposed();
  172. video_BackgroundSubtractorMOG2_setVarThresholdGen_10(nativeObj, varThresholdGen);
  173. }
  174. //
  175. // C++: double cv::BackgroundSubtractorMOG2::getVarInit()
  176. //
  177. /**
  178. * Returns the initial variance of each gaussian component
  179. * return automatically generated
  180. */
  181. public double getVarInit()
  182. {
  183. ThrowIfDisposed();
  184. return video_BackgroundSubtractorMOG2_getVarInit_10(nativeObj);
  185. }
  186. //
  187. // C++: void cv::BackgroundSubtractorMOG2::setVarInit(double varInit)
  188. //
  189. /**
  190. * Sets the initial variance of each gaussian component
  191. * param varInit automatically generated
  192. */
  193. public void setVarInit(double varInit)
  194. {
  195. ThrowIfDisposed();
  196. video_BackgroundSubtractorMOG2_setVarInit_10(nativeObj, varInit);
  197. }
  198. //
  199. // C++: double cv::BackgroundSubtractorMOG2::getVarMin()
  200. //
  201. public double getVarMin()
  202. {
  203. ThrowIfDisposed();
  204. return video_BackgroundSubtractorMOG2_getVarMin_10(nativeObj);
  205. }
  206. //
  207. // C++: void cv::BackgroundSubtractorMOG2::setVarMin(double varMin)
  208. //
  209. public void setVarMin(double varMin)
  210. {
  211. ThrowIfDisposed();
  212. video_BackgroundSubtractorMOG2_setVarMin_10(nativeObj, varMin);
  213. }
  214. //
  215. // C++: double cv::BackgroundSubtractorMOG2::getVarMax()
  216. //
  217. public double getVarMax()
  218. {
  219. ThrowIfDisposed();
  220. return video_BackgroundSubtractorMOG2_getVarMax_10(nativeObj);
  221. }
  222. //
  223. // C++: void cv::BackgroundSubtractorMOG2::setVarMax(double varMax)
  224. //
  225. public void setVarMax(double varMax)
  226. {
  227. ThrowIfDisposed();
  228. video_BackgroundSubtractorMOG2_setVarMax_10(nativeObj, varMax);
  229. }
  230. //
  231. // C++: double cv::BackgroundSubtractorMOG2::getComplexityReductionThreshold()
  232. //
  233. /**
  234. * Returns the complexity reduction threshold
  235. *
  236. * This parameter defines the number of samples needed to accept to prove the component exists. CT=0.05
  237. * is a default value for all the samples. By setting CT=0 you get an algorithm very similar to the
  238. * standard Stauffer&Grimson algorithm.
  239. * return automatically generated
  240. */
  241. public double getComplexityReductionThreshold()
  242. {
  243. ThrowIfDisposed();
  244. return video_BackgroundSubtractorMOG2_getComplexityReductionThreshold_10(nativeObj);
  245. }
  246. //
  247. // C++: void cv::BackgroundSubtractorMOG2::setComplexityReductionThreshold(double ct)
  248. //
  249. /**
  250. * Sets the complexity reduction threshold
  251. * param ct automatically generated
  252. */
  253. public void setComplexityReductionThreshold(double ct)
  254. {
  255. ThrowIfDisposed();
  256. video_BackgroundSubtractorMOG2_setComplexityReductionThreshold_10(nativeObj, ct);
  257. }
  258. //
  259. // C++: bool cv::BackgroundSubtractorMOG2::getDetectShadows()
  260. //
  261. /**
  262. * Returns the shadow detection flag
  263. *
  264. * If true, the algorithm detects shadows and marks them. See createBackgroundSubtractorMOG2 for
  265. * details.
  266. * return automatically generated
  267. */
  268. public bool getDetectShadows()
  269. {
  270. ThrowIfDisposed();
  271. return video_BackgroundSubtractorMOG2_getDetectShadows_10(nativeObj);
  272. }
  273. //
  274. // C++: void cv::BackgroundSubtractorMOG2::setDetectShadows(bool detectShadows)
  275. //
  276. /**
  277. * Enables or disables shadow detection
  278. * param detectShadows automatically generated
  279. */
  280. public void setDetectShadows(bool detectShadows)
  281. {
  282. ThrowIfDisposed();
  283. video_BackgroundSubtractorMOG2_setDetectShadows_10(nativeObj, detectShadows);
  284. }
  285. //
  286. // C++: int cv::BackgroundSubtractorMOG2::getShadowValue()
  287. //
  288. /**
  289. * Returns the shadow value
  290. *
  291. * Shadow value is the value used to mark shadows in the foreground mask. Default value is 127. Value 0
  292. * in the mask always means background, 255 means foreground.
  293. * return automatically generated
  294. */
  295. public int getShadowValue()
  296. {
  297. ThrowIfDisposed();
  298. return video_BackgroundSubtractorMOG2_getShadowValue_10(nativeObj);
  299. }
  300. //
  301. // C++: void cv::BackgroundSubtractorMOG2::setShadowValue(int value)
  302. //
  303. /**
  304. * Sets the shadow value
  305. * param value automatically generated
  306. */
  307. public void setShadowValue(int value)
  308. {
  309. ThrowIfDisposed();
  310. video_BackgroundSubtractorMOG2_setShadowValue_10(nativeObj, value);
  311. }
  312. //
  313. // C++: double cv::BackgroundSubtractorMOG2::getShadowThreshold()
  314. //
  315. /**
  316. * Returns the shadow threshold
  317. *
  318. * A shadow is detected if pixel is a darker version of the background. The shadow threshold (Tau in
  319. * the paper) is a threshold defining how much darker the shadow can be. Tau= 0.5 means that if a pixel
  320. * is more than twice darker then it is not shadow. See Prati, Mikic, Trivedi and Cucchiara,
  321. * Detecting Moving Shadows...*, IEEE PAMI,2003.
  322. * return automatically generated
  323. */
  324. public double getShadowThreshold()
  325. {
  326. ThrowIfDisposed();
  327. return video_BackgroundSubtractorMOG2_getShadowThreshold_10(nativeObj);
  328. }
  329. //
  330. // C++: void cv::BackgroundSubtractorMOG2::setShadowThreshold(double threshold)
  331. //
  332. /**
  333. * Sets the shadow threshold
  334. * param threshold automatically generated
  335. */
  336. public void setShadowThreshold(double threshold)
  337. {
  338. ThrowIfDisposed();
  339. video_BackgroundSubtractorMOG2_setShadowThreshold_10(nativeObj, threshold);
  340. }
  341. //
  342. // C++: void cv::BackgroundSubtractorMOG2::apply(Mat image, Mat& fgmask, double learningRate = -1)
  343. //
  344. /**
  345. * Computes a foreground mask.
  346. *
  347. * param image Next video frame. Floating point frame will be used without scaling and should be in range \([0,255]\).
  348. * param fgmask The output foreground mask as an 8-bit binary image.
  349. * param learningRate The value between 0 and 1 that indicates how fast the background model is
  350. * learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
  351. * rate. 0 means that the background model is not updated at all, 1 means that the background model
  352. * is completely reinitialized from the last frame.
  353. */
  354. public override void apply(Mat image, Mat fgmask, double learningRate)
  355. {
  356. ThrowIfDisposed();
  357. if (image != null) image.ThrowIfDisposed();
  358. if (fgmask != null) fgmask.ThrowIfDisposed();
  359. video_BackgroundSubtractorMOG2_apply_10(nativeObj, image.nativeObj, fgmask.nativeObj, learningRate);
  360. }
  361. /**
  362. * Computes a foreground mask.
  363. *
  364. * param image Next video frame. Floating point frame will be used without scaling and should be in range \([0,255]\).
  365. * param fgmask The output foreground mask as an 8-bit binary image.
  366. * learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
  367. * rate. 0 means that the background model is not updated at all, 1 means that the background model
  368. * is completely reinitialized from the last frame.
  369. */
  370. public override void apply(Mat image, Mat fgmask)
  371. {
  372. ThrowIfDisposed();
  373. if (image != null) image.ThrowIfDisposed();
  374. if (fgmask != null) fgmask.ThrowIfDisposed();
  375. video_BackgroundSubtractorMOG2_apply_11(nativeObj, image.nativeObj, fgmask.nativeObj);
  376. }
  377. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  378. const string LIBNAME = "__Internal";
  379. #else
  380. const string LIBNAME = "opencvforunity";
  381. #endif
  382. // C++: int cv::BackgroundSubtractorMOG2::getHistory()
  383. [DllImport(LIBNAME)]
  384. private static extern int video_BackgroundSubtractorMOG2_getHistory_10(IntPtr nativeObj);
  385. // C++: void cv::BackgroundSubtractorMOG2::setHistory(int history)
  386. [DllImport(LIBNAME)]
  387. private static extern void video_BackgroundSubtractorMOG2_setHistory_10(IntPtr nativeObj, int history);
  388. // C++: int cv::BackgroundSubtractorMOG2::getNMixtures()
  389. [DllImport(LIBNAME)]
  390. private static extern int video_BackgroundSubtractorMOG2_getNMixtures_10(IntPtr nativeObj);
  391. // C++: void cv::BackgroundSubtractorMOG2::setNMixtures(int nmixtures)
  392. [DllImport(LIBNAME)]
  393. private static extern void video_BackgroundSubtractorMOG2_setNMixtures_10(IntPtr nativeObj, int nmixtures);
  394. // C++: double cv::BackgroundSubtractorMOG2::getBackgroundRatio()
  395. [DllImport(LIBNAME)]
  396. private static extern double video_BackgroundSubtractorMOG2_getBackgroundRatio_10(IntPtr nativeObj);
  397. // C++: void cv::BackgroundSubtractorMOG2::setBackgroundRatio(double ratio)
  398. [DllImport(LIBNAME)]
  399. private static extern void video_BackgroundSubtractorMOG2_setBackgroundRatio_10(IntPtr nativeObj, double ratio);
  400. // C++: double cv::BackgroundSubtractorMOG2::getVarThreshold()
  401. [DllImport(LIBNAME)]
  402. private static extern double video_BackgroundSubtractorMOG2_getVarThreshold_10(IntPtr nativeObj);
  403. // C++: void cv::BackgroundSubtractorMOG2::setVarThreshold(double varThreshold)
  404. [DllImport(LIBNAME)]
  405. private static extern void video_BackgroundSubtractorMOG2_setVarThreshold_10(IntPtr nativeObj, double varThreshold);
  406. // C++: double cv::BackgroundSubtractorMOG2::getVarThresholdGen()
  407. [DllImport(LIBNAME)]
  408. private static extern double video_BackgroundSubtractorMOG2_getVarThresholdGen_10(IntPtr nativeObj);
  409. // C++: void cv::BackgroundSubtractorMOG2::setVarThresholdGen(double varThresholdGen)
  410. [DllImport(LIBNAME)]
  411. private static extern void video_BackgroundSubtractorMOG2_setVarThresholdGen_10(IntPtr nativeObj, double varThresholdGen);
  412. // C++: double cv::BackgroundSubtractorMOG2::getVarInit()
  413. [DllImport(LIBNAME)]
  414. private static extern double video_BackgroundSubtractorMOG2_getVarInit_10(IntPtr nativeObj);
  415. // C++: void cv::BackgroundSubtractorMOG2::setVarInit(double varInit)
  416. [DllImport(LIBNAME)]
  417. private static extern void video_BackgroundSubtractorMOG2_setVarInit_10(IntPtr nativeObj, double varInit);
  418. // C++: double cv::BackgroundSubtractorMOG2::getVarMin()
  419. [DllImport(LIBNAME)]
  420. private static extern double video_BackgroundSubtractorMOG2_getVarMin_10(IntPtr nativeObj);
  421. // C++: void cv::BackgroundSubtractorMOG2::setVarMin(double varMin)
  422. [DllImport(LIBNAME)]
  423. private static extern void video_BackgroundSubtractorMOG2_setVarMin_10(IntPtr nativeObj, double varMin);
  424. // C++: double cv::BackgroundSubtractorMOG2::getVarMax()
  425. [DllImport(LIBNAME)]
  426. private static extern double video_BackgroundSubtractorMOG2_getVarMax_10(IntPtr nativeObj);
  427. // C++: void cv::BackgroundSubtractorMOG2::setVarMax(double varMax)
  428. [DllImport(LIBNAME)]
  429. private static extern void video_BackgroundSubtractorMOG2_setVarMax_10(IntPtr nativeObj, double varMax);
  430. // C++: double cv::BackgroundSubtractorMOG2::getComplexityReductionThreshold()
  431. [DllImport(LIBNAME)]
  432. private static extern double video_BackgroundSubtractorMOG2_getComplexityReductionThreshold_10(IntPtr nativeObj);
  433. // C++: void cv::BackgroundSubtractorMOG2::setComplexityReductionThreshold(double ct)
  434. [DllImport(LIBNAME)]
  435. private static extern void video_BackgroundSubtractorMOG2_setComplexityReductionThreshold_10(IntPtr nativeObj, double ct);
  436. // C++: bool cv::BackgroundSubtractorMOG2::getDetectShadows()
  437. [DllImport(LIBNAME)]
  438. [return: MarshalAs(UnmanagedType.U1)]
  439. private static extern bool video_BackgroundSubtractorMOG2_getDetectShadows_10(IntPtr nativeObj);
  440. // C++: void cv::BackgroundSubtractorMOG2::setDetectShadows(bool detectShadows)
  441. [DllImport(LIBNAME)]
  442. private static extern void video_BackgroundSubtractorMOG2_setDetectShadows_10(IntPtr nativeObj, [MarshalAs(UnmanagedType.U1)] bool detectShadows);
  443. // C++: int cv::BackgroundSubtractorMOG2::getShadowValue()
  444. [DllImport(LIBNAME)]
  445. private static extern int video_BackgroundSubtractorMOG2_getShadowValue_10(IntPtr nativeObj);
  446. // C++: void cv::BackgroundSubtractorMOG2::setShadowValue(int value)
  447. [DllImport(LIBNAME)]
  448. private static extern void video_BackgroundSubtractorMOG2_setShadowValue_10(IntPtr nativeObj, int value);
  449. // C++: double cv::BackgroundSubtractorMOG2::getShadowThreshold()
  450. [DllImport(LIBNAME)]
  451. private static extern double video_BackgroundSubtractorMOG2_getShadowThreshold_10(IntPtr nativeObj);
  452. // C++: void cv::BackgroundSubtractorMOG2::setShadowThreshold(double threshold)
  453. [DllImport(LIBNAME)]
  454. private static extern void video_BackgroundSubtractorMOG2_setShadowThreshold_10(IntPtr nativeObj, double threshold);
  455. // C++: void cv::BackgroundSubtractorMOG2::apply(Mat image, Mat& fgmask, double learningRate = -1)
  456. [DllImport(LIBNAME)]
  457. private static extern void video_BackgroundSubtractorMOG2_apply_10(IntPtr nativeObj, IntPtr image_nativeObj, IntPtr fgmask_nativeObj, double learningRate);
  458. [DllImport(LIBNAME)]
  459. private static extern void video_BackgroundSubtractorMOG2_apply_11(IntPtr nativeObj, IntPtr image_nativeObj, IntPtr fgmask_nativeObj);
  460. // native support for java finalize()
  461. [DllImport(LIBNAME)]
  462. private static extern void video_BackgroundSubtractorMOG2_delete(IntPtr nativeObj);
  463. }
  464. }