You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CannyTest.cs 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //using UnityEngine;
  2. //using OpenCvSharp;
  3. //using System.Threading.Tasks;
  4. //public class CannyTest : MonoBehaviour
  5. //{
  6. // // Video parameters
  7. // public MeshRenderer CameraRenderer;
  8. // public MeshRenderer ProcessedImageRenderer;
  9. // public Camera textureCamera;
  10. // // Video size
  11. // private const int imWidth = 1280; //TODO: Set width and height based on agent observation size
  12. // private const int imHeight = 720;
  13. // private int imFrameRate;
  14. // // OpenCVSharp parameters
  15. // private Mat videoSourceImage;
  16. // private Mat cannyImage;
  17. // private Texture2D processedTexture;
  18. // private Vec3b[] videoSourceImageData;
  19. // private byte[] cannyImageData;
  20. // // Frame rate parameter
  21. // private int updateFrameCount = 0;
  22. // private int textureCount = 0;
  23. // private int displayCount = 0;
  24. // void Start()
  25. // {
  26. // // assign the camera texture to the meshrenderer
  27. // CameraRenderer.material.mainTexture = textureCamera.targetTexture;
  28. // // initialize video / image with given size
  29. // videoSourceImage = new Mat(imHeight, imWidth, MatType.CV_8UC3);
  30. // videoSourceImageData = new Vec3b[imHeight * imWidth];
  31. // cannyImage = new Mat(imHeight, imWidth, MatType.CV_8UC1);
  32. // cannyImageData = new byte[imHeight * imWidth];
  33. // // create processed video texture as Texture2D object
  34. // processedTexture = new Texture2D(imWidth, imHeight, TextureFormat.RGBA32, true, true);
  35. // // assign the processedTexture to the meshrenderer for display
  36. // ProcessedImageRenderer.material.mainTexture = processedTexture;
  37. // }
  38. // void Update()
  39. // {
  40. // updateFrameCount++;
  41. // if (textureCamera.targetTexture.didUpdateThisFrame)
  42. // {
  43. // textureCount++;
  44. // // convert texture of original video to OpenCVSharp Mat object
  45. // TextureToMat();
  46. // // update the opencv window of source video
  47. // UpdateWindow(videoSourceImage);
  48. // // create the canny edge image out of source image
  49. // ProcessImage(videoSourceImage);
  50. // // convert the OpenCVSharp Mat of canny image to Texture2D
  51. // // the texture will be displayed automatically
  52. // MatToTexture();
  53. // }
  54. // }
  55. // else
  56. // {
  57. // Debug.Log("Can't find camera!");
  58. // }
  59. // // output frame rate information
  60. // if (updateFrameCount % 30 == 0)
  61. // {
  62. // Debug.Log("Frame count: " + updateFrameCount + ", Texture count: " + textureCount + ", Display count: " + displayCount);
  63. // }
  64. // }
  65. // // Convert Unity Texture2D object to OpenCVSharp Mat object
  66. // void TextureToMat()
  67. //{
  68. // // Color32 array : r, g, b, a
  69. // Color32[] c = _webcamTexture.GetPixels32();
  70. // // Parallel for loop
  71. // // convert Color32 object to Vec3b object
  72. // // Vec3b is the representation of pixel for Mat
  73. // Parallel.For(0, imHeight, i =>
  74. // {
  75. // for (var j = 0; j < imWidth; j++)
  76. // {
  77. // var col = c[j + i * imWidth];
  78. // var vec3 = new Vec3b
  79. // {
  80. // Item0 = col.b,
  81. // Item1 = col.g,
  82. // Item2 = col.r
  83. // };
  84. // // set pixel to an array
  85. // videoSourceImageData[j + i * imWidth] = vec3;
  86. // }
  87. // });
  88. // // assign the Vec3b array to Mat
  89. // videoSourceImage.SetArray(0, 0, videoSourceImageData);
  90. //}
  91. //// Convert OpenCVSharp Mat object to Unity Texture2D object
  92. //void MatToTexture()
  93. //{
  94. // // cannyImageData is byte array, because canny image is grayscale
  95. // cannyImage.GetArray(0, 0, cannyImageData);
  96. // // create Color32 array that can be assigned to Texture2D directly
  97. // Color32[] c = new Color32[imHeight * imWidth];
  98. // // parallel for loop
  99. // Parallel.For(0, imHeight, i =>
  100. // {
  101. // for (var j = 0; j < imWidth; j++)
  102. // {
  103. // byte vec = cannyImageData[j + i * imWidth];
  104. // var color32 = new Color32
  105. // {
  106. // r = vec,
  107. // g = vec,
  108. // b = vec,
  109. // a = 0
  110. // };
  111. // c[j + i * imWidth] = color32;
  112. // }
  113. // });
  114. // processedTexture.SetPixels32(c);
  115. // // to update the texture, OpenGL manner
  116. // processedTexture.Apply();
  117. //}
  118. //// Simple example of canny edge detect
  119. //void ProcessImage(Mat _image)
  120. //{
  121. // Cv2.Flip(_image, _image, FlipMode.X);
  122. // Cv2.Canny(_image, cannyImage, 100, 100);
  123. //}
  124. //// Display the original video in a opencv window
  125. //void UpdateWindow(Mat _image)
  126. //{
  127. // Cv2.Flip(_image, _image, FlipMode.X);
  128. // Cv2.ImShow("Copy video", _image);
  129. // displayCount++;
  130. //}
  131. //// close the opencv window
  132. //public void OnDestroy()
  133. //{
  134. // Cv2.DestroyAllWindows();
  135. //}
  136. //}