Thursday, April 7, 2016

Traffic Count using OpenCV Python 'Moments' method | Code | Video

I'm currently working on various simple image processing modules at my internship and got permission to showcase my simple traffic count using a slightly different method than conventional blob analysis or contours method, which is moments method. The moments method works on the geometries of the image like centroid, area etc.

The following is the algorithm that i've applied:
  1. Get the Video frame by frame, thereby applying processing techniques for every frame/image.
  2. Apply Background subtraction. Generally when static background is present as in the case of a static CCTV camera, to get a binary image for the moving vehicles. We simply subtracted a static background from the current image thus leaving us with just the moving vehicles.
  3. Next, apply moments function to each frame, therefore getting the centroid of the moving vehicles( binary image).
  4. Finally assign a certain range of pixel values in (x,y) form on the frame so that when the centroid of the moment area crosses this range the counter increments by one which is reflected in the counter text. 


Here's a small video on how it works.


  1. Hi, what will happen of more than 1 car is detected?

  2. Hi Robert, This method is very primitive and basic and has its fair share of limitation and uses if more cars are there then the centroid will be in between those cars and the answer won't be accurate.
    I've applied better methods than this, but haven't uploaded on blog.

    1. can u send me the code for that. I am currently working on this project. please help me

  3. mas saya mau tanya, nentuin x dan y dari jalan nya gimana ya.

  4. can you upload those better methods sir,,need help

  5. sir error occure in erode function .... can you tell me how to solve this error ?

  6. Is there any changes in backgroundSubstractorMOG() method it throws up an error, and also using backgroundSubstractorMOG2() it throws a type error


Featured Post

ROS Autonomous Navigation Bot - [Clerkbot] - Initial Tests

Finally it took me three months to fully come up with this robot and just a fun fact, it took me a month to just tune the ocean of paramet...