1. Input a video of vehicles ex. test.avi
2. Now extract the frames and save in a specific folder
Get the code here, one of my previous post
3. Detect the motion of the vehicle in the video
Get the code here
Now if everything has gone alright,we can begin.
We just make very simple changes in the previous code to make the thing work perfectly.
Step 1 : Watch the redbox around the moving object.Now insert this code in there
rectangle('Position',cen,'EdgeColor','r','LineWidth',5);
Here 'cen' is the centroid
Step 2: Now the most important step,insert this code after that
txt=text(no, strcat('car: ', num2str(round(no))));
set(txt, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'black');
It will display the number of cars. 'no' is a variable which is set to '0' at first.But when ever a motion is detected in the video 'no' will be incremented by 1,that is how we get the vehicle count.
But the basic problem what you will face in this method that is,here we are considering frame after frame,so the 'no' variable gets incremented each time,but thats not what we want,we want the actual count,so we are gonna need a FLAG variable which indicates if 'no' is incremented by 1 for a particular vehicle then it wont increment for the specific one again.
like this,
FLAG=0;
if FLAG==0
no=no+1;
flag=1;
frame=frame+1;
end
else
frame=frame+1;
end