TrackedFinger.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.FingerRecognition
  9. {
  10. class TrackedFinger : TrackedObject<Finger>
  11. {
  12. private Kalman2DPositionFilter tipPointKalman;
  13. private Kalman2DPositionFilter handPointKalman;
  14. public Vector2D TipPointPrediction { get { return tipPointKalman.getPrediction(); } }
  15. public Vector2D HandPointPrediction { get { return handPointKalman.getPrediction(); } }
  16. public TrackedFinger(int id, Finger detectedFinger, int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted)
  17. : base(id, detectedFinger, numFramesDetectedUntilTracked, numFramesLostUntilDeleted)
  18. {
  19. tipPointKalman = new Kalman2DPositionFilter(Constants.FingermXX, Constants.FingermXY, Constants.FingermYY);
  20. tipPointKalman.setInitialPosition(detectedFinger.TipPoint);
  21. handPointKalman = new Kalman2DPositionFilter(Constants.FingermXX, Constants.FingermXY, Constants.FingermYY);
  22. handPointKalman.setInitialPosition(detectedFinger.HandPoint);
  23. logStateChange();
  24. }
  25. public override void updateFrame(Finger detectedObject)
  26. {
  27. base.updateFrame(detectedObject);
  28. if (NumFramesInCurrentState == 1)
  29. logStateChange();
  30. if (detectedObject != null)
  31. {
  32. tipPointKalman.getCorrectedPosition(detectedObject.TipPoint);
  33. handPointKalman.getCorrectedPosition(detectedObject.HandPoint);
  34. }
  35. }
  36. public override float getSimilarity(Finger detectedFinger)
  37. {
  38. //tip position
  39. float tipPointDistance = detectedFinger.TipPoint.getDistanceTo(TipPointPrediction);
  40. float tipPointSimilarity = Math.Max(0, 1 - tipPointDistance / Constants.ImageDiagonalLength);
  41. //hand position
  42. float handPointDistance = detectedFinger.HandPoint.getDistanceTo(HandPointPrediction);
  43. float handPointSimilarity = Math.Max(0, 1 - handPointDistance / Constants.ImageDiagonalLength);
  44. return tipPointSimilarity * handPointSimilarity;
  45. }
  46. private void logStateChange()
  47. {
  48. String stateAsString = CurrentState.ToString().ToLower();
  49. Logger.log(String.Format("Finger #{0} {1}", this.ID, stateAsString), LogSubject.FingerTracker);
  50. }
  51. }
  52. }