# Count the vehicles flow that pass over one bridge

I live in an apartment near a bridge. From my window I can see the cars traveling over the bridge as you can see below:

Is there a relatively simple manner to count each car pass over the bridge? Using an android cell phone pointed to the bridge behind a lunette and using a API for image processing like Open CV?

• Welcome to DSP.SE! From that angle, it'll be hard, especially in changing lighting conditions (night, fog, rain, etc.). How accurate do you need it to be?
– Peter K.
May 24 '16 at 20:22
• Yeah! Welcome! Since you've already mentioned OpenCV, I suppose you've looked at their tutorials and already got an approach? So you might want to share that! May 24 '16 at 22:13
• Its not necessary to be accurate. I want to do it just for fun. I have some Ideas what OpenCV and related libraries can do. But I don't know the effort to achieve this. Is the current "state of art" of these APIs allows us to make this sort of thing without deal without dealing with complex algorithms? My questions is about how hard it would be. Not how to do. If it is not that simply, I move on... May 25 '16 at 0:25

It is relatively easy to create the "sensing element" for the cars but difficult to get a short term valid count for the number of vehicles.

As far as the sensing element is concerned, this is a simple pulse counter. The signal that creates the "pulse", comes from the optical flow of the vehicles as they pass by a region of interest in front of the camera.

You basically need to differentiate between successive frames, rectify the differentiated signal and integrate it with respect to time with the possible addition of a threshold.

When you differentiate between two successive frames (for instance $I_n, I_{n-1}$, where $I$ is a two-dimensional array of pixel brightness values at some color depth and $n$ is the frame number (integer)) you are basically removing everything that is not moving and highlighting parts of the image that appear to have changed between time instances $n$ and $n-1$.

At this point, you don't really care about the direction of the change and this is why you need to take the absolute difference: $Q_n = |I_n-I_{n-1}|$

If you now focus on a small region of interest of the $Q$ series of images, you will notice that when nothing happens, that region is pitch black and when an object MOVES in front of it the region "lights up" because of all the differences. Therefore, what you can do now is obtain the mean value of all the pixels of this small region of interest (over the $Q$ sequence) and observe it in time.

This will be producing a "pulse" every time that something disturbs the region of interest. The duration of the pulse (in time) will be equal to the duration of the disturbance.

Now, if we were to think in terms of "counting cars over a bridge", you can position your region of interest around a light pole. Past the first frame, the actual scene will not matter anymore because the light frame doesn't move (actually it does move because the whole bridge vibrates slightly and the light poles are tuning in but let's assume that this is negligible in your case). If you do this then you can count disturbances under the light pole during the day and also during the night in a similar way (rather than having to rely on the irregular shapes of the car lights).

Fine, so now we have a way to transform passing vehicles into "pulses". What is the problem? Count the pulses and you get the number of vehicles.

Not exactly, so here is where things become difficult:

1. There are more than one lanes per direction. Therefore, it is quite possible that two cars fit through the region of interest at the same time thus creating just one pulse. Even worse, a car can be masked by a bus (be behind it) and never be counted. The only way to counteract this is to repeat the same ultra simple "counting" process at multiple points on the bridge (multiple light poles) and obtain an average count. (Of course, a car can travel at the same speed as a bus and remain hidden from the perspective of the camera for as long as it remains on the bridge...but there is no remedy for this....from your point of view).

2. As you can see, this pulse counting is actually counting disturbances, not necessarily vehicles. Here are a few disturbances that have nothing to do with the vehicles: Atmospheric disturbance (smoke, sea evaporation, anything that causes a slight flickering), movement of the camera, birds and light signals. Imagine for example what happens when the bridge lights will come on. Your pulse counter will register a pulse (because before, the area under the light pole was darker and now, it is lighter) but this pulse is going to be extremely brief. The only way to counteract spurious movement somewhere across the image is by the solution to #1 (multiple counting points + average) and the only way to counteract invalid pulses is by setting thresholds. So, very short pulses (in time) are not counted and very weak pulses (in terms of magnitude) are not counted too.

This is the principle. You can do this with openCV, python, (openCV through Python), MATLAB, even off-line video processing.

Hope this helps.