Tutorial: Counting Road Traffic Capacity with OpenCV

Andrey Nikishaev
Sep 16, 2017 · 3 min read

Want become an expert in Computer Vision & Object Detection?

Subscribe on New Practical Course


Today I will show you very simple but powerful example of how to count traffic capacity with the algorithm that you can run on devices.

As always, here is full code of this project

Also, I recommend you to read my first article about road traffic classification, cause it cool and also maintains part of base pipeline architecture that we will you in this project.

So this algorithm works in 4 steps:
1. Get frame edges.
2. Blur them to get the more filled area.
3. Binary threshold blurred the image.
4. Overlap threshold image with ROI(you mask where you count) and count black pixels/white pixels which gives you traffic capacity.

You can see each step on image below:

Edges

Here we use CLAHE equalization to remove noise from the image that can occur on cheap/old cameras at night. It not the best thing, but gives a better result.

Then we use Canny Edge Detector to get edges from the image. We invert it to get white background(just for visual convenient)

Blur

We use basic blur with bilateral filtering which removes some color noise and gives better segmentation.

Threshold

The last filter is a binary threshold which we use to get only white and black pixels which give as our segmentation on car/not car.

Counting

And the last simple step just divides the number of black pixels with the number of white pixels to get traffic capacity.

Problems

Because of some camera noise and different outdoor conditions, accuracy may not so big ~ 70–85%.
But that is not a big problem because we can set min/max limits, or use additional filtering based on light conditions and for example amount of edges on some test ROI(for example some white rectangle).
And also this data mainly used as additional data, so only relative values are needed.

Why is this data needed?

All data is needed, even if you don’t know how to use it right now :)
In our case with this data, we can say why traffic was limited at some point in time.

Why not use one big algorithm that will do all the work?

The main thing that you must remember working on Data Science projects that they should not only be effective in the domain but also cost-effective for business which includes speed, memory usage, scalability, one hour runtime costs for one task and in scale.
There are no algorithms that run efficiently in any conditions, for example, sort algorithms that used for local projects will never be used in big data projects because will be slow, and big data algorithms will not be used for local projects cause also will be slow(cause they faster only on big amount of data).
So you should build your pipeline with understanding your projects and business limitation.

If you have questions/ideas don’t hesitate to post them in comments.

PS: Also I got few emails where people asked me if it’s ok to critic my code/ideas. So yes, it’s ok, and moreover, I ask you to do so, so we all can get benefit from this.

Support

Become a Patron and support our community to make more interesting articles & tutorials

Get interesting articles every day — Subscribe on Telegram Channel

Machine Learning World

The best about Machine Learning, Computer Vision, Deep Learning, Natural language processing and other.

Andrey Nikishaev

Written by

Entrepreneur, Software Developer, Machine Learning and Computer Vision Researcher. Contact me: creotiv@gmail.com

Machine Learning World

The best about Machine Learning, Computer Vision, Deep Learning, Natural language processing and other.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade