BackgroundSubtractorMOG2Example.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using System.Collections;
  4. using OpenCVForUnity.CoreModule;
  5. using OpenCVForUnity.VideoModule;
  6. using OpenCVForUnity.ImgprocModule;
  7. using OpenCVForUnity.UnityUtils;
  8. using OpenCVForUnity.UnityUtils.Helper;
  9. namespace OpenCVForUnityExample
  10. {
  11. /// <summary>
  12. /// BackgroundSubtractorMOG2 Example
  13. /// An example of background subtraction using the Video.BackgroundSubtractorMOG2 class.
  14. /// Referring to http://docs.opencv.org/master/d1/dc5/tutorial_background_subtraction.html#gsc.tab=0.
  15. /// </summary>
  16. [RequireComponent (typeof(WebCamTextureToMatHelper))]
  17. public class BackgroundSubtractorMOG2Example : MonoBehaviour
  18. {
  19. /// <summary>
  20. /// The texture.
  21. /// </summary>
  22. Texture2D texture;
  23. /// <summary>
  24. /// The webcam texture to mat helper.
  25. /// </summary>
  26. WebCamTextureToMatHelper webCamTextureToMatHelper;
  27. /// <summary>
  28. /// The background substractor MOG2.
  29. /// </summary>
  30. BackgroundSubtractorMOG2 backgroundSubstractorMOG2;
  31. /// <summary>
  32. /// The rgb mat.
  33. /// </summary>
  34. Mat rgbMat;
  35. /// <summary>
  36. /// The fgmask mat.
  37. /// </summary>
  38. Mat fgmaskMat;
  39. /// <summary>
  40. /// The FPS monitor.
  41. /// </summary>
  42. FpsMonitor fpsMonitor;
  43. // Use this for initialization
  44. void Start ()
  45. {
  46. fpsMonitor = GetComponent<FpsMonitor> ();
  47. webCamTextureToMatHelper = gameObject.GetComponent<WebCamTextureToMatHelper> ();
  48. #if UNITY_ANDROID && !UNITY_EDITOR
  49. // Avoids the front camera low light issue that occurs in only some Android devices (e.g. Google Pixel, Pixel2).
  50. webCamTextureToMatHelper.avoidAndroidFrontCameraLowLightIssue = true;
  51. #endif
  52. webCamTextureToMatHelper.Initialize ();
  53. backgroundSubstractorMOG2 = Video.createBackgroundSubtractorMOG2 ();
  54. // backgroundSubstractorMOG2.setHistory (2);
  55. // backgroundSubstractorMOG2.setVarThreshold (16);
  56. // backgroundSubstractorMOG2.setDetectShadows (true);
  57. }
  58. /// <summary>
  59. /// Raises the webcam texture to mat helper initialized event.
  60. /// </summary>
  61. public void OnWebCamTextureToMatHelperInitialized ()
  62. {
  63. Debug.Log ("OnWebCamTextureToMatHelperInitialized");
  64. Mat webCamTextureMat = webCamTextureToMatHelper.GetMat ();
  65. texture = new Texture2D (webCamTextureMat.cols (), webCamTextureMat.rows (), TextureFormat.RGBA32, false);
  66. gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  67. gameObject.transform.localScale = new Vector3 (webCamTextureMat.cols (), webCamTextureMat.rows (), 1);
  68. Debug.Log ("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);
  69. if (fpsMonitor != null) {
  70. fpsMonitor.Add ("width", webCamTextureMat.width ().ToString ());
  71. fpsMonitor.Add ("height", webCamTextureMat.height ().ToString ());
  72. fpsMonitor.Add ("orientation", Screen.orientation.ToString ());
  73. }
  74. float width = webCamTextureMat.width ();
  75. float height = webCamTextureMat.height ();
  76. float widthScale = (float)Screen.width / width;
  77. float heightScale = (float)Screen.height / height;
  78. if (widthScale < heightScale) {
  79. Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
  80. } else {
  81. Camera.main.orthographicSize = height / 2;
  82. }
  83. rgbMat = new Mat (webCamTextureMat.rows (), webCamTextureMat.cols (), CvType.CV_8UC3);
  84. fgmaskMat = new Mat (webCamTextureMat.rows (), webCamTextureMat.cols (), CvType.CV_8UC1);
  85. }
  86. /// <summary>
  87. /// Raises the webcam texture to mat helper disposed event.
  88. /// </summary>
  89. public void OnWebCamTextureToMatHelperDisposed ()
  90. {
  91. Debug.Log ("OnWebCamTextureToMatHelperDisposed");
  92. if (rgbMat != null)
  93. rgbMat.Dispose ();
  94. if (fgmaskMat != null)
  95. fgmaskMat.Dispose ();
  96. if (texture != null) {
  97. Texture2D.Destroy (texture);
  98. texture = null;
  99. }
  100. }
  101. /// <summary>
  102. /// Raises the webcam texture to mat helper error occurred event.
  103. /// </summary>
  104. /// <param name="errorCode">Error code.</param>
  105. public void OnWebCamTextureToMatHelperErrorOccurred (WebCamTextureToMatHelper.ErrorCode errorCode)
  106. {
  107. Debug.Log ("OnWebCamTextureToMatHelperErrorOccurred " + errorCode);
  108. }
  109. // Update is called once per frame
  110. void Update ()
  111. {
  112. if (webCamTextureToMatHelper.IsPlaying () && webCamTextureToMatHelper.DidUpdateThisFrame ()) {
  113. Mat rgbaMat = webCamTextureToMatHelper.GetMat ();
  114. Imgproc.cvtColor (rgbaMat, rgbMat, Imgproc.COLOR_RGBA2RGB);
  115. backgroundSubstractorMOG2.apply (rgbMat, fgmaskMat);
  116. Core.bitwise_not (fgmaskMat, fgmaskMat);
  117. rgbaMat.setTo (new Scalar (0, 0, 0, 0), fgmaskMat);
  118. Utils.fastMatToTexture2D (rgbaMat, texture);
  119. }
  120. }
  121. /// <summary>
  122. /// Raises the destroy event.
  123. /// </summary>
  124. void OnDestroy ()
  125. {
  126. webCamTextureToMatHelper.Dispose ();
  127. }
  128. /// <summary>
  129. /// Raises the back button click event.
  130. /// </summary>
  131. public void OnBackButtonClick ()
  132. {
  133. SceneManager.LoadScene ("OpenCVForUnityExample");
  134. }
  135. /// <summary>
  136. /// Raises the play button click event.
  137. /// </summary>
  138. public void OnPlayButtonClick ()
  139. {
  140. webCamTextureToMatHelper.Play ();
  141. }
  142. /// <summary>
  143. /// Raises the pause button click event.
  144. /// </summary>
  145. public void OnPauseButtonClick ()
  146. {
  147. webCamTextureToMatHelper.Pause ();
  148. }
  149. /// <summary>
  150. /// Raises the stop button click event.
  151. /// </summary>
  152. public void OnStopButtonClick ()
  153. {
  154. webCamTextureToMatHelper.Stop ();
  155. }
  156. /// <summary>
  157. /// Raises the change camera button click event.
  158. /// </summary>
  159. public void OnChangeCameraButtonClick ()
  160. {
  161. webCamTextureToMatHelper.requestedIsFrontFacing = !webCamTextureToMatHelper.IsFrontFacing ();
  162. }
  163. }
  164. }