clc
clear all
close all
Filename='newtraf.mp4';
temp=zeros(2,10);
standarddim=imread('120.jpg');
vid = VideoReader(Filename);
p=0;
figure
while vid.hasFrame 
    currentFrame = vid.readFrame(); 
    imshow(currentFrame);
    
    im3=(currentFrame-standarddim);
    im3=im3(:,:,1);
    im3=im3>34;
    s=strel('disk',5);
    im3=imdilate(im3,s);
    title('Drawing Bounding Box over original image')
    hold on;
    
    stats = regionprops(im3);
    if(size(stats)>0)
        mm=transpose(stats(1).Centroid); 
        
        for i =2 : size(stats)
            mm =[mm transpose(stats(i).Centroid)];
        end
    end
    
    [newcar,oldcar]=Currentobj(mm,temp); 
    
    newcar(3,:)=p:(size(newcar,2)+p-1);
    
    
    
    temp=mm;
    
    
    for i=1:length(stats)
        if(stats(i).Area>300)
            rectangle('Position', stats(i).BoundingBox, 'LineWidth', 2, 'EdgeColor', 'g');
            
        end
    end
    pause(1.0/vid.FrameRate);
end
function [new_object,old_object]= Currentobj(neww_frame,previous_frame)
[rn,cn]=size(neww_frame);
[rt,ct]=size(previous_frame);
old_object=[0;0];
new_object=[0;0];
for i=1:cn
    count=0;
    for j=1:ct
        if (distnce(neww_frame(:,i),previous_frame(:,j))<10)
            old_object=[old_object neww_frame(:,i)];
            count=count+1;
            break;
        end
    end
    if(count==0)
        new_object=[new_object neww_frame(:,i)];
    end
end
new_object(:,1)=[];
old_object(:,1)=[];
end
function d =distnce(p,q)
d = sqrt( (p(1)-q(1))^2 + (p(2)-q(2))^2 );
end