VideoHandle.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 getPalmPosition3D(uint handIndex)
  67. {
  68. //improved palm recognition or just return the value provided by inputprovider
  69. return inputProvider.getPalmPosition3D(handIndex);
  70. }
  71. public VertexArray getVertexArray()
  72. {
  73. int width = depthImage.getWidth();
  74. int height = depthImage.getHeight();
  75. Vertex[] vertices = new Vertex[width * height];
  76. Color[] colors = new Color[width * height];
  77. int index = 0;
  78. for (int x = 0; x < width; x++)
  79. {
  80. for (int y = 0; y < height; y++)
  81. {
  82. int depth = depthImage.getDepth(x, y);
  83. vertices[index] = create3DVertexFrom2D(x, y, depth);
  84. colors[index] = getColor(x, y);
  85. index++;
  86. }
  87. }
  88. return new VertexArray(vertices, colors);
  89. }
  90. public Vertex create3DVertexFrom2D(float pixelX, float pixelY, int depth)
  91. {
  92. int width = depthImage.getWidth();
  93. int height = depthImage.getHeight();
  94. float convertedDepth = depth / 1000f; // mm into m
  95. float u = (pixelX / (float)width - 0.5f) * 2f;
  96. float v = ((1 - pixelY / (float)height) - 0.5f) * 2f;
  97. float relX = (u * maxU);
  98. float relY = (v * maxV);
  99. float z = convertedDepth / (float)Math.Sqrt(1 + relX * relX + relY * relY);
  100. float x = relX * z;
  101. float y = relY * z;
  102. return new Vertex(x, y, z);
  103. }
  104. }
  105. }