using System.Collections; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using UnityEngine.EventSystems; using OpenCVForUnity.CoreModule; using OpenCVForUnity.ImgprocModule; using OpenCVForUnity.UnityUtils; using OpenCVForUnity.UnityUtils.Helper; namespace OpenCVForUnityExample { /// /// Green Screen Example /// Referring to https://gist.github.com/drscotthawley/2d6bbffce9dda5f3057b4879c3bd4422. /// [RequireComponent (typeof(WebCamTextureToMatHelper))] public class GreenScreenExample : MonoBehaviour { /// /// The thresh. /// [Range (0, 255)] public float thresh = 50.0f; /// /// The background raw image. /// public RawImage bgRawImage; /// /// The texture. /// Texture2D texture; /// /// The webcam texture to mat helper. /// WebCamTextureToMatHelper webCamTextureToMatHelper; /// /// The background mat. /// Mat bgMat; /// /// The foreground mask mat. /// Mat fgMaskMat; /// /// The background mask mat. /// Mat bgMaskMat; /// /// The green mat. /// Mat greenMat; /// /// The background texture. /// Texture2D bgTexture; /// /// 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 (); } /// /// 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 ()); fpsMonitor.consoleText = "SPACE KEY or TOUCH SCREEN: Reset backgroud image."; } 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; } bgMat = new Mat (webCamTextureMat.rows (), webCamTextureMat.cols (), CvType.CV_8UC4); fgMaskMat = new Mat (webCamTextureMat.rows (), webCamTextureMat.cols (), CvType.CV_8UC1); bgMaskMat = new Mat (webCamTextureMat.rows (), webCamTextureMat.cols (), CvType.CV_8UC1); greenMat = new Mat (webCamTextureMat.rows (), webCamTextureMat.cols (), CvType.CV_8UC4, new Scalar (0, 255, 0, 255)); bgTexture = new Texture2D (bgMat.cols (), bgMat.rows (), TextureFormat.RGBA32, false); } /// /// Raises the webcam texture to mat helper disposed event. /// public void OnWebCamTextureToMatHelperDisposed () { Debug.Log ("OnWebCamTextureToMatHelperDisposed"); if (bgMat != null) { bgMat.Dispose (); bgMat = null; } if (fgMaskMat != null) { fgMaskMat.Dispose (); fgMaskMat = null; } if (bgMaskMat != null) { bgMaskMat.Dispose (); bgMaskMat = null; } if (greenMat != null) { greenMat.Dispose (); greenMat = null; } 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 () { bool isTouched = false; #if ((UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR) if (Input.touchCount == 1) { Touch t = Input.GetTouch(0); if (t.phase == TouchPhase.Ended && !EventSystem.current.IsPointerOverGameObject (t.fingerId)) { isTouched = true; } } #else if (Input.GetKeyUp (KeyCode.Space)) { isTouched = true; } #endif if (webCamTextureToMatHelper.IsPlaying () && webCamTextureToMatHelper.DidUpdateThisFrame ()) { Mat rgbaMat = webCamTextureToMatHelper.GetMat (); if (isTouched) { rgbaMat.copyTo (bgMat); setBgTexture (bgMat); } //set fgMaskMat findFgMaskMat (rgbaMat, bgMat, thresh); //set bgMaskMat Core.bitwise_not (fgMaskMat, bgMaskMat); //copy greenMat using bgMaskMat greenMat.copyTo (rgbaMat, bgMaskMat); //Imgproc.putText (rgbaMat, "SPACE KEY or TOUCH SCREEN: Reset backgroud image.", new Point (5, rgbaMat.rows () - 10), Core.FONT_HERSHEY_SIMPLEX, 0.6, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false); Utils.fastMatToTexture2D (rgbaMat, texture); } } /// /// Finds the foreground mask mat. /// /// Fg mat. /// Background mat. /// Thresh. private void findFgMaskMat (Mat fgMat, Mat bgMat, float thresh = 13.0f) { Mat diff1 = new Mat (); Core.absdiff (fgMat, bgMat, diff1); Mat diff2 = new Mat (); Core.absdiff (bgMat, fgMat, diff2); Mat diff = diff1 + diff2; Imgproc.threshold (diff, diff, thresh, 0, Imgproc.THRESH_TOZERO); Imgproc.cvtColor (diff, fgMaskMat, Imgproc.COLOR_RGBA2GRAY); Imgproc.threshold (fgMaskMat, fgMaskMat, 10, 0, Imgproc.THRESH_TOZERO); Imgproc.threshold (fgMaskMat, fgMaskMat, 0, 255, Imgproc.THRESH_BINARY); diff1.Dispose (); diff2.Dispose (); diff.Dispose (); } /// /// Sets the background texture. /// /// Background mat. private void setBgTexture (Mat bgMat) { Utils.matToTexture2D (bgMat, bgTexture); bgRawImage.texture = bgTexture; bgRawImage.rectTransform.localScale = new Vector3 (1.0f, (float)bgMat.height () / (float)bgMat.width (), 1.0f); } /// /// 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 (); } } }