|
@@ -4,28 +4,59 @@ using bbiwarg.Utility;
|
|
|
|
|
|
namespace bbiwarg.Recognition.Tracking
|
|
|
{
|
|
|
- internal abstract class Tracker<T, TrackedT>
|
|
|
+ /// <summary>
|
|
|
+ /// Tracks multiple trackableObjects
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="T">The type of the TrackableObjects</typeparam>
|
|
|
+ /// <typeparam name="TrackedT">The type of the TrackedObject</typeparam>
|
|
|
+ public abstract class Tracker<T, TrackedT>
|
|
|
where T : TrackableObject
|
|
|
where TrackedT : TrackedObject<T>
|
|
|
{
|
|
|
+ /// <summary>
|
|
|
+ /// the size of the input image (used to calculate position similarities)
|
|
|
+ /// </summary>
|
|
|
private ImageSize imageSize;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// list of similarities between trackableObjects and trackedObjects
|
|
|
+ /// </summary>
|
|
|
private List<Similarity<T, TrackedT>> similarities;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// the unique ID generator
|
|
|
+ /// </summary>
|
|
|
protected TrackIDPool idPool;
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// the trackedObjects
|
|
|
+ /// </summary>
|
|
|
public List<TrackedT> TrackedObjects;
|
|
|
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Initializes a new instance of the Tracker class.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="imageSize">Size of the input image.</param>
|
|
|
public Tracker(ImageSize imageSize)
|
|
|
{
|
|
|
this.imageSize = imageSize;
|
|
|
reset();
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Resets the idPool and the trackedObjects.
|
|
|
+ /// </summary>
|
|
|
public void reset()
|
|
|
{
|
|
|
idPool = new TrackIDPool();
|
|
|
TrackedObjects = new List<TrackedT>();
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// adds new TrackedObjects or updates and removes TrackedObjects with the new trackableObjects
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="detectedObjects">the trackableObjects in the current frame</param>
|
|
|
protected void trackObjects(List<T> detectedObjects)
|
|
|
{
|
|
|
if (TrackedObjects.Count == 0)
|
|
@@ -39,6 +70,10 @@ namespace bbiwarg.Recognition.Tracking
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 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.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="detectedObjects">the trackableObjects in the current frame</param>
|
|
|
protected void updateTrackedObjects(List<T> detectedObjects)
|
|
|
{
|
|
|
List<TrackedT> unassignedTrackedObjects = new List<TrackedT>(TrackedObjects);
|
|
@@ -63,6 +98,10 @@ namespace bbiwarg.Recognition.Tracking
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// creates a new TrackedObject for each trackableObject
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="detectedObjects">the unassigned trackableObjects</param>
|
|
|
private void addNewTrackedObjects(List<T> detectedObjects)
|
|
|
{
|
|
|
foreach (T detectedObject in detectedObjects)
|
|
@@ -72,6 +111,10 @@ namespace bbiwarg.Recognition.Tracking
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Creates the list of similarities by creating a similarity for each TrackedObject with each detected TrackableObject.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="detectedObjects">the trackableObjects in the current frame</param>
|
|
|
private void createSimilarities(List<T> detectedObjects)
|
|
|
{
|
|
|
similarities = new List<Similarity<T, TrackedT>>();
|
|
@@ -81,19 +124,27 @@ namespace bbiwarg.Recognition.Tracking
|
|
|
foreach (T detectedObject in detectedObjects)
|
|
|
{
|
|
|
float similarityValue = calculateSimilarity(trackedObject, detectedObject);
|
|
|
- Similarity<T, TrackedT> similarity = new Similarity<T, TrackedT>(trackedObject, detectedObject, similarityValue);
|
|
|
- if (similarity.Value > 0)
|
|
|
- similarities.Add(similarity);
|
|
|
- }
|
|
|
+ if (similarityValue > 0)
|
|
|
+ similarities.Add(new Similarity<T, TrackedT>(trackedObject, detectedObject, similarityValue));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// sort depending on similarity-value
|
|
|
similarities.Sort((s1, s2) => s2.Value.CompareTo(s1.Value));
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Calculates the similarity [0-1] between a TrackedObject and a TrackableObject.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="trackedObject">the tracked object</param>
|
|
|
+ /// <param name="detectedObject">the detected trackable object</param>
|
|
|
+ /// <returns></returns>
|
|
|
public abstract float calculateSimilarity(TrackedT trackedObject, T detectedObject);
|
|
|
|
|
|
-
|
|
|
+ /// <summary>
|
|
|
+ /// Removes all similarities with the trackedObject or the same trackableObject as the given similarity.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="similarity">the similarity of two assigned objects</param>
|
|
|
private void removeConcurringSimilarities(Similarity<T, TrackedT> similarity)
|
|
|
{
|
|
|
for (int i = similarities.Count - 1; i >= 0; i--)
|
|
@@ -104,6 +155,9 @@ namespace bbiwarg.Recognition.Tracking
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Removes all TrackedObjects, which current state is "delete".
|
|
|
+ /// </summary>
|
|
|
private void removeDeletableTrackedObjects()
|
|
|
{
|
|
|
for (int i = TrackedObjects.Count - 1; i >= 0; i--)
|
|
@@ -117,6 +171,11 @@ namespace bbiwarg.Recognition.Tracking
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Gets a list of TrackableObjects from all TrackedObjects with the given state.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="state">the desired tracking state</param>
|
|
|
+ /// <returns>the list of trackableObjects with the given state</returns>
|
|
|
protected List<T> getCurrentObjectsWithState(TrackingState state)
|
|
|
{
|
|
|
List<T> objects = new List<T>();
|
|
@@ -128,8 +187,20 @@ namespace bbiwarg.Recognition.Tracking
|
|
|
return objects;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Creates a new TrackedObject with the given TrackableObject as initial object.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="detectedObject">the initial trackableObject</param>
|
|
|
+ /// <returns>the TrackedObject</returns>
|
|
|
protected abstract TrackedT createTrackedObject(T detectedObject);
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Calculates a similarity [0-1] between two positions with the given maximum relative distance.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="p1">the first position</param>
|
|
|
+ /// <param name="p2">the second position</param>
|
|
|
+ /// <param name="maxRelativeDistance">the maximum distance [0-1] relative to the image size (maxAbsoluteDistance = maxRelativeDistance*imageSize.DiagonalLenght)</param>
|
|
|
+ /// <returns>the calculated similarity between the two positions</returns>
|
|
|
protected float getPositionSimilarity(Vector2D p1, Vector2D p2, float maxRelativeDistance)
|
|
|
{
|
|
|
float distance = p1.getDistanceTo(p2);
|