TrackedObject.cs 5.7 KB

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