TrackedPalm.cs 5.2 KB

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