Total Pageviews


October 13, 2016

Tracking Suzuka - the ant with Kalman Filter using MATLAB

by 4hathacker  |  in Image Processing at  1:15 PM

Hello everyone...!!!

I have posted about optical flow algorithms for motion detection in the previous article. In this post, I have gently touched motion detection and tracking using Kalman filter function configureKalmanFilter() in MATLAB. 

Kalman filter has many uses, including applications in computer vision, control systems, navigation systems, etc. Kalman filter can be introduced as "a set of mathematical equations that provides an efficient computational (recursive) solution to the least-squares method"[Welch and Bishop]. It is the most commonly used position estimator for solving tracking issues. It assumes a changing state (situation) vector and its associated covariance matrix. Thus, resulting in a process model that updates the state over time. 

So, how a Kalman Filter helped in tracking the target?

It worked in the following steps worth mentioned.
  1. Removal of irrelevant background and detection of changes.
  2. Tracking noisy motion with Kalman filter.
  3. May predict the tracking of object.
For processing in MATLAB, according to the above steps, first create system objects to read video frames, detect foreground objects. I have explained background subtraction in one of the earlier posts which we have utilized here along with blob analysis (I will post on it later). When the object to be tracked is detected, the filter predicts its state at current video frame. And then uses newly detected position to correct the predicted state.

%%%%% Name: Nitin Sharma
%%%%% Code: kalman.m
%%%%% Toolbox Required: Image Processing Toolbox and Computer Vision System
%%%%%                   Toolbox
%%%%% MATLAB version: R2014a
%%%%% Utility: Tracking objects in video, Predicting object motion,
%%%%%           Calculating radius of the object(on your own), 
%%%%%           Detecting Path for a line follower or maze solver

%%% Create system objects for reading video and position
videoReader = vision.VideoFileReader('C:\Users\ntnsh\Desktop\antmaze1.mp4');
videoPlayer = vision.VideoPlayer('Position',[100,100,500,400]);

%%% Detecting object using foreground detector and blob creation for object
foregroundDetector = vision.ForegroundDetector('NumTrainingFrames',10,...
blobAnalyzer = vision.BlobAnalysis('AreaOutputPort',false,...
%%% Start tracking using Kalman Filter
kalmanFilter = []; isTrackInitialized = false;
   while ~isDone(videoReader)
     colorImage  = step(videoReader);

     foregroundMask = step(foregroundDetector, rgb2gray(colorImage));
     detectedLocation = step(blobAnalyzer,foregroundMask);
     isObjectDetected = size(detectedLocation, 1) > 0;

     if ~isTrackInitialized
       if isObjectDetected
         kalmanFilter = configureKalmanFilter('ConstantAcceleration',...
                  detectedLocation(1,:), [1 1 1]*1e5, [25, 10, 10], 25);
         isTrackInitialized = true;
       label = ''; circle = zeros(0,3);
       if isObjectDetected
         trackedLocation = correct(kalmanFilter, detectedLocation(1,:));
         label = 'Suzuka';
%        else
%          trackedLocation = predict(kalmanFilter);
%          label = 'Predicted';
       circle = [trackedLocation, 5];

     colorImage = insertObjectAnnotation(colorImage,'circle',...

The example video is taken from a ant maze harvester testing for IQ of ants to solve and came out of the maze. The object to be tracked was the ant. The video was selected on the basis of constant contrasting with object, plain enough background and color difference with background. Otherwise, tracking object is one of the challenging and hard vision problems with emerging shadows, occlusions, varying illuminations. The ant to be detected in the test video was named as Suzuka, while detection has taken place. Suzuka was my pet name given by my very best friends, Ayush and Nitiksha.

The analysis showed that the motion of object (Suzuka - the ant) detected clearly, surrounded by a red circle. The prediction for the motion can also be obtained. The code mentioned uses specific set of parameters as explained in Mathworks examples for tracking. For any kind of error and improvement in the post, sincere apologies from my side and must comment about them. On manipulating for the code, I obtained for the detected and tracked results as given below.


Like Our Facebook Page

Nitin Sharma's DEV Profile
Proudly Designed by 4hathacker.