ForeFingerDetection.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. seenPoints = new HashSet<Vector>();
  32. Vector palmPosition2D = source.getPalmPosition2D(handIndex);
  33. palmPosition = new DenseVector(3);
  34. palmPosition[0] = palmPosition2D[0];
  35. palmPosition[1] = palmPosition2D[1];
  36. palmPosition[2] = source.getDepth((int)palmPosition[0], (int)palmPosition[1]);
  37. foreFingerPosition = palmPosition;
  38. checkPoint(palmPosition);
  39. return foreFingerPosition;
  40. }
  41. private void checkNeighbours(Vector point)
  42. {
  43. for (int y = -(int)Math.Min(TOLERANCE_2D, point[1]); y <= TOLERANCE_2D && point[1] + y < source.getHeight(); y++)
  44. {
  45. for (int x = -(int)Math.Min(TOLERANCE_2D, point[0]); x <= TOLERANCE_2D && point[0] + x < source.getWidth(); x++)
  46. {
  47. Vector currentPoint = new DenseVector(3);
  48. currentPoint[0] = point[0] + x;
  49. currentPoint[1] = point[1] + y;
  50. currentPoint[2] = source.getDepth((int) currentPoint[0], (int) currentPoint[1]);
  51. if(Math.Abs(currentPoint[2]-point[2]) <= TOLERANCE_Z)
  52. checkPoint(currentPoint);
  53. }
  54. }
  55. }
  56. private void checkPoint(Vector point)
  57. {
  58. if (!seenPoints.Contains<Vector>(point))
  59. {
  60. seenPoints.Add(point);
  61. Vector distanceVector = new DenseVector(3);
  62. palmPosition.Subtract(point, distanceVector);
  63. float currentPointDistance = distanceVector.Norm(2);
  64. if (currentPointDistance >= foreFingerDistance)
  65. {
  66. foreFingerPosition = point;
  67. foreFingerDistance = currentPointDistance;
  68. checkNeighbours(point);
  69. }
  70. }
  71. }
  72. }
  73. }