TrackedPalm.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using BBIWARG.Recognition.Tracking;
  2. using BBIWARG.Utility;
  3. using System;
  4. namespace BBIWARG.Recognition.PalmRecognition
  5. {
  6. /// <summary>
  7. /// Represents a palm that is tracked for several frames.
  8. /// </summary>
  9. internal class TrackedPalm : TrackedObject<Palm>
  10. {
  11. /// <summary>
  12. /// the kalman filter for the fingers lower prediction
  13. /// </summary>
  14. private Kalman2DPositionFilter fingersLowerKalman;
  15. /// <summary>
  16. /// the kalman filter for the fingers upper prediction
  17. /// </summary>
  18. private Kalman2DPositionFilter fingersUpperKalman;
  19. /// <summary>
  20. /// the kalman filter for the wrist lower prediction
  21. /// </summary>
  22. private Kalman2DPositionFilter wristLowerKalman;
  23. /// <summary>
  24. /// the kalman filter for the wrist upper prediction
  25. /// </summary>
  26. private Kalman2DPositionFilter wristUpperKalman;
  27. /// <summary>
  28. /// the predicted position of the fingers lower
  29. /// </summary>
  30. public Vector2D FingersLowerPrediction { get { return fingersLowerKalman.getPrediction(); } }
  31. /// <summary>
  32. /// the predicted position of the fingers upper
  33. /// </summary>
  34. public Vector2D FingersUpperPrediction { get { return fingersUpperKalman.getPrediction(); } }
  35. /// <summary>
  36. /// the optimized palm (using predicted palm points)
  37. /// </summary>
  38. public Palm OptimizedPalm { get; private set; }
  39. /// <summary>
  40. /// the predicted position of the wrist lower
  41. /// </summary>
  42. public Vector2D WristLowerPrediction { get { return wristLowerKalman.getPrediction(); } }
  43. /// <summary>
  44. /// the predicted position of the wrist upper
  45. /// </summary>
  46. public Vector2D WristUpperPrediction { get { return wristUpperKalman.getPrediction(); } }
  47. /// <summary>
  48. /// Initializes a new instance of the TrackedPalm class.
  49. /// </summary>
  50. /// <param name="id">The track ID.</param>
  51. /// <param name="detectedPalm">The detected palm.</param>
  52. /// <param name="numFramesDetectedUntilTracked">The number of consecutive frames detected until the palm is considered tracked.</param>
  53. /// <param name="numFramesLostUntilDeleted">The number of consecutive frames lost until the palm is deleted.</param>
  54. public TrackedPalm(int id, Palm detectedPalm, int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted)
  55. : base(id, detectedPalm, numFramesDetectedUntilTracked, numFramesLostUntilDeleted)
  56. {
  57. wristUpperKalman = new Kalman2DPositionFilter(Parameters.PalmmXX, Parameters.PalmmXY, Parameters.PalmmYY);
  58. wristLowerKalman = new Kalman2DPositionFilter(Parameters.PalmmXX, Parameters.PalmmXY, Parameters.PalmmYY);
  59. fingersUpperKalman = new Kalman2DPositionFilter(Parameters.PalmmXX, Parameters.PalmmXY, Parameters.PalmmYY);
  60. fingersLowerKalman = new Kalman2DPositionFilter(Parameters.PalmmXX, Parameters.PalmmXY, Parameters.PalmmYY);
  61. wristUpperKalman.setInitialPosition(detectedPalm.WristUpper);
  62. wristLowerKalman.setInitialPosition(detectedPalm.WristLower);
  63. fingersUpperKalman.setInitialPosition(detectedPalm.FingersUpper);
  64. fingersLowerKalman.setInitialPosition(detectedPalm.FingersLower);
  65. updateOptimizedPalm(detectedPalm);
  66. logStateChange();
  67. }
  68. /// <summary>
  69. /// Updates the tracked palm with the given palm, logs the state change, updates the palm point predictions and the optimized palm.
  70. /// </summary>
  71. /// <param name="detectedPalm">the detected palm</param>
  72. public override void updateFrame(Palm detectedPalm)
  73. {
  74. base.updateFrame(detectedPalm);
  75. if (NumFramesInCurrentState == 1)
  76. logStateChange();
  77. if (detectedPalm != null)
  78. {
  79. wristUpperKalman.getCorrectedPosition(detectedPalm.WristUpper);
  80. wristLowerKalman.getCorrectedPosition(detectedPalm.WristLower);
  81. fingersUpperKalman.getCorrectedPosition(detectedPalm.FingersUpper);
  82. fingersLowerKalman.getCorrectedPosition(detectedPalm.FingersLower);
  83. updateOptimizedPalm(detectedPalm);
  84. }
  85. }
  86. /// <summary>
  87. /// logs the state change
  88. /// </summary>
  89. private void logStateChange()
  90. {
  91. String stateAsString = CurrentState.ToString().ToLower();
  92. Logger.log(String.Format("Palm #{0} {1}", this.ID, stateAsString), LogSubject.PalmTracker);
  93. }
  94. /// <summary>
  95. /// Updates the optimized palm by creating a new palm with the predicted palm points
  96. /// </summary>
  97. /// <param name="detectedPalm">the detected palm</param>
  98. private void updateOptimizedPalm(Palm detectedPalm)
  99. {
  100. OptimizedPalm = new Palm(detectedPalm.Hand, detectedPalm.ThumbDefect, detectedPalm.HandSide, WristUpperPrediction, FingersUpperPrediction, FingersLowerPrediction, WristLowerPrediction);
  101. OptimizedPalm.setTracked(ID);
  102. }
  103. }
  104. }