TrackedHand.cs 2.5 KB

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