Centroid based object Tracking(vehicle tracking using image processing) ,Counting number of vehicles in video

8 views (last 30 days)
I am trying to count all the vehicles in a video.(or track all the objects in video)
I am trying to track vehicle in a sample video.For that perposes I have differentiated the videos in frames .And I would like to track the objects in every frame.In everyframe I found the old objects and new objects .Now My task is to index every vehicle.Like the image below
In this link one get get the sample video and code. (video file) https://drive.google.com/drive/folders/1kfZJW3knR8oqhq23TD3VGQtbOu4bsGt5
Any kind of help will be highly appreciated.
clc
clear all
close all
%%
Filename='newtraf.mp4';
temp=zeros(2,10);
standarddim=imread('120.jpg');%there is no vehicle in this image
%create a VideoReader object
vid = VideoReader(Filename);
p=0;
figure
while vid.hasFrame %loop through the video file using an index
currentFrame = vid.readFrame(); %read an image
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);
% this mm is the array to store all the centroids of the objects in current frame
for i =2 : size(stats)
mm =[mm transpose(stats(i).Centroid)];
end
end
%% finding new objects and mark them with a id no
[newcar,oldcar]=Currentobj(mm,temp); % I made a function wchich return the new object centoid
newcar(3,:)=p:(size(newcar,2)+p-1);% I am trying to index
%%in this section it demands couple of lines code.
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 for conting new objects in a frame.
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
Thanks in advance.Please help me indexing the objects in frame.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!