Disclaimer: I posted this question on StackOverFlow but learned that it is better suited here.

I am a bit new to image processing so I'd like to ask you about finding the optimal solution for my problem, not help for code. I couldn't think of a good idea yet so wanted to ask your advice.

I'm working on a project using OpenCV on counting the vehicles from a video file or a live camera. Other people working on such a project generally track the moving objects then count them but instead of that, I wanted to work with a different viewpoint; asking user to set a ROI (Region of interest) on the video window and work only for this region (for some reasons, like to not deal with the whole frame and some performance increase), as seen below. (The user can set more than one ROI and the user is asked to set the height of the ROI about two times of a normal car by sense of proportion.)


I've done some basic progress so far, like background updating, morphological filters, thresholding and getting the moving object as a binary image something like below.


After doing them, I tried to count the white pixels of the final thresholded foreground frame and estimate whether it was a car or not by checking the total white pixels number (I set a lower bound by a static calculation by knowing the height of ROI). To illustrate, I drew a sample graphic:


As you can see, it was easy to calculate the white pixels and check if it draws a curve by the time and determining whether a car or something like noise.


I was quite successful until two cars passed through my ROI together at the same time. My algorithm incorrectly counted them as one car. I tried different approaches for this problem and similar to this like long vehicles but I haven't found a satisfactory solution.


Is it possible to handle this task by this approach of pixel value counting? If not, what do you suggest?

  • $\begingroup$ Your problem is the reason people use tracking. Also, do you want cars that are moving together (say, a car with a trailer) to be counted as one vehicle or two? $\endgroup$
    – Emre
    May 21, 2012 at 22:40
  • $\begingroup$ Firstly, thanks for edition. I dont expect %100 accuracy, cars with trailers can be ignored. but if it is two seperate cars going very close to each other, I need to make the program distinguish both of them, but I have no idea . $\endgroup$ May 22, 2012 at 0:37

1 Answer 1


You can keep track of the last 10-15 frames and count the number of white pixel of these frames. You can differentiate if it is a vehicle or not, since the graph will be high for 10-15 frames you will be able to say that it is a vehicle otherwise it is noise.

To count multiple cars in multiple side by side lanes you can check the pixels column wise, I mean vertically. For the time being you must be checking the pixels horizontally for white pixels.

  • $\begingroup$ Yes you are right about checking the vertical lines but how ? Can I determine the entry point of vehicles by checking the first line of my ROI ? $\endgroup$ May 22, 2012 at 0:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.