VideoHandle.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using MathNet.Numerics.LinearAlgebra.Single;
  8. using bbiwarg.Graphics;
  9. namespace bbiwarg.DataSource
  10. {
  11. class VideoHandle : IVideoHandle
  12. {
  13. private IInputProvider inputProvider;
  14. private DepthImage depthImage;
  15. private ConfidenceImage confidenceImage;
  16. private UVImage uvImage;
  17. private ColorImage colorImage;
  18. private float maxU;
  19. private float maxV;
  20. public VideoHandle(IInputProvider inputProvider) {
  21. this.inputProvider = inputProvider;
  22. inputProvider.init();
  23. inputProvider.start();
  24. inputProvider.updateFrame();
  25. createImageData();
  26. maxU = (float)(Math.Tan(inputProvider.getHFOV() / 2f));
  27. maxV = (float)(Math.Tan(inputProvider.getVFOV() / 2f));
  28. }
  29. ~VideoHandle()
  30. {
  31. inputProvider.stop();
  32. }
  33. public void nextFrame()
  34. {
  35. inputProvider.releaseFrame();
  36. inputProvider.updateFrame();
  37. createImageData();
  38. }
  39. private void createImageData()
  40. {
  41. depthImage = inputProvider.getDepthImage();
  42. confidenceImage = inputProvider.getConfidenceImage();
  43. uvImage = inputProvider.getUVImage();
  44. colorImage = inputProvider.getColorImage();
  45. }
  46. public short getDepth(int x, int y)
  47. {
  48. return depthImage.getDepth(x, y);
  49. }
  50. public short getConfidence(int x, int y)
  51. {
  52. return confidenceImage.getConfidence(x, y);
  53. }
  54. public Color getColor(int x, int y)
  55. {
  56. float u = uvImage.getU(x, y);
  57. float v = uvImage.getV(x, y);
  58. if (u < 0 || v < 0)
  59. return Color.Black;
  60. int colorImageWidth = colorImage.getWidth();
  61. int colorImageHeight = colorImage.getHeight();
  62. int xInColorImage = (int)(u * colorImageWidth) % colorImageWidth;
  63. int yInColorImage = (int)(v * colorImageHeight) % colorImageHeight;
  64. return colorImage.getColor(xInColorImage, yInColorImage);
  65. }
  66. public Vector getPalmPosition2D(uint handIndex)
  67. {
  68. //improved palm recognition or just return the value provided by inputprovider
  69. return inputProvider.getPalmPosition2D(handIndex);
  70. }
  71. public Vector getPalmPosition3D(uint handIndex)
  72. {
  73. //improved palm recognition or just return the value provided by inputprovider
  74. return inputProvider.getPalmPosition3D(handIndex);
  75. }
  76. // TODO
  77. public void createVertexArray(IntPtr vertexBuffer)
  78. {
  79. int width = depthImage.getWidth();
  80. int height = depthImage.getHeight();
  81. int index = 0;
  82. for (int x = 0; x < width; x++)
  83. {
  84. for (int y = 0; y < height; y++)
  85. {
  86. int depth = depthImage.getDepth(x, y);
  87. Color c = getColor(x, y);
  88. create3DVertexFrom2D(x, y, depth, c, index, vertexBuffer);
  89. index++;
  90. }
  91. }
  92. }
  93. private void create3DVertexFrom2D(float pixelX, float pixelY, int depth, Color c, int index, IntPtr vertexBuffer)
  94. {
  95. int width = depthImage.getWidth();
  96. int height = depthImage.getHeight();
  97. float convertedDepth = depth / 1000f; // mm into m
  98. float u = (pixelX / (float)width - 0.5f) * 2f;
  99. float v = ((1 - pixelY / (float)height) - 0.5f) * 2f;
  100. float relX = (u * maxU);
  101. float relY = (v * maxV);
  102. float z = convertedDepth / (float)Math.Sqrt(1 + relX * relX + relY * relY);
  103. float x = relX * z;
  104. float y = relY * z;
  105. int i4 = (3 * sizeof(float) + 4 * sizeof(byte)) / sizeof(float) * index;
  106. int i16 = (3 * sizeof(float) + 4 * sizeof(byte)) * index;
  107. unsafe
  108. {
  109. byte* vertexArrayB = (byte*)vertexBuffer.ToPointer();
  110. float* vertexArrayF = (float*)vertexBuffer.ToPointer();
  111. vertexArrayF[i4 + 0] = x;
  112. vertexArrayF[i4 + 1] = y;
  113. vertexArrayF[i4 + 2] = -z;
  114. vertexArrayB[i16 + 12] = c.R;
  115. vertexArrayB[i16 + 13] = c.G;
  116. vertexArrayB[i16 + 14] = c.B;
  117. vertexArrayB[i16 + 15] = c.A;
  118. }
  119. }
  120. public int getWidth()
  121. {
  122. return depthImage.getWidth();
  123. }
  124. public int getHeight()
  125. {
  126. return depthImage.getHeight();
  127. }
  128. }
  129. }