TrackedObject.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. namespace BBIWARG.Recognition.Tracking
  2. {
  3. /// <summary>
  4. /// The possible tracking state values
  5. /// </summary>
  6. public enum TrackingState
  7. {
  8. Undefined = 0,
  9. Detected = 1,
  10. Tracked = 2,
  11. Lost = 3,
  12. Deleted = 4
  13. }
  14. /// <summary>
  15. /// A tracked object is identified by several TrackableObjects in different frames that have a high similarity value and are considered to represent the same object over time.
  16. /// </summary>
  17. /// <typeparam name="T">The type of the trackable object</typeparam>
  18. public abstract class TrackedObject<T> where T : TrackableObject
  19. {
  20. /// <summary>
  21. /// the number of consecutive frames the object has to be detected until its state changes to tracked
  22. /// </summary>
  23. private int numFramesDetectedUntilTracked;
  24. /// <summary>
  25. /// the number of consecutive frames the object has to be lost until its state changes to delete
  26. /// </summary>
  27. private int numFramesLostUntilDeleted;
  28. /// <summary>
  29. /// indicates whether the object has been tracked before
  30. /// </summary>
  31. private bool wasTrackedBefore;
  32. /// <summary>
  33. /// a reference to the current TrackableObject
  34. /// </summary>
  35. public T CurrentObject { get; private set; }
  36. /// <summary>
  37. /// the current tracking state
  38. /// </summary>
  39. public TrackingState CurrentState { get; private set; }
  40. /// <summary>
  41. /// the objects track id
  42. /// </summary>
  43. public int ID { get; private set; }
  44. /// <summary>
  45. /// a reference to the last TrackableObject
  46. /// </summary>
  47. public T LastObject { get; private set; }
  48. /// <summary>
  49. /// the number of consecutive frames in the current state
  50. /// </summary>
  51. public int NumFramesInCurrentState { get; private set; }
  52. /// <summary>
  53. /// the previous tracking state
  54. /// </summary>
  55. public TrackingState PreviousState { get; private set; }
  56. /// <summary>
  57. /// Initializes a new instance of the TrackedObject class.
  58. /// </summary>
  59. /// <param name="id">The track id.</param>
  60. /// <param name="detectedObject">The detected object.</param>
  61. /// <param name="numFramesDetectedUntilTracked">The number of consecutive frames detected until it is considered to be tracked.</param>
  62. /// <param name="numFramesLostUntilDeleted">The number of consecutive frames lost until the object will be deleted.</param>
  63. public TrackedObject(int id, T detectedObject, int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted)
  64. {
  65. ID = id;
  66. this.numFramesDetectedUntilTracked = numFramesDetectedUntilTracked;
  67. this.numFramesLostUntilDeleted = numFramesLostUntilDeleted;
  68. wasTrackedBefore = false;
  69. CurrentObject = detectedObject;
  70. LastObject = detectedObject;
  71. CurrentState = TrackingState.Detected;
  72. PreviousState = TrackingState.Undefined;
  73. NumFramesInCurrentState = 1;
  74. }
  75. /// <summary>
  76. /// Adds the detectedObject to the tracking history and updates the current state.
  77. /// </summary>
  78. /// <param name="detectedObject">the best fitting trackableObject in the current frame</param>
  79. public virtual void updateFrame(T detectedObject)
  80. {
  81. if (detectedObject != null)
  82. {
  83. detectedObject.setTracked(ID);
  84. LastObject = detectedObject;
  85. }
  86. TrackingState newState = getNewState(detectedObject);
  87. if (!wasTrackedBefore && newState == TrackingState.Tracked)
  88. wasTrackedBefore = true;
  89. PreviousState = CurrentState;
  90. CurrentState = newState;
  91. CurrentObject = detectedObject;
  92. if (PreviousState == newState)
  93. NumFramesInCurrentState++;
  94. else
  95. NumFramesInCurrentState = 1;
  96. }
  97. /// <summary>
  98. /// Calculates the new tracking state given the new trackableObject.
  99. /// </summary>
  100. /// <param name="detectedObject">the best fitting trackableObject in the current frame</param>
  101. /// <returns>the new TrackingState</returns>
  102. private TrackingState getNewState(T detectedObject)
  103. {
  104. TrackingState newState = TrackingState.Undefined;
  105. if (detectedObject != null)
  106. {
  107. switch (CurrentState)
  108. {
  109. case TrackingState.Lost:
  110. if (wasTrackedBefore)
  111. newState = TrackingState.Tracked;
  112. else
  113. newState = TrackingState.Detected;
  114. break;
  115. case TrackingState.Tracked:
  116. newState = TrackingState.Tracked;
  117. break;
  118. case TrackingState.Detected:
  119. if (NumFramesInCurrentState >= numFramesDetectedUntilTracked)
  120. newState = TrackingState.Tracked;
  121. else
  122. newState = TrackingState.Detected;
  123. break;
  124. }
  125. }
  126. else if (CurrentState == TrackingState.Lost && NumFramesInCurrentState >= numFramesLostUntilDeleted)
  127. newState = TrackingState.Deleted;
  128. else
  129. newState = TrackingState.Lost;
  130. return newState;
  131. }
  132. }
  133. }