TrackedObject.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using bbiwarg.Utility;
  7. namespace bbiwarg.Recognition.Tracking
  8. {
  9. public enum TrackingState
  10. {
  11. Undefined = 0,
  12. Detected = 1,
  13. Tracked = 2,
  14. Lost = 3,
  15. Deleted = 4
  16. }
  17. public abstract class TrackedObject<T> where T : TrackableObject
  18. {
  19. private int numFramesDetectedUntilTracked;
  20. private int numFramesLostUntilDeleted;
  21. private bool wasTrackedBefore;
  22. public int ID { get; private set; }
  23. public T CurrentObject { get; private set; }
  24. public T LastObject { get; private set; }
  25. public TrackingState CurrentState { get; private set; }
  26. public TrackingState PreviousState { get; private set; }
  27. public int NumFramesInCurrentState { get; private set; }
  28. public TrackedObject(int id, T detectedObject, int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted)
  29. {
  30. ID = id;
  31. this.numFramesDetectedUntilTracked = numFramesDetectedUntilTracked;
  32. this.numFramesLostUntilDeleted = numFramesLostUntilDeleted;
  33. wasTrackedBefore = false;
  34. CurrentObject = detectedObject;
  35. LastObject = detectedObject;
  36. CurrentState = TrackingState.Detected;
  37. PreviousState = TrackingState.Undefined;
  38. NumFramesInCurrentState = 1;
  39. }
  40. public abstract float calculateSimilarity(T detectedObject);
  41. public virtual void updateFrame(T detectedObject)
  42. {
  43. if (detectedObject != null)
  44. {
  45. detectedObject.setTracked(ID);
  46. LastObject = detectedObject;
  47. }
  48. TrackingState newState = getNewState(detectedObject);
  49. if (!wasTrackedBefore && newState == TrackingState.Tracked)
  50. wasTrackedBefore = true;
  51. PreviousState = CurrentState;
  52. CurrentState = newState;
  53. CurrentObject = detectedObject;
  54. if (PreviousState == newState)
  55. NumFramesInCurrentState++;
  56. else
  57. NumFramesInCurrentState = 1;
  58. }
  59. private TrackingState getNewState(T detectedObject)
  60. {
  61. TrackingState newState = TrackingState.Undefined;
  62. if (detectedObject != null)
  63. {
  64. switch (CurrentState)
  65. {
  66. case TrackingState.Lost:
  67. if (wasTrackedBefore)
  68. newState = TrackingState.Tracked;
  69. else
  70. newState = TrackingState.Detected;
  71. break;
  72. case TrackingState.Tracked:
  73. newState = TrackingState.Tracked;
  74. break;
  75. case TrackingState.Detected:
  76. if (NumFramesInCurrentState >= numFramesDetectedUntilTracked)
  77. newState = TrackingState.Tracked;
  78. else
  79. newState = TrackingState.Detected;
  80. break;
  81. }
  82. }
  83. else if (CurrentState == TrackingState.Lost && NumFramesInCurrentState >= numFramesLostUntilDeleted)
  84. newState = TrackingState.Deleted;
  85. else
  86. newState = TrackingState.Lost;
  87. return newState;
  88. }
  89. protected float getPositionSimilarity(Vector2D p1, Vector2D p2, float maxDistance)
  90. {
  91. float distance = p1.getDistanceTo(p2);
  92. float similarity = Math.Max(1 - distance / maxDistance, 0);
  93. return similarity;
  94. }
  95. }
  96. }