ForeFingerDetection.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MathNet.Numerics.LinearAlgebra.Single;
  7. using System.Drawing;
  8. namespace bbiwarg.DataSource
  9. {
  10. class ForeFingerDetection
  11. {
  12. /*
  13. * Returns the forefinger's position
  14. * all fingers (except der forefinger) have to be inactive
  15. * params:
  16. * handIndex: which hand's forefinger's position
  17. */
  18. public const int TOLERANCE_Z = 5;
  19. public const int TOLERANCE_2D = 5;
  20. private IVideoHandle source;
  21. private HashSet<Vector> seenPoints;
  22. private Vector palmPosition;
  23. private Vector foreFingerPosition;
  24. private float foreFingerDistance;
  25. public ForeFingerDetection(IVideoHandle source)
  26. {
  27. this.source = source;
  28. }
  29. public Vector getForeFingerPosition3D(uint handIndex)
  30. {
  31. Vector palmPosition2D = source.getPalmPosition2D(handIndex);
  32. palmPosition = new DenseVector(3);
  33. palmPosition[0] = palmPosition2D[0];
  34. palmPosition[1] = palmPosition2D[1];
  35. palmPosition[2] = source.getDepth((int)palmPosition[0], (int)palmPosition[1]);
  36. seenPoints = new HashSet<Vector>();
  37. foreFingerPosition = palmPosition;
  38. foreFingerDistance = 0;
  39. checkPoint(palmPosition);
  40. return foreFingerPosition;
  41. }
  42. private void checkNeighbours(Vector point)
  43. {
  44. for (int y = -(int)Math.Min(TOLERANCE_2D, point[1]); y <= TOLERANCE_2D && point[1] + y < source.getHeight(); y++)
  45. {
  46. for (int x = -(int)Math.Min(TOLERANCE_2D, point[0]); x <= TOLERANCE_2D && point[0] + x < source.getWidth(); x++)
  47. {
  48. Vector currentPoint = new DenseVector(3);
  49. currentPoint[0] = point[0] + x;
  50. currentPoint[1] = point[1] + y;
  51. currentPoint[2] = source.getDepth((int) currentPoint[0], (int) currentPoint[1]);
  52. if(Math.Abs(currentPoint[2]-point[2]) <= TOLERANCE_Z)
  53. checkPoint(currentPoint);
  54. }
  55. }
  56. }
  57. private void checkPoint(Vector point)
  58. {
  59. if (!seenPoints.Contains<Vector>(point))
  60. {
  61. seenPoints.Add(point);
  62. Vector distanceVector = new DenseVector(3);
  63. palmPosition.Subtract(point, distanceVector);
  64. float currentPointDistance = distanceVector.Norm(2);
  65. if (currentPointDistance >= foreFingerDistance)
  66. {
  67. foreFingerPosition = point;
  68. foreFingerDistance = currentPointDistance;
  69. checkNeighbours(point);
  70. }
  71. }
  72. }
  73. }
  74. }