Tracker.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Collections.Generic;
  2. using System;
  3. using bbiwarg.Utility;
  4. namespace bbiwarg.Recognition.Tracking
  5. {
  6. internal abstract class Tracker<T, TrackedT>
  7. where T : TrackableObject
  8. where TrackedT : TrackedObject<T>
  9. {
  10. private ImageSize imageSize;
  11. private List<Similarity<T, TrackedT>> similarities;
  12. protected TrackIDPool idPool;
  13. public List<TrackedT> TrackedObjects;
  14. public Tracker(ImageSize imageSize)
  15. {
  16. this.imageSize = imageSize;
  17. reset();
  18. }
  19. public void reset()
  20. {
  21. idPool = new TrackIDPool();
  22. TrackedObjects = new List<TrackedT>();
  23. }
  24. protected void trackObjects(List<T> detectedObjects)
  25. {
  26. if (TrackedObjects.Count == 0)
  27. {
  28. addNewTrackedObjects(detectedObjects);
  29. }
  30. else
  31. {
  32. updateTrackedObjects(detectedObjects);
  33. removeDeletableTrackedObjects();
  34. }
  35. }
  36. protected void updateTrackedObjects(List<T> detectedObjects)
  37. {
  38. List<TrackedT> unassignedTrackedObjects = new List<TrackedT>(TrackedObjects);
  39. List<T> unassignedDetectedObjects = new List<T>(detectedObjects);
  40. createSimilarities(detectedObjects);
  41. while (similarities.Count > 0)
  42. {
  43. Similarity<T, TrackedT> maxSimilarity = similarities[0];
  44. maxSimilarity.TrackedObject.updateFrame(maxSimilarity.DetectedObject);
  45. unassignedDetectedObjects.Remove(maxSimilarity.DetectedObject);
  46. unassignedTrackedObjects.Remove(maxSimilarity.TrackedObject);
  47. removeConcurringSimilarities(maxSimilarity);
  48. }
  49. addNewTrackedObjects(unassignedDetectedObjects);
  50. foreach (TrackedT trackedObject in unassignedTrackedObjects)
  51. {
  52. trackedObject.updateFrame(null);
  53. }
  54. }
  55. private void addNewTrackedObjects(List<T> detectedObjects)
  56. {
  57. foreach (T detectedObject in detectedObjects)
  58. {
  59. TrackedT trackedObject = createTrackedObject(detectedObject);
  60. TrackedObjects.Add(trackedObject);
  61. }
  62. }
  63. private void createSimilarities(List<T> detectedObjects)
  64. {
  65. similarities = new List<Similarity<T, TrackedT>>();
  66. foreach (TrackedT trackedObject in TrackedObjects)
  67. {
  68. foreach (T detectedObject in detectedObjects)
  69. {
  70. float similarityValue = calculateSimilarity(trackedObject, detectedObject);
  71. Similarity<T, TrackedT> similarity = new Similarity<T, TrackedT>(trackedObject, detectedObject, similarityValue);
  72. if (similarity.Value > 0)
  73. similarities.Add(similarity);
  74. }
  75. }
  76. // sort depending on similarity-value
  77. similarities.Sort((s1, s2) => s2.Value.CompareTo(s1.Value));
  78. }
  79. public abstract float calculateSimilarity(TrackedT trackedObject, T detectedObject);
  80. private void removeConcurringSimilarities(Similarity<T, TrackedT> similarity)
  81. {
  82. for (int i = similarities.Count - 1; i >= 0; i--)
  83. {
  84. Similarity<T, TrackedT> s = similarities[i];
  85. if (s.TrackedObject == similarity.TrackedObject || s.DetectedObject == similarity.DetectedObject)
  86. similarities.RemoveAt(i);
  87. }
  88. }
  89. private void removeDeletableTrackedObjects()
  90. {
  91. for (int i = TrackedObjects.Count - 1; i >= 0; i--)
  92. {
  93. TrackedT trackedObject = TrackedObjects[i];
  94. if (trackedObject.CurrentState == TrackingState.Deleted)
  95. {
  96. idPool.setIDUnused(trackedObject.ID);
  97. TrackedObjects.RemoveAt(i);
  98. }
  99. }
  100. }
  101. protected List<T> getCurrentObjectsWithState(TrackingState state)
  102. {
  103. List<T> objects = new List<T>();
  104. foreach (TrackedT trackedObject in TrackedObjects)
  105. {
  106. if (trackedObject.CurrentState == state)
  107. objects.Add(trackedObject.CurrentObject);
  108. }
  109. return objects;
  110. }
  111. protected abstract TrackedT createTrackedObject(T detectedObject);
  112. protected float getPositionSimilarity(Vector2D p1, Vector2D p2, float maxRelativeDistance)
  113. {
  114. float distance = p1.getDistanceTo(p2);
  115. float maxDistance = maxRelativeDistance * imageSize.DiagonalLength;
  116. float similarity = Math.Max(1 - distance / maxDistance, 0);
  117. return similarity;
  118. }
  119. }
  120. }