OutputDepthImage.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using OpenTK;
  7. using OpenTK.Graphics.OpenGL;
  8. using System.Drawing;
  9. using System.Diagnostics;
  10. using MathNet.Numerics.LinearAlgebra.Single;
  11. using bbiwarg.DataSource;
  12. namespace bbiwarg.Graphics
  13. {
  14. class OutputDepthImage: GameWindow
  15. {
  16. private IInputProvider input;
  17. private uint textureId;
  18. public OutputDepthImage(IInputProvider input): base(3 * 320, 3 * 240)
  19. {
  20. this.input = input;
  21. }
  22. protected override void OnLoad(EventArgs e)
  23. {
  24. base.OnLoad(e);
  25. Title = "OutputTest";
  26. GL.ClearColor(Color.Black);
  27. // transparency
  28. GL.Enable(EnableCap.Blend);
  29. GL.BlendEquation(BlendEquationMode.Max);
  30. //Depth Test
  31. GL.Enable(EnableCap.DepthTest);
  32. // Textures
  33. GL.Enable(EnableCap.Texture2D);
  34. GL.GenTextures(1, out textureId);
  35. GL.BindTexture(TextureTarget.Texture2D, textureId);
  36. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
  37. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
  38. }
  39. protected override void OnRenderFrame(FrameEventArgs e)
  40. {
  41. base.OnRenderFrame(e);
  42. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  43. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  44. GL.MatrixMode(MatrixMode.Modelview);
  45. GL.LoadMatrix(ref modelview);
  46. input.releaseFrame();
  47. input.updateFrame();
  48. Stopwatch sw = new Stopwatch();
  49. sw.Start();
  50. DepthImage depthImage = input.getDepthImage();
  51. depthImage.filterMedian(3);
  52. Vector palmOrigin = input.getPalmPosition2D(1);
  53. int palmX = (int) palmOrigin.x();
  54. int palmY = (int) palmOrigin.y();
  55. int palmDepth = depthImage.getDepth(palmX, palmY);
  56. int maxHandSize = 150; // mm Wieviel Pixel sind das?
  57. List<Vector> points = new List<Vector>();
  58. if (palmX != 0 && palmY != 0)
  59. {
  60. depthImage.thresholdDepth(palmDepth - 10, palmDepth + 30);
  61. depthImage.thresholdPosition(palmX - maxHandSize / 2, palmX + maxHandSize / 2, palmY - maxHandSize / 2, palmY + maxHandSize / 2);
  62. points = depthImage.getRectPoints();
  63. }
  64. short[] textureData = new short[3 * depthImage.getWidth() * depthImage.getHeight()];
  65. int index = 0;
  66. for (int y = 0; y < depthImage.getHeight(); ++y) {
  67. for (int x = 0; x < depthImage.getWidth(); ++x) {
  68. // 0 --> 0 / 2000 -> Int16.MaxValue
  69. int d = depthImage.getDepth(x, y);
  70. short depth = (short) (d * Int16.MaxValue / 2000);
  71. textureData[index] = textureData[index + 1] = textureData[index + 2] = depth;
  72. index += 3;
  73. }
  74. }
  75. // draw palm origin
  76. index = 3 * ((int) palmOrigin.y() * depthImage.getWidth() + (int) palmOrigin.x());
  77. textureData[index + 0] = Int16.MaxValue;
  78. textureData[index + 1] = 0;
  79. textureData[index + 2] = 0;
  80. // draw rect points
  81. foreach (Vector v in points)
  82. {
  83. int x = (int)v.x() % depthImage.getWidth();
  84. int y = (int)v.y() % depthImage.getHeight();
  85. for (int i = -1; i < 2; ++i)
  86. {
  87. for (int j = -1; j < 2; ++j)
  88. {
  89. int px = Math.Min(depthImage.getWidth() - 1, Math.Max(0, x + i));
  90. int py = Math.Min(depthImage.getHeight() - 1, Math.Max(0, y + j));
  91. index = 3 * (py * depthImage.getWidth() + px);
  92. textureData[index + 0] = 0;
  93. textureData[index + 1] = 0;
  94. textureData[index + 2] = Int16.MaxValue;
  95. }
  96. }
  97. }
  98. GL.BindTexture(TextureTarget.Texture2D, textureId);
  99. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, depthImage.getWidth(), depthImage.getHeight(), 0,
  100. PixelFormat.Rgb, PixelType.Short, textureData);
  101. float size_2 = 0.5f / 2.0f;
  102. GL.Begin(PrimitiveType.Quads);
  103. GL.TexCoord2(0.0, 0.0); GL.Vertex3(-size_2, size_2, -0.5);
  104. GL.TexCoord2(1.0, 0.0); GL.Vertex3( size_2, size_2, -0.5);
  105. GL.TexCoord2(1.0, 1.0); GL.Vertex3( size_2, -size_2, -0.5);
  106. GL.TexCoord2(0.0, 1.0); GL.Vertex3(-size_2, -size_2, -0.5);
  107. GL.End();
  108. sw.Stop();
  109. Console.WriteLine(sw.ElapsedMilliseconds);
  110. SwapBuffers();
  111. }
  112. protected override void OnResize(EventArgs e)
  113. {
  114. base.OnResize(e);
  115. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  116. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f);
  117. GL.MatrixMode(MatrixMode.Projection);
  118. GL.LoadMatrix(ref projection);
  119. }
  120. }
  121. }