Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Your life may be easier if the line corresponded to a row of the image. That way you just do:

drawContours( drawing1, contours, i, Scalar(255,0,0), -2, 8, hierarchy, 0, Point() );//note that thickness is negative - that fills the contour.
if (countNonZero(Drawing1.row(300))>0)
//car #i is crossing row 300

The problem with this type of algorithm is that you would need to keep track that you don't count the same car in the next frame.

If only one car at a time can cross the line, you would just need some sort of flag that only allows the counter to increment if there weren't any lit pixels on the line in the previous frame. If multiple cars can cross the line simultaneously, you will need to correlate between car i in one frame and car j in the other. This is much harder

If the cars are relatively slow you could just look at the blob centroids and find the nearest one in the previous frame or maybe one that has moved in a predefined direction. If they are fast (i.e. motion between frames is larger than average distance between cars) you may need a more complex algorithm.