using UnityEngine; using UnityEngine.SceneManagement; using System.Collections; using OpenCVForUnity.CoreModule; using OpenCVForUnity.VideoModule; using OpenCVForUnity.ImgprocModule; using OpenCVForUnity.UnityUtils; using OpenCVForUnity.UnityUtils.Helper; namespace OpenCVForUnityExample { /// /// BackgroundSubtractorMOG2 Example /// An example of background subtraction using the Video.BackgroundSubtractorMOG2 class. /// Referring to http://docs.opencv.org/master/d1/dc5/tutorial_background_subtraction.html#gsc.tab=0. /// [RequireComponent (typeof(WebCamTextureToMatHelper))] public class BackgroundSubtractorMOG2Example : MonoBehaviour { /// /// The texture. /// Texture2D texture; /// /// The webcam texture to mat helper. /// WebCamTextureToMatHelper webCamTextureToMatHelper; /// /// The background substractor MOG2. /// BackgroundSubtractorMOG2 backgroundSubstractorMOG2; /// /// The rgb mat. /// Mat rgbMat; /// /// The fgmask mat. /// Mat fgmaskMat; /// /// The FPS monitor. /// FpsMonitor fpsMonitor; // Use this for initialization void Start () { fpsMonitor = GetComponent (); webCamTextureToMatHelper = gameObject.GetComponent (); #if UNITY_ANDROID && !UNITY_EDITOR // Avoids the front camera low light issue that occurs in only some Android devices (e.g. Google Pixel, Pixel2). webCamTextureToMatHelper.avoidAndroidFrontCameraLowLightIssue = true; #endif webCamTextureToMatHelper.Initialize (); backgroundSubstractorMOG2 = Video.createBackgroundSubtractorMOG2 (); // backgroundSubstractorMOG2.setHistory (2); // backgroundSubstractorMOG2.setVarThreshold (16); // backgroundSubstractorMOG2.setDetectShadows (true); } /// /// Raises the webcam texture to mat helper initialized event. /// public void OnWebCamTextureToMatHelperInitialized () { Debug.Log ("OnWebCamTextureToMatHelperInitialized"); Mat webCamTextureMat = webCamTextureToMatHelper.GetMat (); texture = new Texture2D (webCamTextureMat.cols (), webCamTextureMat.rows (), TextureFormat.RGBA32, false); gameObject.GetComponent ().material.mainTexture = texture; gameObject.transform.localScale = new Vector3 (webCamTextureMat.cols (), webCamTextureMat.rows (), 1); Debug.Log ("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation); if (fpsMonitor != null) { fpsMonitor.Add ("width", webCamTextureMat.width ().ToString ()); fpsMonitor.Add ("height", webCamTextureMat.height ().ToString ()); fpsMonitor.Add ("orientation", Screen.orientation.ToString ()); } float width = webCamTextureMat.width (); float height = webCamTextureMat.height (); float widthScale = (float)Screen.width / width; float heightScale = (float)Screen.height / height; if (widthScale < heightScale) { Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2; } else { Camera.main.orthographicSize = height / 2; } rgbMat = new Mat (webCamTextureMat.rows (), webCamTextureMat.cols (), CvType.CV_8UC3); fgmaskMat = new Mat (webCamTextureMat.rows (), webCamTextureMat.cols (), CvType.CV_8UC1); } /// /// Raises the webcam texture to mat helper disposed event. /// public void OnWebCamTextureToMatHelperDisposed () { Debug.Log ("OnWebCamTextureToMatHelperDisposed"); if (rgbMat != null) rgbMat.Dispose (); if (fgmaskMat != null) fgmaskMat.Dispose (); if (texture != null) { Texture2D.Destroy (texture); texture = null; } } /// /// Raises the webcam texture to mat helper error occurred event. /// /// Error code. public void OnWebCamTextureToMatHelperErrorOccurred (WebCamTextureToMatHelper.ErrorCode errorCode) { Debug.Log ("OnWebCamTextureToMatHelperErrorOccurred " + errorCode); } // Update is called once per frame void Update () { if (webCamTextureToMatHelper.IsPlaying () && webCamTextureToMatHelper.DidUpdateThisFrame ()) { Mat rgbaMat = webCamTextureToMatHelper.GetMat (); Imgproc.cvtColor (rgbaMat, rgbMat, Imgproc.COLOR_RGBA2RGB); backgroundSubstractorMOG2.apply (rgbMat, fgmaskMat); Core.bitwise_not (fgmaskMat, fgmaskMat); rgbaMat.setTo (new Scalar (0, 0, 0, 0), fgmaskMat); Utils.fastMatToTexture2D (rgbaMat, texture); } } /// /// Raises the destroy event. /// void OnDestroy () { webCamTextureToMatHelper.Dispose (); } /// /// Raises the back button click event. /// public void OnBackButtonClick () { SceneManager.LoadScene ("OpenCVForUnityExample"); } /// /// Raises the play button click event. /// public void OnPlayButtonClick () { webCamTextureToMatHelper.Play (); } /// /// Raises the pause button click event. /// public void OnPauseButtonClick () { webCamTextureToMatHelper.Pause (); } /// /// Raises the stop button click event. /// public void OnStopButtonClick () { webCamTextureToMatHelper.Stop (); } /// /// Raises the change camera button click event. /// public void OnChangeCameraButtonClick () { webCamTextureToMatHelper.requestedIsFrontFacing = !webCamTextureToMatHelper.IsFrontFacing (); } } }