Monday, 25 February 2013

Vehicle Counting System in Matlab


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

So now it is done,Thank You :)
Press like and leave comment

6 comments:

  1. I have tried running camshift algorithm. It shows error because of the usage of aviread properties. My matlab version is R2008a. It doesn't support aviread function. So, I have to use only mmreader. How to append the frames using mmreader instead of addframes in aviread????????????

    ReplyDelete
  2. ??? Attempt to execute SCRIPT cen as a function:
    C:\Users\project\Documents\MATLAB\cen.m

    Error in ==> kalman at 110
    rectangle('Position',cen,'EdgeColor','r','LineWidth',5);

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Thanks, this is very informative and i think it's a very useful tips. I admire the time and effort you put into your well-detailed nice blog. I hope to see more of your write ups. Keep it up!!!

    ReplyDelete
  5. what is centroid?thanks

    ReplyDelete
  6. FLAG=0;
    if FLAG==0
    no=no+1;
    flag=1;
    frame=frame+1;
    end
    else
    frame=frame+1;
    end

    where i must put this code in kalman filter?please answer my question..thnks

    ReplyDelete