Tracker.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using BBIWARG.Utility;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace BBIWARG.Recognition.Tracking
  5. {
  6. /// <summary>
  7. /// Tracks multiple trackableObjects
  8. /// </summary>
  9. /// <typeparam name="T">The type of the TrackableObjects</typeparam>
  10. /// <typeparam name="TrackedT">The type of the TrackedObject</typeparam>
  11. public abstract class Tracker<T, TrackedT>
  12. where T : TrackableObject
  13. where TrackedT : TrackedObject<T>
  14. {
  15. /// <summary>
  16. /// the trackedObjects
  17. /// </summary>
  18. public List<TrackedT> TrackedObjects;
  19. /// <summary>
  20. /// the unique ID generator
  21. /// </summary>
  22. protected TrackIDPool idPool;
  23. /// <summary>
  24. /// the size of the input image (used to calculate position similarities)
  25. /// </summary>
  26. private ImageSize imageSize;
  27. /// <summary>
  28. /// list of similarities between trackableObjects and trackedObjects
  29. /// </summary>
  30. private List<Similarity<T, TrackedT>> similarities;
  31. /// <summary>
  32. /// Initializes a new instance of the Tracker class.
  33. /// </summary>
  34. /// <param name="imageSize">Size of the input image.</param>
  35. public Tracker(ImageSize imageSize)
  36. {
  37. this.imageSize = imageSize;
  38. reset();
  39. }
  40. /// <summary>
  41. /// Calculates the similarity [0-1] between a TrackedObject and a TrackableObject.
  42. /// </summary>
  43. /// <param name="trackedObject">the tracked object</param>
  44. /// <param name="detectedObject">the detected trackable object</param>
  45. /// <returns>the similarity [0-1]</returns>
  46. public abstract float calculateSimilarity(TrackedT trackedObject, T detectedObject);
  47. /// <summary>
  48. /// Resets the idPool and the trackedObjects.
  49. /// </summary>
  50. public void reset()
  51. {
  52. idPool = new TrackIDPool();
  53. TrackedObjects = new List<TrackedT>();
  54. }
  55. /// <summary>
  56. /// Creates a new TrackedObject with the given TrackableObject as initial object.
  57. /// </summary>
  58. /// <param name="detectedObject">the initial trackableObject</param>
  59. /// <returns>the TrackedObject</returns>
  60. protected abstract TrackedT createTrackedObject(T detectedObject);
  61. /// <summary>
  62. /// Gets a list of TrackableObjects from all TrackedObjects with the given state.
  63. /// </summary>
  64. /// <param name="state">the desired tracking state</param>
  65. /// <returns>the list of trackableObjects with the given state</returns>
  66. protected List<T> getCurrentObjectsWithState(TrackingState state)
  67. {
  68. List<T> objects = new List<T>();
  69. foreach (TrackedT trackedObject in TrackedObjects)
  70. {
  71. if (trackedObject.CurrentState == state)
  72. objects.Add(trackedObject.CurrentObject);
  73. }
  74. return objects;
  75. }
  76. /// <summary>
  77. /// Calculates a similarity [0-1] between two positions with the given maximum relative distance.
  78. /// </summary>
  79. /// <param name="p1">the first position</param>
  80. /// <param name="p2">the second position</param>
  81. /// <param name="maxRelativeDistance">the maximum distance [0-1] relative to the image size (maxAbsoluteDistance = maxRelativeDistance*imageSize.DiagonalLength)</param>
  82. /// <returns>the calculated similarity between the two positions</returns>
  83. protected float getPositionSimilarity(Vector2D p1, Vector2D p2, float maxRelativeDistance)
  84. {
  85. float distance = p1.getDistanceTo(p2);
  86. float maxDistance = maxRelativeDistance * imageSize.DiagonalLength;
  87. float similarity = Math.Max(1 - distance / maxDistance, 0);
  88. return similarity;
  89. }
  90. /// <summary>
  91. /// adds new TrackedObjects or updates and removes TrackedObjects with the new trackableObjects
  92. /// </summary>
  93. /// <param name="detectedObjects">the trackableObjects in the current frame</param>
  94. protected void trackObjects(List<T> detectedObjects)
  95. {
  96. if (TrackedObjects.Count == 0)
  97. {
  98. addNewTrackedObjects(detectedObjects);
  99. }
  100. else
  101. {
  102. updateTrackedObjects(detectedObjects);
  103. removeDeletableTrackedObjects();
  104. }
  105. }
  106. /// <summary>
  107. /// Updates the trackedObjects with the detectedObjects in the current frame. Each TrackedObject is assigned the best fitting detectedObject. Each unassigned detectedObject gets a new instance of a TrackedObject, each unassigned TrackedObject gets updated with null.
  108. /// </summary>
  109. /// <param name="detectedObjects">the trackableObjects in the current frame</param>
  110. protected void updateTrackedObjects(List<T> detectedObjects)
  111. {
  112. List<TrackedT> unassignedTrackedObjects = new List<TrackedT>(TrackedObjects);
  113. List<T> unassignedDetectedObjects = new List<T>(detectedObjects);
  114. createSimilarities(detectedObjects);
  115. while (similarities.Count > 0)
  116. {
  117. Similarity<T, TrackedT> maxSimilarity = similarities[0];
  118. maxSimilarity.TrackedObject.updateFrame(maxSimilarity.DetectedObject);
  119. unassignedDetectedObjects.Remove(maxSimilarity.DetectedObject);
  120. unassignedTrackedObjects.Remove(maxSimilarity.TrackedObject);
  121. removeConcurringSimilarities(maxSimilarity);
  122. }
  123. addNewTrackedObjects(unassignedDetectedObjects);
  124. foreach (TrackedT trackedObject in unassignedTrackedObjects)
  125. {
  126. trackedObject.updateFrame(null);
  127. }
  128. }
  129. /// <summary>
  130. /// creates a new TrackedObject for each trackableObject
  131. /// </summary>
  132. /// <param name="detectedObjects">the unassigned trackableObjects</param>
  133. private void addNewTrackedObjects(List<T> detectedObjects)
  134. {
  135. foreach (T detectedObject in detectedObjects)
  136. {
  137. TrackedT trackedObject = createTrackedObject(detectedObject);
  138. TrackedObjects.Add(trackedObject);
  139. }
  140. }
  141. /// <summary>
  142. /// Creates the list of similarities by creating a similarity for each TrackedObject with each detected TrackableObject.
  143. /// </summary>
  144. /// <param name="detectedObjects">the trackableObjects in the current frame</param>
  145. private void createSimilarities(List<T> detectedObjects)
  146. {
  147. similarities = new List<Similarity<T, TrackedT>>();
  148. foreach (TrackedT trackedObject in TrackedObjects)
  149. {
  150. foreach (T detectedObject in detectedObjects)
  151. {
  152. float similarityValue = calculateSimilarity(trackedObject, detectedObject);
  153. if (similarityValue > 0)
  154. similarities.Add(new Similarity<T, TrackedT>(trackedObject, detectedObject, similarityValue));
  155. }
  156. }
  157. // sort depending on similarity-value
  158. similarities.Sort((s1, s2) => s2.Value.CompareTo(s1.Value));
  159. }
  160. /// <summary>
  161. /// Removes all similarities with the trackedObject or the same trackableObject as the given similarity.
  162. /// </summary>
  163. /// <param name="similarity">the similarity of two assigned objects</param>
  164. private void removeConcurringSimilarities(Similarity<T, TrackedT> similarity)
  165. {
  166. for (int i = similarities.Count - 1; i >= 0; i--)
  167. {
  168. Similarity<T, TrackedT> s = similarities[i];
  169. if (s.TrackedObject == similarity.TrackedObject || s.DetectedObject == similarity.DetectedObject)
  170. similarities.RemoveAt(i);
  171. }
  172. }
  173. /// <summary>
  174. /// Removes all TrackedObjects, which current state is "delete".
  175. /// </summary>
  176. private void removeDeletableTrackedObjects()
  177. {
  178. for (int i = TrackedObjects.Count - 1; i >= 0; i--)
  179. {
  180. TrackedT trackedObject = TrackedObjects[i];
  181. if (trackedObject.CurrentState == TrackingState.Deleted)
  182. {
  183. idPool.setIDUnused(trackedObject.ID);
  184. TrackedObjects.RemoveAt(i);
  185. }
  186. }
  187. }
  188. }
  189. }