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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. [Header("RenderTexture")]
  9. [Tooltip("RenderTexture that will be passed to the LearningBrain.")]
  10. public RenderTexture renderTextureCropped;
  11. [Header("Debug Helper")]
  12. [Tooltip("Reference to the MeshRenderer that will show the processed Image from Cozmo")]
  13. public MeshRenderer processedImageRenderer;
  14. [Tooltip("Reference to the MeshRenderer that will show the processed and cropped Image from Cozmo")]
  15. public MeshRenderer processedImageRendererCropped;
  16. /// <summary>
  17. /// Center of Gravity in the cropped canny image
  18. /// </summary>
  19. public Point CenterOfGravity { get; private set; }
  20. // OpenCVSharp parameters
  21. private Mat cozmoImageMat;
  22. private Mat cannyImage;
  23. private Texture2D finalProcessedCozmoTexture;
  24. private Vec3b[] cozmoImageData;
  25. private byte[] cannyImageData;
  26. private int imWidth = 320; // Width of the camera image from the virtual cozmo
  27. private int imHeight = 240; // Height of the camera image from the virtual cozmo
  28. private int croppedImHeight = 120; // Height of the cropped camera image from the virtual cozmo
  29. private Camera textureCamera; // Virtual Cozmo camera
  30. private Texture2D originalCozmoTexture;
  31. private void Start()
  32. {
  33. // Get reference to the cozmo camera
  34. textureCamera = GetComponent<Camera>();
  35. // Set image widths and heights based on the given RenderTextures
  36. imWidth = textureCamera.targetTexture.width;
  37. imHeight = textureCamera.targetTexture.height;
  38. // assign the processed targetTexture to the renderer to display the image
  39. processedImageRenderer.material.mainTexture = textureCamera.targetTexture;
  40. processedImageRendererCropped.material.mainTexture = renderTextureCropped;
  41. // initialize video / image with given size
  42. cozmoImageMat = new Mat(imHeight, imWidth, MatType.CV_8UC3);
  43. cozmoImageData = new Vec3b[imHeight * imWidth];
  44. cannyImage = new Mat(imHeight, imWidth, MatType.CV_8UC1);
  45. cannyImageData = new byte[croppedImHeight * imWidth];
  46. originalCozmoTexture = new Texture2D(imWidth, imHeight, TextureFormat.RGBA32, true, true);
  47. finalProcessedCozmoTexture = new Texture2D(imWidth, croppedImHeight, TextureFormat.RGBA32, true, true);
  48. }
  49. ///// <summary>
  50. ///// Gets called when a new image arrives from the camera this script lies on
  51. ///// </summary>
  52. ///// <param name="source"></param>
  53. ///// <param name="destination"></param>
  54. public void OnRenderImage(RenderTexture source, RenderTexture destination)
  55. {
  56. RenderTextureToTexture2D(source);
  57. TextureToMat(originalCozmoTexture);
  58. ProcessImage(cozmoImageMat);
  59. cannyImage = CropImage(cannyImage);
  60. FindCenterOfGravity(cannyImage);
  61. MatToTexture(cannyImage);
  62. Graphics.Blit(finalProcessedCozmoTexture, destination);
  63. Graphics.Blit(finalProcessedCozmoTexture, renderTextureCropped);
  64. }
  65. // Crop image to just see the middle of the original image
  66. private Mat CropImage(Mat image)
  67. {
  68. //cut a fourth out of the top and bottom of the image
  69. OpenCvSharp.Rect rectCroped = new OpenCvSharp.Rect(0, image.Height / 4, image.Width, image.Height / 2);
  70. Mat croppedImage = new Mat(image, rectCroped);
  71. return croppedImage;
  72. }
  73. private void RenderTextureToTexture2D(RenderTexture rTex)
  74. {
  75. RenderTexture.active = rTex;
  76. originalCozmoTexture.ReadPixels(new UnityEngine.Rect(0, 0, rTex.width, rTex.height), 0, 0);
  77. originalCozmoTexture.Apply();
  78. }
  79. // Convert Unity Texture2D object to OpenCVSharp Mat object
  80. private void TextureToMat(Texture2D source)
  81. {
  82. // Color32 array : r, g, b, a
  83. Color32[] c = source.GetPixels32();
  84. // Parallel for loop
  85. // convert Color32 object to Vec3b object
  86. // Vec3b is the representation of pixel for Mat
  87. Parallel.For(0, imHeight, i =>
  88. {
  89. for (var j = 0; j < imWidth; j++)
  90. {
  91. var col = c[j + i * imWidth];
  92. var vec3 = new Vec3b
  93. {
  94. Item0 = col.b,
  95. Item1 = col.g,
  96. Item2 = col.r
  97. };
  98. // set pixel to an array
  99. cozmoImageData[j + i * imWidth] = vec3;
  100. }
  101. });
  102. // assign the Vec3b array to Mat
  103. cozmoImageMat.SetArray(0, 0, cozmoImageData);
  104. }
  105. // Simple example of canny edge detect
  106. private void ProcessImage(Mat _image)
  107. {
  108. Cv2.Canny(_image, cannyImage, 100, 100);
  109. }
  110. // Convert OpenCVSharp Mat object to Unity Texture2D object
  111. private void MatToTexture(Mat mat)
  112. {
  113. // cannyImageData is byte array, because canny image is grayscale
  114. mat.GetArray(0, 0, cannyImageData);
  115. // create Color32 array that can be assigned to Texture2D directly
  116. Color32[] c = new Color32[croppedImHeight * imWidth];
  117. // parallel for loop
  118. Parallel.For(0, croppedImHeight, i =>
  119. {
  120. for (var j = 0; j < imWidth; j++)
  121. {
  122. byte vec = cannyImageData[j + i * imWidth];
  123. var color32 = new Color32
  124. {
  125. r = vec,
  126. g = vec,
  127. b = vec,
  128. a = 0
  129. };
  130. c[j + i * imWidth] = color32;
  131. }
  132. });
  133. finalProcessedCozmoTexture.SetPixels32(c);
  134. // update texture
  135. finalProcessedCozmoTexture.Apply();
  136. }
  137. // Find the Center of Gravity in the image
  138. private void FindCenterOfGravity(Mat processedImage)
  139. {
  140. // find moments of the image
  141. Moments m = new Moments(processedImage, true);
  142. CenterOfGravity = new Point(m.M10 / m.M00, m.M01 / m.M00);
  143. #if UNITY_EDITOR
  144. // show the image with a point mark at the centroid
  145. Cv2.Circle(processedImage, CenterOfGravity, 5, new Scalar(128, 0, 0), -1);
  146. Cv2.Flip(processedImage, processedImage, FlipMode.X);
  147. Cv2.ImShow("Image with center", processedImage);
  148. #endif
  149. }
  150. }