CircleDetectionExample.cs 6.7 KB

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