VideoHandle.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 ForeFingerDetection foreFingerDetection;
  14. private PalmDetection palmDetection;
  15. private IInputProvider inputProvider;
  16. private DepthImage depthImage;
  17. private ConfidenceImage confidenceImage;
  18. private UVImage uvImage;
  19. private ColorImage colorImage;
  20. private float maxU;
  21. private float maxV;
  22. public VideoHandle(IInputProvider inputProvider) {
  23. this.inputProvider = inputProvider;
  24. inputProvider.init();
  25. inputProvider.start();
  26. inputProvider.updateFrame();
  27. foreFingerDetection = new ForeFingerDetection(inputProvider, this);
  28. palmDetection = new PalmDetection(inputProvider, this);
  29. createImageData();
  30. maxU = (float)(Math.Tan(inputProvider.getHFOV() / 2f));
  31. maxV = (float)(Math.Tan(inputProvider.getVFOV() / 2f));
  32. }
  33. ~VideoHandle()
  34. {
  35. //inputProvider.stop();
  36. }
  37. public void nextFrame()
  38. {
  39. if (inputProvider.isActive())
  40. {
  41. inputProvider.releaseFrame();
  42. inputProvider.updateFrame();
  43. createImageData();
  44. }
  45. else {
  46. inputProvider.stop();
  47. }
  48. }
  49. public int getWidth()
  50. {
  51. return depthImage.getWidth();
  52. }
  53. public int getHeight()
  54. {
  55. return depthImage.getHeight();
  56. }
  57. private void createImageData()
  58. {
  59. depthImage = inputProvider.getDepthImage();
  60. confidenceImage = inputProvider.getConfidenceImage();
  61. uvImage = inputProvider.getUVImage();
  62. colorImage = inputProvider.getColorImage();
  63. }
  64. public short getDepth(int x, int y)
  65. {
  66. return depthImage.getDepth(x, y);
  67. }
  68. public short getConfidence(int x, int y)
  69. {
  70. return confidenceImage.getConfidence(x, y);
  71. }
  72. public Color getColor(int x, int y)
  73. {
  74. float u = uvImage.getU(x, y);
  75. float v = uvImage.getV(x, y);
  76. if (u < 0 || v < 0)
  77. return Color.Black;
  78. int colorImageWidth = colorImage.getWidth();
  79. int colorImageHeight = colorImage.getHeight();
  80. int xInColorImage = (int)(u * colorImageWidth) % colorImageWidth;
  81. int yInColorImage = (int)(v * colorImageHeight) % colorImageHeight;
  82. return colorImage.getColor(xInColorImage, yInColorImage);
  83. }
  84. public DetectionStatus[] getFingerStatus(uint handIndex)
  85. {
  86. return inputProvider.getFingerStatus(handIndex);
  87. }
  88. public Vector[] getFingerTipPositions3D(uint handIndex)
  89. {
  90. return inputProvider.getFingerTipPositions3D(handIndex);
  91. }
  92. public Vector getPalmPosition3D(uint handIndex)
  93. {
  94. return inputProvider.getPalmPosition3D(handIndex);
  95. }
  96. public Vector getForearmPosition3D(uint handIndex)
  97. {
  98. return inputProvider.getForearmPosition3D(handIndex);
  99. }
  100. public Vector getForeFingerPosition3D(uint handIndex)
  101. {
  102. return foreFingerDetection.getForeFingerPosition3D(handIndex);
  103. }
  104. public Palm getPalm(uint handIndex)
  105. {
  106. return palmDetection.getPalm(handIndex);
  107. }
  108. // TODO
  109. public void createVertexArray(IntPtr vertexBuffer)
  110. {
  111. int width = depthImage.getWidth();
  112. int height = depthImage.getHeight();
  113. int confidenceThreshold = inputProvider.getConfidenceThreshold();
  114. int index = 0;
  115. for (int x = 0; x < width; x++)
  116. {
  117. for (int y = 0; y < height; y++)
  118. {
  119. if (confidenceImage.getConfidence(x, y) > confidenceThreshold)
  120. {
  121. int depth = depthImage.getDepth(x, y);
  122. Color c = getColor(x, y);
  123. create3DVertexFrom2D(x, y, depth, c, index, vertexBuffer);
  124. index++;
  125. }
  126. }
  127. }
  128. }
  129. public Vector pixel2VertexPosition(Vector pixelPosition)
  130. {
  131. int width = depthImage.getWidth();
  132. int height = depthImage.getHeight();
  133. float convertedDepth = pixelPosition[2] / 1000f; // mm into m
  134. float u = (pixelPosition[0] / (float)width - 0.5f) * 2f;
  135. float v = ((1 - pixelPosition[1] / (float)height) - 0.5f) * 2f;
  136. float relX = (u * maxU);
  137. float relY = (v * maxV);
  138. Vector result = new DenseVector(3);
  139. result[2] = -convertedDepth / (float)Math.Sqrt(1 + relX * relX + relY * relY);
  140. result[0] = -relX * result[2];
  141. result[1] = -relY * result[2];
  142. return result;
  143. }
  144. private void create3DVertexFrom2D(float pixelX, float pixelY, int depth, Color c, int index, IntPtr vertexBuffer)
  145. {
  146. Vector pixelPosition = new DenseVector(3);
  147. pixelPosition[0] = pixelX;
  148. pixelPosition[1] = pixelY;
  149. pixelPosition[2] = depth;
  150. Vector vertexPosition = pixel2VertexPosition(pixelPosition);
  151. int i4 = (3 * sizeof(float) + 4 * sizeof(byte)) / sizeof(float) * index;
  152. int i16 = (3 * sizeof(float) + 4 * sizeof(byte)) * index;
  153. unsafe
  154. {
  155. byte* vertexArrayB = (byte*)vertexBuffer.ToPointer();
  156. float* vertexArrayF = (float*)vertexBuffer.ToPointer();
  157. vertexArrayF[i4 + 0] = vertexPosition[0];
  158. vertexArrayF[i4 + 1] = vertexPosition[1];
  159. vertexArrayF[i4 + 2] = vertexPosition[2];
  160. vertexArrayB[i16 + 12] = c.R;
  161. vertexArrayB[i16 + 13] = c.G;
  162. vertexArrayB[i16 + 14] = c.B;
  163. vertexArrayB[i16 + 15] = c.A;
  164. }
  165. }
  166. }
  167. }