TrackedObject.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. detectedObject.setTracked(ID);
  70. CurrentObject = detectedObject;
  71. LastObject = detectedObject;
  72. CurrentState = TrackingState.Detected;
  73. PreviousState = TrackingState.Undefined;
  74. NumFramesInCurrentState = 1;
  75. }
  76. /// <summary>
  77. /// Adds the detectedObject to the tracking history and updates the current state.
  78. /// </summary>
  79. /// <param name="detectedObject">the best fitting trackableObject in the current frame</param>
  80. public virtual void updateFrame(T detectedObject)
  81. {
  82. if (detectedObject != null)
  83. {
  84. detectedObject.setTracked(ID);
  85. LastObject = detectedObject;
  86. }
  87. TrackingState newState = getNewState(detectedObject);
  88. if (!wasTrackedBefore && newState == TrackingState.Tracked)
  89. wasTrackedBefore = true;
  90. PreviousState = CurrentState;
  91. CurrentState = newState;
  92. CurrentObject = detectedObject;
  93. if (PreviousState == newState)
  94. NumFramesInCurrentState++;
  95. else
  96. NumFramesInCurrentState = 1;
  97. }
  98. /// <summary>
  99. /// Calculates the new tracking state given the new trackableObject.
  100. /// </summary>
  101. /// <param name="detectedObject">the best fitting trackableObject in the current frame</param>
  102. /// <returns>the new TrackingState</returns>
  103. private TrackingState getNewState(T detectedObject)
  104. {
  105. TrackingState newState = TrackingState.Undefined;
  106. if (detectedObject != null)
  107. {
  108. switch (CurrentState)
  109. {
  110. case TrackingState.Lost:
  111. if (wasTrackedBefore)
  112. newState = TrackingState.Tracked;
  113. else
  114. newState = TrackingState.Detected;
  115. break;
  116. case TrackingState.Tracked:
  117. newState = TrackingState.Tracked;
  118. break;
  119. case TrackingState.Detected:
  120. if (NumFramesInCurrentState >= numFramesDetectedUntilTracked)
  121. newState = TrackingState.Tracked;
  122. else
  123. newState = TrackingState.Detected;
  124. break;
  125. }
  126. }
  127. else if (CurrentState == TrackingState.Lost && NumFramesInCurrentState >= numFramesLostUntilDeleted)
  128. newState = TrackingState.Deleted;
  129. else
  130. newState = TrackingState.Lost;
  131. return newState;
  132. }
  133. }
  134. }