VideoHandle.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 getPalmNormal3D(uint handIndex)
  97. {
  98. return inputProvider.getPalmNormal3D(handIndex);
  99. }
  100. public Vector getForearmPosition3D(uint handIndex)
  101. {
  102. return inputProvider.getForearmPosition3D(handIndex);
  103. }
  104. public Vector getForeFingerPosition3D(uint handIndex)
  105. {
  106. return foreFingerDetection.getForeFingerPosition3D(handIndex);
  107. }
  108. public List<Vector> getHandPoints()
  109. {
  110. return foreFingerDetection.getHandPoints();
  111. }
  112. public Palm getPalm(uint handIndex)
  113. {
  114. return palmDetection.getPalm(handIndex);
  115. }
  116. // TODO
  117. public void createVertexArray(IntPtr vertexBuffer)
  118. {
  119. int width = depthImage.getWidth();
  120. int height = depthImage.getHeight();
  121. int confidenceThreshold = inputProvider.getConfidenceThreshold();
  122. int index = 0;
  123. for (int x = 0; x < width; x++)
  124. {
  125. for (int y = 0; y < height; y++)
  126. {
  127. if (confidenceImage.getConfidence(x, y) > confidenceThreshold)
  128. {
  129. int depth = depthImage.getDepth(x, y);
  130. Color c = getColor(x, y);
  131. create3DVertexFrom2D(x, y, depth, c, index, vertexBuffer);
  132. index++;
  133. }
  134. }
  135. }
  136. }
  137. public Vector pixel2VertexPosition(Vector pixelPosition)
  138. {
  139. int width = depthImage.getWidth();
  140. int height = depthImage.getHeight();
  141. float convertedDepth = pixelPosition[2] / 1000f; // mm into m
  142. float u = (pixelPosition[0] / (float)width - 0.5f) * 2f;
  143. float v = ((1 - pixelPosition[1] / (float)height) - 0.5f) * 2f;
  144. float relX = (u * maxU);
  145. float relY = (v * maxV);
  146. Vector result = new DenseVector(3);
  147. result[2] = convertedDepth / (float)Math.Sqrt(1 + relX * relX + relY * relY);
  148. result[0] = relX * result[2];
  149. result[1] = relY * result[2];
  150. return result;
  151. }
  152. private void create3DVertexFrom2D(float pixelX, float pixelY, int depth, Color c, int index, IntPtr vertexBuffer)
  153. {
  154. Vector pixelPosition = new DenseVector(3);
  155. pixelPosition[0] = pixelX;
  156. pixelPosition[1] = pixelY;
  157. pixelPosition[2] = depth;
  158. Vector vertexPosition = pixel2VertexPosition(pixelPosition);
  159. int i4 = (3 * sizeof(float) + 4 * sizeof(byte)) / sizeof(float) * index;
  160. int i16 = (3 * sizeof(float) + 4 * sizeof(byte)) * index;
  161. unsafe
  162. {
  163. byte* vertexArrayB = (byte*)vertexBuffer.ToPointer();
  164. float* vertexArrayF = (float*)vertexBuffer.ToPointer();
  165. vertexArrayF[i4 + 0] = vertexPosition[0];
  166. vertexArrayF[i4 + 1] = vertexPosition[1];
  167. vertexArrayF[i4 + 2] = -vertexPosition[2];
  168. vertexArrayB[i16 + 12] = c.R;
  169. vertexArrayB[i16 + 13] = c.G;
  170. vertexArrayB[i16 + 14] = c.B;
  171. vertexArrayB[i16 + 15] = c.A;
  172. }
  173. }
  174. }
  175. }