VideoHandle.cs 7.3 KB

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