Click or drag to resize
Accord.NET (logo)

MatchingTracker Class

Template matching object tracker.
Inheritance Hierarchy

Namespace:  Accord.Vision.Tracking
Assembly:  Accord.Vision (in Accord.Vision.dll) Version: 3.8.0
public class MatchingTracker : IObjectTracker
Request Example View Source

The MatchingTracker type exposes the following members.

Public methodMatchingTracker
Constructs a new MatchingTracker object tracker.
Public propertyExtract
Gets or sets a value indicating whether the tracker should extract the object image from the source. The extracted image should be stored in TrackingObject.
Public propertyRegistrationThreshold
Gets or sets at which similarity threshold the currently tracked object should be re-registered as the new template to look for. This can help track slowly changing objects. Default is 0.99.
Public propertySearchWindow
Gets or sets the current search window.
Public propertyTemplate
Gets or sets the template being chased by the tracker.
Public propertyThreshold
Gets or sets the similarity threshold to determine when the object has been lost. Default is 0.95.
Public propertyTrackingObject
Gets the current location of the object being tracked.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodProcessFrame
Process a new video frame.
Public methodReset
Resets this instance.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Extension Methods
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)

The matching tracker will track the object presented in the search window of the first frame given to the tracker. To reset the tracker and start tracking another object, one can call the Reset method, then set the search window around a new object of interest present the image containing the new object to the tracker.

This is a very simple tracker that cannot handle size changes or occlusion.


The following example shows how to track an moving person from a video using the VideoFileReader class, how to mark the object positions using RectanglesMarker, and save those frames as individual files to the disk.

// Let's test the tracker using a sample video from 
// the collection of test videos in the framework:
TestVideos ds = new TestVideos(basePath);
string fileName = ds["walking.mp4"];

// Now, let's open the video using FFMPEG:
var video = new VideoFileReader();

// And then check the contents of one of the frames:
Bitmap frame = video.ReadVideoFrame(frameIndex: 150);
frame.Save(Path.Combine(basePath, "walking_frame.png"));

// Let's register a template for the bike rider in gray shirt
Rectangle roi = new Rectangle(x: 70, y: 105, width: 28, height: 54);

// initialization
var tracker = new MatchingTracker()
    SearchWindow = roi,
    Threshold = 0.0, // never reset the tracker in case it gets lost
    RegistrationThreshold = 0.95 // re-register the template if we are 95% 
    // confident that the tracked object is indeed the object we want to follow

// Creating bitmaps and locking them is an expensive 
// operation. Instead, let's allocate once and reuse
BitmapData bitmapData = frame.LockBits(ImageLockMode.ReadWrite);
UnmanagedImage unmanagedImage = new UnmanagedImage(bitmapData);

// We will create two color markers: one to show the location of the
// tracked object (red) and another one to show the regions of the image
// that the tracker is looking at (white).
RectanglesMarker objectMarker = new RectanglesMarker(Color.Red);
RectanglesMarker windowMarker = new RectanglesMarker(Color.White);

// Now, for each frame of the video
for (int frameIndex = 0; frameIndex < video.FrameCount; frameIndex++)
    // Read the current frame into the bitmap data
    video.ReadVideoFrame(frameIndex, bitmapData);

    if (frameIndex > 150) // wait until the bike rider enters the scene
        // Feed the frame to the tracker

        // Mark the location of the tracker object in red color
        objectMarker.SingleRectangle = tracker.TrackingObject.Rectangle;
        objectMarker.ApplyInPlace(unmanagedImage); // overwrite the frame

        windowMarker.SingleRectangle = tracker.SearchWindow;
        windowMarker.ApplyInPlace(unmanagedImage); // overwrite the frame

    // Save it to disk
    frame.Save(Path.Combine(basePath, "frame_{0}.png".Format(frameIndex)));

See Also