FingerTracker.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using BBIWARG.Input.InputHandling;
  2. using BBIWARG.Recognition.Tracking;
  3. using BBIWARG.Utility;
  4. namespace BBIWARG.Recognition.FingerRecognition
  5. {
  6. /// <summary>
  7. /// Keeps track of fingers over a period of time.
  8. /// </summary>
  9. internal class FingerTracker : Tracker<Finger, TrackedFinger>
  10. {
  11. /// <summary>
  12. /// Initializes a new instance of the FingerTracker class.
  13. /// </summary>
  14. /// <param name="imageSize">Size of the input image.</param>
  15. public FingerTracker(ImageSize imageSize)
  16. : base(imageSize)
  17. {
  18. }
  19. /// <summary>
  20. /// Calculates the similarity [0-1] of a tracked Finger and a detected finger
  21. /// </summary>
  22. /// <param name="trackedFinger">the tracked finger</param>
  23. /// <param name="detectedFinger">the detected finger</param>
  24. /// <returns>the similarity between both fingers</returns>
  25. public override float calculateSimilarity(TrackedFinger trackedFinger, Finger detectedFinger)
  26. {
  27. Vector2D tip, hand;
  28. if (trackedFinger.shouldFingerBeReversed(detectedFinger))
  29. {
  30. tip = detectedFinger.HandPoint;
  31. hand = detectedFinger.TipPoint;
  32. }
  33. else
  34. {
  35. tip = detectedFinger.TipPoint;
  36. hand = detectedFinger.HandPoint;
  37. }
  38. float tipPointSimilarity = getPositionSimilarity(trackedFinger.TipPointPrediction, tip, Parameters.FingerTrackerMaxTipPointRelativeMove);
  39. float handPointSimilarity = getPositionSimilarity(trackedFinger.HandPointPrediction, hand, Parameters.FingerTrackerMaxHandPointRelativeMove);
  40. return tipPointSimilarity * handPointSimilarity;
  41. }
  42. /// <summary>
  43. /// Updates the TrackedFingers with the detected fingers in the current frame and stores the tracked fingers.
  44. /// </summary>
  45. /// <param name="frameData">the current frame</param>
  46. public void trackFingers(FrameData frameData)
  47. {
  48. trackObjects(frameData.DetectedFingers);
  49. frameData.TrackedFingers = getCurrentObjectsWithState(TrackingState.Tracked);
  50. }
  51. /// <summary>
  52. /// Creates a TrackedFinger.
  53. /// </summary>
  54. /// <param name="detectedObject">The detected finger.</param>
  55. /// <returns>a TrackedFinger</returns>
  56. protected override TrackedFinger createTrackedObject(Finger detectedObject)
  57. {
  58. return new TrackedFinger(idPool.getNextUnusedID(), detectedObject, Parameters.FingerTrackerNumFramesDetectedUntilTracked, Parameters.FingerTrackerNumFramesLostUntilDeleted);
  59. }
  60. }
  61. }