TrackedHand.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. class TrackedHand : TrackedObject<Hand>
  11. {
  12. private Kalman2DPositionFilter centroidKalman;
  13. public Vector2D CentroidPrediction { get { return centroidKalman.getPrediction(); } }
  14. public TrackedHand(int id, Hand detectedHand, int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted)
  15. : base(id, detectedHand, numFramesDetectedUntilTracked, numFramesLostUntilDeleted)
  16. {
  17. centroidKalman = new Kalman2DPositionFilter(Parameters.HandmXX, Parameters.HandmXY, Parameters.HandmYY);
  18. centroidKalman.setInitialPosition(detectedHand.Centroid);
  19. logStateChange();
  20. }
  21. public override void updateFrame(Hand detectedHand)
  22. {
  23. base.updateFrame(detectedHand);
  24. if (NumFramesInCurrentState == 1)
  25. logStateChange();
  26. if (detectedHand != null)
  27. centroidKalman.getCorrectedPosition(detectedHand.Centroid);
  28. }
  29. public override float calculateSimilarity(Hand detectedHand)
  30. {
  31. //centroid position
  32. float centroidDistance = CentroidPrediction.getDistanceTo(detectedHand.Centroid);
  33. float centroidSimilarity = Math.Max(0, 1 - centroidDistance / Parameters.ImageDiagonalLength);
  34. return centroidSimilarity;
  35. }
  36. private void logStateChange()
  37. {
  38. String stateAsString = CurrentState.ToString().ToLower();
  39. Logger.log(String.Format("Hand #{0} {1}", this.ID, stateAsString), LogSubject.HandTracker);
  40. }
  41. }
  42. }