Thursday 24 January 2013

Motion Tracking using Kalman Filter Matlab Code



clear all; close all; clc

%% Read video into MATLAB using aviread
video = aviread('Roof1.AVI');
nframes = length(video);

% Calculate the background image by averaging the first 5 images
temp = zeros(size(video(1).cdata));
[M,N] = size(temp(:,:,1));
for i = 1:10
    temp = double(video(i).cdata) + temp;
end
imbkg = temp/10;

% Initialization for Kalman Filtering
centroidx = zeros(nframes,1);
centroidy = zeros(nframes,1);
predicted = zeros(nframes,4);
actual = zeros(nframes,4);

% % Initialize the Kalman filter parameters
% R - measurement noise,
% H - transform from measure to state
% Q - system noise,
% P - the status covarince matrix
% A - state transform matrix

R=[[0.2845,0.0045]',[0.0045,0.0455]'];
H=[[1,0]',[0,1]',[0,0]',[0,0]'];
Q=0.01*eye(4);
P = 100*eye(4);
dt=1;
A=[[1,0,0,0]',[0,1,0,0]',[dt,0,1,0]',[0,dt,0,1]'];

% loop over all image frames in the video
kfinit = 0;
th = 38;
for i=1:nframes
  imshow(video(i).cdata);
  hold on
  imcurrent = double(video(i).cdata);
 
  % Calculate the difference image to extract pixels with more than 40(threshold) change
  diffimg = zeros(M,N);
  diffimg = (abs(imcurrent(:,:,1)-imbkg(:,:,1))>th) ...
      | (abs(imcurrent(:,:,2)-imbkg(:,:,2))>th) ...
      | (abs(imcurrent(:,:,3)-imbkg(:,:,3))>th);

  % Label the image and mark
  labelimg = bwlabel(diffimg,4);
  markimg = regionprops(labelimg,['basic']);
  [MM,NN] = size(markimg);

  % Do bubble sort (large to small) on regions in case there are more than 1
  % The largest region is the object (1st one)
  for nn = 1:MM
      if markimg(nn).Area > markimg(1).Area
          tmp = markimg(1);
          markimg(1)= markimg(nn);
          markimg(nn)= tmp;
      end
  end

  % Get the upper-left corner, the measurement centroid and bounding window size
  bb = markimg(1).BoundingBox;
  xcorner = bb(1);
  ycorner = bb(2);
  xwidth = bb(3);
  ywidth = bb(4);
  cc = markimg(1).Centroid;
  centroidx(i)= cc(1);
  centroidy(i)= cc(2);

  % Plot the rectangle of background subtraction algorithm -- blue
  hold on
  rectangle('Position',[xcorner ycorner xwidth ywidth],'EdgeColor','b');
  hold on
  plot(centroidx(i),centroidy(i), 'bx');

  % Kalman window size
  kalmanx = centroidx(i)- xcorner;
  kalmany = centroidy(i)- ycorner;

  if kfinit == 0
      % Initialize the predicted centroid and volocity
      predicted =[centroidx(i),centroidy(i),0,0]' ;
  else
      % Use the former state to predict the new centroid and volocity
      predicted = A*actual(i-1,:)';
  end
  kfinit = 1;

  Ppre = A*P*A' + Q;
  K = Ppre*H'/(H*Ppre*H'+R);
  actual(i,:) = (predicted + K*([centroidx(i),centroidy(i)]' - H*predicted))';
  P = (eye(4)-K*H)*Ppre;

  % Plot the tracking rectangle after Kalman filtering -- red
  hold on
  rectangle('Position',[(actual(i,1)-kalmanx) (actual(i,2)-kalmany) xwidth ywidth],'EdgeColor','r','LineWidth',1.5);
  hold on
  plot(actual(i,1),actual(i,2), 'rx','LineWidth',1.5);
  drawnow;
end


16 comments:

  1. thanks. it works perfectly in my code.

    actually i got error like this one ::

    ?? Error using ==> readavi Unable to locate decompressor to decompress video stream
    Error in ==> aviread at 64 X = readavi(info.Filename,-1);

    you have to download the codec, so you can play the video in aviread function :)

    ReplyDelete
    Replies
    1. what is that codec means please tell me in details and where its get from.

      Delete
    2. you can download the codec pack. here the example http://filehippo.com/download_klite_codec_pack/
      and install it. it means the video not decompressed on your computer/laptop.

      if it doesnt work, you have to install the latest codec.

      Delete
  2. and anybody have the reference of "kalman filter" method. maybe any journal, paper, etc?
    please give me any clue and send to iqbal.aghe@gmail.com please.

    thanks anyway :)

    ReplyDelete
    Replies
    1. I get the same error for some videos...but for some videos it works perfectly...actually i think its a problem with video codec....the link you gave i also tried this but same prob again happens...

      Delete
  3. Once again a great informative post. Thank you so much for taking the time to share this exciting information. But, I would be grateful to you if you could provide some more information about heat map issue. Thanks !

    ReplyDelete
  4. Doesnt work for me. I am getting a box around the whole image.

    ReplyDelete
  5. video = aviread('Roof1.AVI');
    i get stuck to this error

    can you send me the link of this video....! at kesarartcircle@gmail.com

    ReplyDelete
  6. Hi,
    I'm using Matlab 2013a version and to read the video, I've used VideoReader instead of using aviread but there is problem with line...temp = zeros(size(video(1).cdata));.
    Error= No appropriate method, property,
    or field cdata for class.
    VideoReader.Anyone knows how to fix this?

    ReplyDelete
    Replies
    1. Hi, me too get the same error,... do u know how to fix it???

      Delete
  7. can u send me the code with database for moving vehicle detection and tracking to this mail id sunsid1989@gmail.com

    ReplyDelete
  8. I am using windows xp OS. K-lite codec is not getting installed properly, and also i'm having GOM player, MP4 player,but the coding is not working properly and i'm getting this error.
    Error using ==> aviread at 84
    Unable to locate decompressor to decompress video stream

    ReplyDelete
  9. video = aviread('Roof1.AVI');
    i get this error

    can you send me the link of this video....! at shahpooja.311@gmail.com

    ReplyDelete
  10. In matlab command video type aviinfo('filename.avi')
    It will give you all the information about tha video file named filename.avi
    From these information find out which compression is used and then try downloading that codec from google.
    If you like this comment just type "Mia san Mia"

    ReplyDelete
  11. hello sir code is running perfectly..bt i m using it for car tracking so it tracks all cars on the road ,i want to track single car what changes i have to do in this code

    ReplyDelete
  12. Hi,
    I'm using Matlab 2016a version and to read the video, I've used VideoReader instead of using aviread but there is problem with line...temp = zeros(size(video(1).cdata));.
    Error= No appropriate method, property,
    or field cdata for class.
    VideoReader.Anyone knows how to fix this?

    ReplyDelete