Tracker.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using bbiwarg.Graphics;
  7. using bbiwarg.Utility;
  8. namespace bbiwarg.Recognition.Tracking
  9. {
  10. public enum TrackingState
  11. {
  12. None = 0,
  13. Detected = 1,
  14. Tracked = 2,
  15. Lost = 3,
  16. Delete = 4
  17. }
  18. abstract class Tracker<T> where T : TrackableObject
  19. {
  20. public List<TrackableObjectHistory<T>> Histories { get; private set; }
  21. public List<T> TrackedObjects { get; private set; }
  22. private List<SimilarityContainer<T>> similarities;
  23. private int numFramesDetectedUntilTracked;
  24. private int numFramesLostUntilDeleted;
  25. private float minSimilarityForTracking;
  26. public Tracker(int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted, float minSimilarityForTracking)
  27. {
  28. this.numFramesDetectedUntilTracked = numFramesDetectedUntilTracked;
  29. this.numFramesLostUntilDeleted = numFramesLostUntilDeleted;
  30. this.minSimilarityForTracking = minSimilarityForTracking;
  31. reset();
  32. }
  33. public void reset()
  34. {
  35. Histories = new List<TrackableObjectHistory<T>>();
  36. TrackedObjects = new List<T>();
  37. }
  38. public void updateFrame(List<T> detectedObjects)
  39. {
  40. if (Histories.Count == 0)
  41. {
  42. foreach (T detectedObject in detectedObjects)
  43. {
  44. TrackableObjectHistory<T> history = createNewHistory();
  45. history.addObjectToHistory(detectedObject);
  46. Histories.Add(history);
  47. }
  48. }
  49. else
  50. {
  51. updateHistories(detectedObjects);
  52. removeLongLostObjects();
  53. }
  54. }
  55. private void updateHistories(List<T> detectedObjects)
  56. {
  57. TrackedObjects = new List<T>();
  58. List<TrackableObjectHistory<T>> unasignedHistories = new List<TrackableObjectHistory<T>>();
  59. foreach (TrackableObjectHistory<T> history in Histories)
  60. unasignedHistories.Add(history);
  61. List<TrackableObject> unasignedDetectedObjects = new List<TrackableObject>();
  62. foreach (T detectedObject in detectedObjects)
  63. unasignedDetectedObjects.Add(detectedObject);
  64. //asign previously tracked objects to their best fits of detectedObjects
  65. createSimilarities(detectedObjects);
  66. while (similarities.Count > 0)
  67. {
  68. SimilarityContainer<T> maxSimilarity = similarities[0];
  69. TrackableObjectHistory<T> history = maxSimilarity.History;
  70. T detectedObject = maxSimilarity.DetectedObject;
  71. history.addObjectToHistory(detectedObject);
  72. TrackedObjects.Add(detectedObject);
  73. unasignedHistories.Remove(history);
  74. unasignedDetectedObjects.Remove(detectedObject);
  75. removeConcurringSimilarities(maxSimilarity);
  76. }
  77. //add new history for each new unasigned detectedObject
  78. foreach (T unasignedDetectedObject in unasignedDetectedObjects)
  79. {
  80. TrackableObjectHistory<T> newHistory = createNewHistory();
  81. newHistory.addObjectToHistory(unasignedDetectedObject);
  82. Histories.Add(newHistory);
  83. }
  84. //add null-object to each unasigned history (didn't find a best fit during this frame -> lost track)
  85. foreach (TrackableObjectHistory<T> unasignedHistory in unasignedHistories)
  86. {
  87. unasignedHistory.addObjectToHistory(null);
  88. }
  89. }
  90. private void createSimilarities(List<T> detectedObjects)
  91. {
  92. similarities = new List<SimilarityContainer<T>>();
  93. foreach (TrackableObjectHistory<T> history in Histories)
  94. {
  95. foreach (T detectedObject in detectedObjects)
  96. {
  97. SimilarityContainer<T> similarityContainer = new SimilarityContainer<T>(history, detectedObject);
  98. if (similarityContainer.Similarity > minSimilarityForTracking)
  99. {
  100. similarities.Add(similarityContainer);
  101. }
  102. }
  103. }
  104. // sort depending on similarity-value
  105. similarities.Sort((fs1, fs2) => fs2.Similarity.CompareTo(fs1.Similarity));
  106. }
  107. private void removeConcurringSimilarities(SimilarityContainer<T> removeSimilarity)
  108. {
  109. for (int i = similarities.Count - 1; i >= 0; i--)
  110. {
  111. SimilarityContainer<T> similarity = similarities[i];
  112. if (similarity.History == removeSimilarity.History || similarity.DetectedObject == removeSimilarity.DetectedObject)
  113. similarities.RemoveAt(i);
  114. }
  115. }
  116. private void removeLongLostObjects()
  117. {
  118. for (int i = Histories.Count - 1; i >= 0; i--)
  119. {
  120. TrackableObjectHistory<T> history = Histories[i];
  121. if (history.CurrentState == TrackingState.Delete)
  122. {
  123. Histories.RemoveAt(i);
  124. }
  125. }
  126. }
  127. private TrackableObjectHistory<T> createNewHistory()
  128. {
  129. TrackableObjectHistory<T> history = new TrackableObjectHistory<T>(numFramesDetectedUntilTracked, numFramesLostUntilDeleted);
  130. history.Detected += onDetect;
  131. history.Tracked += onTrack;
  132. history.Retracked += onRetrack;
  133. history.Lost += onLoose;
  134. history.Deleted += onDelete;
  135. return history;
  136. }
  137. protected abstract void onDetect(object sender, EventArgs e);
  138. protected abstract void onTrack(object sender, EventArgs e);
  139. protected abstract void onRetrack(object sender, EventArgs e);
  140. protected abstract void onLoose(object sender, EventArgs e);
  141. protected abstract void onDelete(object sender, EventArgs e);
  142. }
  143. }