Project: Car Tracking for velocity measurement

Posted by Damian Dailisan on October 28, 2014 Tags: Python Image Processing   3 minute read

For my project, I want to try tracking cars in a video to determine velocities of vehicle on the road. Just a simple code with a rough estimate of velocity is what I need for now, I don’t need to match velocities to specific cars yet. But first, I need to be able to track an object across a video, which is simply a series of still images.

Tracking based on Image Segmentation

We can start things of by tracking a ping-pong ball undergoing projectile motion to calculate the acceleration due to gravity. We simply converted the video to a series of images using ffmpeg as a decoder. Then we took a set of images which corresponds to one projectile motion.

We select the ROI by color, just crop the pingpong ball. And then use Image segmentation using histogram back projection.

Now we have a collection of points which represent the ball. We can improve the quality of our collection of points using morphological operations to clean it as well. We can get the centroid using this following snippet of code:

IsCalculated = CreateFeatureStruct(%f);
IsCalculated.Centroid = %t;
C = AnalyzeBlobs(im, IsCalculated);
Centroid = C(1).Centroid;
y = [y Centroid(2)];
x = [x Centroid(1)];

Doing this for the rest of the images, we will get a collection of x and y positions, which we can plot using excel. Of course, these x and y positions need to be scaled properly, using

yscale = .05/20 // meters to pixels
xscale = .05/14 // meters to pixels
Y = -yscale*(y-240);
X = xscale*(x);

If we fit a second order polynomial to the y positions and time, we should get

The term $-4.8871$ is actually $-\frac{g}{2}$. Hence, we have $g=9.77\,\frac{m}{s^2}$.

Tracking Based on Image Difference

Vehicles will not always have the same color on the road. The method of image segmentation is good, but we need something more robust if we want to be able to make a generic car velocity estimator just by video processing. A possible alternative method is using image difference. We need an image of just the background.

Then we take the difference of every frame with this frame. To binarize the difference , we set a threshold value, which will determine if pixels in the difference will be black or white.

We’ll get another blob, from which we can get its position using centroids. We can get the following graph below:

The term $-4.8871$ is actually $-\frac{g}{2}$. Hence, we have $g=9.78\,\frac{m}{s^2}$. This is a similar result as with using image segmentation by color. We are now ready to track car videos.

Tracking cars from a video

A video frame would look like the image shown above. Since we are working with a video captured by a different person, we do not have a background that we can readily use as a reference. Instead, we utilize the difference of subsequent frames, and hope to find the outlines of the vehicles on the road. However, as shown in the next two figures, this is not as straightforward as the previous methodology of subtracting a reference background.

tracks