clear all
close all
aviObj = aviread('ex.avi');
T= size(aviObj,2);         %no. of frames in the avi video

for k =1:1:T
    image = aviObj(k).cdata;
    I(:,:,:,k)=image;
end
I=double(I);
start=1;
N=100; % no. of particles

states=zeros(2,N,T);
state_pred=zeros(2,T);
d=zeros(N,T);
A=zeros(2,2);
B=zeros(2,2);

    A(:,:)=[0.99,0.1;0.1,0.1];
    B(:,:)=rand(1,1);

states_pred=I;
xc=50 % initial centroid- x coordinate
yc=1  %initial centroid - y coordinate
%Intialisation
for i=1:N,
    states(1,i,1)=50+50*rand(1)*0.1;
    r0=states(1,i,1);
    r01(i)=floor(r0);
    states(2,i,1)=1+50*rand(1)*0.1;
    c0=states(2,i,1);
    c01(i)=floor(c0);
  %  states_pred(r0,c0,:,1,i);
end;

for t=2:T,
   states_pred(:,:,:,t)=I(:,:,:,t);
    for i=1:N,
        states(:,i,t)=A * states(:,i,t-1) + B*rand(2,1);
        r=states(1,i,t);
        c=states(2,i,t);
        r1(i)=floor(r);
        c1(i)=floor(c);
        
        d(i,t)=((((states_pred(r1(i),c1(i),1,t)*states_pred(r01(i),c01(i),1,t))+(states_pred(r1(i),c1(i),2,t)*states_pred(r01(i),c01(i),2,t))+(states_pred(r1(i),c1(i),3,t)*states_pred(r01(i),c01(i),3,t)))^0.5)^0.5)^0.5;
    end;
    d1=(sum(d(:,t)))^0.5; d1=floor(d1);
    state_pred(1,t)=ceil(states(1,d1,t));
    state_pred(2,t)=ceil(states(1,d1,t));
    
    for i=1:N,
        states(1,i,t)=state_pred(1,t)+50*rand(1)*0.1;
        states(1,i,t)=state_pred(2,t)+50*rand(1)*0.1;
    end;
end;

