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.

ImageProcessor.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using OpenCvSharp;
  5. using System.Threading.Tasks;
  6. public class ImageProcessor : MonoBehaviour
  7. {
  8. public MeshRenderer processedImageRenderer;
  9. public Point CenterOfGravity { get; private set; }
  10. // OpenCVSharp parameters
  11. private Mat videoSourceImage;
  12. private Mat cannyImage;
  13. private Texture2D processedTexture;
  14. private Vec3b[] videoSourceImageData;
  15. private byte[] cannyImageData;
  16. private const int imWidth = 320; //TODO: Set width and height based on agent observation size
  17. private const int imHeight = 240;
  18. private Camera textureCamera;
  19. private void Start()
  20. {
  21. textureCamera = GetComponent<Camera>();
  22. //assign the processed targetTexture to the renderer to display the image
  23. processedImageRenderer.material.mainTexture = textureCamera.targetTexture;
  24. // initialize video / image with given size
  25. videoSourceImage = new Mat(imHeight, imWidth, MatType.CV_8UC3);
  26. videoSourceImageData = new Vec3b[imHeight * imWidth];
  27. cannyImage = new Mat(imHeight, imWidth, MatType.CV_8UC1);
  28. cannyImageData = new byte[imHeight * imWidth];
  29. }
  30. private void OnRenderImage(RenderTexture source, RenderTexture destination)
  31. {
  32. Texture2D tex = RenderTextureToTexture2D(source);
  33. videoSourceImage = TextureToMat(tex);
  34. cannyImage = ProcessImage(videoSourceImage);
  35. CenterOfGravityTest(cannyImage);
  36. processedTexture = MatToTexture(cannyImage);
  37. Graphics.Blit(processedTexture, destination);
  38. }
  39. private Texture2D RenderTextureToTexture2D(RenderTexture rTex)
  40. {
  41. Texture2D tex = new Texture2D(imWidth, imHeight, TextureFormat.RGBA32, true, true);
  42. RenderTexture.active = rTex;
  43. tex.ReadPixels(new UnityEngine.Rect(0, 0, rTex.width, rTex.height), 0, 0);
  44. tex.Apply();
  45. return tex;
  46. }
  47. // Convert Unity Texture2D object to OpenCVSharp Mat object
  48. private Mat TextureToMat(Texture2D source)
  49. {
  50. // Color32 array : r, g, b, a
  51. Color32[] c = source.GetPixels32();
  52. // Parallel for loop
  53. // convert Color32 object to Vec3b object
  54. // Vec3b is the representation of pixel for Mat
  55. Parallel.For(0, imHeight, i =>
  56. {
  57. for (var j = 0; j < imWidth; j++)
  58. {
  59. var col = c[j + i * imWidth];
  60. var vec3 = new Vec3b
  61. {
  62. Item0 = col.b,
  63. Item1 = col.g,
  64. Item2 = col.r
  65. };
  66. // set pixel to an array
  67. videoSourceImageData[j + i * imWidth] = vec3;
  68. }
  69. });
  70. // assign the Vec3b array to Mat
  71. Mat tmpMat = new Mat(imHeight, imWidth, MatType.CV_8UC3);
  72. tmpMat.SetArray(0, 0, videoSourceImageData);
  73. return tmpMat;
  74. }
  75. // Simple example of canny edge detect
  76. private Mat ProcessImage(Mat _image)
  77. {
  78. Mat cannyImg = new Mat();
  79. Cv2.Canny(_image, cannyImg, 100, 100);
  80. return cannyImg;
  81. }
  82. // Convert OpenCVSharp Mat object to Unity Texture2D object
  83. private Texture2D MatToTexture(Mat mat)
  84. {
  85. // cannyImageData is byte array, because canny image is grayscale
  86. mat.GetArray(0, 0, cannyImageData);
  87. // create Color32 array that can be assigned to Texture2D directly
  88. Color32[] c = new Color32[imHeight * imWidth];
  89. // parallel for loop
  90. Parallel.For(0, imHeight, i =>
  91. {
  92. for (var j = 0; j < imWidth; j++)
  93. {
  94. byte vec = cannyImageData[j + i * imWidth];
  95. var color32 = new Color32
  96. {
  97. r = vec,
  98. g = vec,
  99. b = vec,
  100. a = 0
  101. };
  102. c[j + i * imWidth] = color32;
  103. }
  104. });
  105. Texture2D texture = new Texture2D(imWidth, imHeight, TextureFormat.RGBA32, true, true);
  106. texture.SetPixels32(c);
  107. // to update the texture, OpenGL manner
  108. texture.Apply();
  109. return texture;
  110. }
  111. private void CenterOfGravityTest(Mat processedImage)
  112. {
  113. // find moments of the image
  114. Moments m = new Moments(processedImage, true);
  115. CenterOfGravity = new Point(m.M10 / m.M00, m.M01 / m.M00);
  116. #if UNITY_EDITOR
  117. // show the image with a point mark at the centroid
  118. Cv2.Circle(processedImage, CenterOfGravity, 5, new Scalar(128, 0, 0), -1);
  119. Cv2.Flip(processedImage, processedImage, FlipMode.X);
  120. Cv2.ImShow("Image with center", processedImage);
  121. #endif
  122. }
  123. }