counting cars in an image
Show older comments
I have an image of a traffic image with a lot of cars. Most of the techniques I have Sen depend upon multiple images from same location. But I have only one image. How should I proceed with this kind of image. Image:

Answers (1)
Image Analyst
on 21 Apr 2013
1 vote
Count them using ginput().
5 Comments
Cedric
on 21 Apr 2013
I would have said "count the tires and divide by 4" but some vehicles have 2 or 3 tires, so I think that ginput wins ;)
More seriously, I am wondering if it wouldn't be possible to count windshields, based on some tolerance on the range of RGB channels for spotting grey areas that are far enough from black and white..
Here is my midnight attempt and now I am off for the night:
img = imread('Demo3_a.jpg') ;
thresh_range = 15 ;
thresh_noBlack = 70 ;
thresh_noWhite = 200 ;
rgb_mean = mean(img, 3) ;
rgb_range = range(img, 3) ;
id = rgb_range < thresh_range & ...
rgb_mean > thresh_noBlack & ...
rgb_mean < thresh_noWhite ;
r = img(:,:,1) ; g = img(:,:,2) ; b = img(:,:,3) ;
r(id) = 0 ; g(id) = 0 ; b(id) = 255 ;
img_windshields = cat(3, r, g, b) ;
subplot(2, 1, 1) ;
imshow(img) ;
subplot(2, 1, 2) ;
imshow(img_windshields) ;
Result:

.. the rest is filtering/separating/counting blobs I guess.
This is a problem that is already extremely complex to tackle when pictures taken from multiple angles are available (or video and some tracking based on e.g. Kalman filter), and it seems that you want a flexible solution that can work on single pictures taken at different locations..
I have no solution to be honest; I mentioned this approach based on grey level specifically for the first image, because I thought that it was simple enough to be interesting.
For solving the general problem, you will have to work much much more, and I doubt that anyone here will be willing to spend all the time that it would require for providing you with a solution.
Image Analyst
on 21 Apr 2013
I thought of counting windshields too, but realized it would be a lot more difficult than it first appears. You can't go by shape since there are a lot of rectangular shaped windshields, and a lot of L-shaped windshields because of partial occlusion. And you can't go based on color or darkness because there are lots of other dark things there, like trees, asphalt, or people's heads. And some windshields, like the bus, might be counted as multiple blobs, or, if you threw out small blobs, maybe not counted at all. Even humans asked to count this scene manually will come up with different answers since there might be a lot of close judgment cases involved. But that's why I recommended manual counting since it's too difficult for automatic counting. Even with it's problem it would probably be more accurate. However I realize you probably want an automatic method to get some kind of "congestion factor" for the intersection.
What I thought of as a better metric for the scene is a bulk measurement, one that does not involve counting each and every car. For example if you had a scene with no cars or people at all, then you might be able to compare the busy scene with the empty scene and come up with some kind of "congestion factor" or "busyness index", for example the area fraction that is covered. You could build a calibration curve based on a few pictures that relates area fraction to the manual count. So then, once you have the area fraction, you can use that calibration look up table you made to read off the number of cars and/or people. For all intents and purposes this might be an equivalent, and usable figure of merit that would be easier (possible) to measure. Of course this method also has problems for example the "empty" scene, and congested scene, will look different depending on the time of day (shadow orientation, overall illumination level, headlights on or off, etc.), the season (how trees appear), and the weather (overall illumination level, shadow appearance, noise caused by rain, etc.).
Categories
Find more on Read, Write, and Modify Image in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!