TrackedHand.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using BBIWARG.Recognition.Tracking;
  2. using BBIWARG.Utility;
  3. using System;
  4. namespace BBIWARG.Recognition.HandRecognition
  5. {
  6. /// <summary>
  7. /// Represents a hand that is tracked for several frames
  8. /// </summary>
  9. internal class TrackedHand : TrackedObject<Hand>
  10. {
  11. /// <summary>
  12. /// the kalman filter for the centroid prediction
  13. /// </summary>
  14. private Kalman2DPositionFilter centroidKalman;
  15. /// <summary>
  16. /// predicted position of the centroid
  17. /// </summary>
  18. public Vector2D CentroidPrediction { get { return centroidKalman.getPrediction(); } }
  19. /// <summary>
  20. /// Initializes a new instance of the TrackedHand class.
  21. /// </summary>
  22. /// <param name="id">The track ID.</param>
  23. /// <param name="detectedHand">The detected hand.</param>
  24. /// <param name="numFramesDetectedUntilTracked">The number of consecutive frames detected until the hand is considered to be tracked.</param>
  25. /// <param name="numFramesLostUntilDeleted">The number of consecutive frames lost until the hand should be deleted.</param>
  26. public TrackedHand(int id, Hand detectedHand, int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted)
  27. : base(id, detectedHand, numFramesDetectedUntilTracked, numFramesLostUntilDeleted)
  28. {
  29. centroidKalman = new Kalman2DPositionFilter(Parameters.HandmXX, Parameters.HandmXY, Parameters.HandmYY);
  30. centroidKalman.setInitialPosition(detectedHand.Centroid);
  31. logStateChange();
  32. }
  33. /// <summary>
  34. /// Updates the tracked hand with the given hand, logs the state change and updates the centroid prediction (kalman filter)
  35. /// </summary>
  36. /// <param name="detectedHand">the detected hand</param>
  37. public override void updateFrame(Hand detectedHand)
  38. {
  39. base.updateFrame(detectedHand);
  40. if (NumFramesInCurrentState == 1)
  41. logStateChange();
  42. if (detectedHand != null)
  43. centroidKalman.getCorrectedPosition(detectedHand.Centroid);
  44. }
  45. /// <summary>
  46. /// logs the state change
  47. /// </summary>
  48. private void logStateChange()
  49. {
  50. String stateAsString = CurrentState.ToString().ToLower();
  51. Logger.log(String.Format("Hand #{0} {1}", this.ID, stateAsString), LogSubject.HandTracker);
  52. }
  53. }
  54. }