CircleDetectionExample.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. Utils.matToTexture2D(webCamTextureMat, texture, webCamTextureToMatHelper.GetBufferColors());
  54. gameObject.GetComponent<Renderer>().material.mainTexture = texture;
  55. gameObject.transform.localScale = new Vector3(webCamTextureMat.cols(), webCamTextureMat.rows(), 1);
  56. Debug.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);
  57. if (fpsMonitor != null)
  58. {
  59. fpsMonitor.Add("width", webCamTextureMat.width().ToString());
  60. fpsMonitor.Add("height", webCamTextureMat.height().ToString());
  61. fpsMonitor.Add("orientation", Screen.orientation.ToString());
  62. }
  63. float width = webCamTextureMat.width();
  64. float height = webCamTextureMat.height();
  65. float widthScale = (float)Screen.width / width;
  66. float heightScale = (float)Screen.height / height;
  67. if (widthScale < heightScale)
  68. {
  69. Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
  70. }
  71. else
  72. {
  73. Camera.main.orthographicSize = height / 2;
  74. }
  75. grayMat = new Mat(webCamTextureMat.rows(), webCamTextureMat.cols(), CvType.CV_8UC1);
  76. }
  77. /// <summary>
  78. /// Raises the web cam texture to mat helper disposed event.
  79. /// </summary>
  80. public void OnWebCamTextureToMatHelperDisposed()
  81. {
  82. Debug.Log("OnWebCamTextureToMatHelperDisposed");
  83. if (grayMat != null)
  84. grayMat.Dispose();
  85. if (texture != null)
  86. {
  87. Texture2D.Destroy(texture);
  88. texture = null;
  89. }
  90. }
  91. /// <summary>
  92. /// Raises the web cam texture to mat helper error occurred event.
  93. /// </summary>
  94. /// <param name="errorCode">Error code.</param>
  95. public void OnWebCamTextureToMatHelperErrorOccurred(WebCamTextureToMatHelper.ErrorCode errorCode)
  96. {
  97. Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode);
  98. }
  99. // Update is called once per frame
  100. void Update()
  101. {
  102. if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame())
  103. {
  104. Mat rgbaMat = webCamTextureToMatHelper.GetMat();
  105. Imgproc.cvtColor(rgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY);
  106. using (Mat circles = new Mat())
  107. {
  108. Imgproc.HoughCircles(grayMat, circles, Imgproc.CV_HOUGH_GRADIENT, 2, 10, 160, 50, 10, 40);
  109. Point pt = new Point();
  110. for (int i = 0; i < circles.cols(); i++)
  111. {
  112. double[] data = circles.get(0, i);
  113. pt.x = data[0];
  114. pt.y = data[1];
  115. double rho = data[2];
  116. Imgproc.circle(rgbaMat, pt, (int)rho, new Scalar(255, 0, 0, 255), 5);
  117. }
  118. }
  119. //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);
  120. Utils.matToTexture2D(rgbaMat, texture, webCamTextureToMatHelper.GetBufferColors());
  121. }
  122. }
  123. /// <summary>
  124. /// Raises the destroy event.
  125. /// </summary>
  126. void OnDestroy()
  127. {
  128. webCamTextureToMatHelper.Dispose();
  129. }
  130. /// <summary>
  131. /// Raises the back button click event.
  132. /// </summary>
  133. public void OnBackButtonClick()
  134. {
  135. SceneManager.LoadScene("OpenCVForUnityExample");
  136. }
  137. /// <summary>
  138. /// Raises the play button click event.
  139. /// </summary>
  140. public void OnPlayButtonClick()
  141. {
  142. webCamTextureToMatHelper.Play();
  143. }
  144. /// <summary>
  145. /// Raises the pause button click event.
  146. /// </summary>
  147. public void OnPauseButtonClick()
  148. {
  149. webCamTextureToMatHelper.Pause();
  150. }
  151. /// <summary>
  152. /// Raises the stop button click event.
  153. /// </summary>
  154. public void OnStopButtonClick()
  155. {
  156. webCamTextureToMatHelper.Stop();
  157. }
  158. /// <summary>
  159. /// Raises the change camera button click event.
  160. /// </summary>
  161. public void OnChangeCameraButtonClick()
  162. {
  163. webCamTextureToMatHelper.requestedIsFrontFacing = !webCamTextureToMatHelper.requestedIsFrontFacing;
  164. }
  165. }
  166. }