Motion based object tracking using opencv

81 views
Skip to first unread message

Dilawar Singh

unread,
Oct 21, 2016, 2:55:34 AM10/21/16
to Web and Coding Club IIT Bombay
Object tracking is both common place and challenging. Each situation demands its own tweaking/implementation/combination of algorithms. Here is a python-opencv script which may be used to track a single moving object (tested with a lab mouse), in static of slightly noisy arena.

One can perform many type of tricks to track a moving object. Matlab has quite extensive documentation on it.

Here it is done as following:

0. Ask user to locate the object she wants to track in first frame, she draws the rectangle around the animal. Call it template_.

1. Now fetch next frame and compute the pixals which have moved. This process is called computing optical flow. Before computing optical flow, apply a bilateral filter (always a good idea).

2. If feature sizes are small, compute optical flow for all pixels otherwise one can use goodFeatureToTrack function from opencv to compute the optical flow on some good pixels (good features).

3. Use the pixals returned from step 2 and compute the center of mas. Near this point, use your selected rectangle (template_)  from step 0 for template matching. The location where match is maximum is the position of current object if it is near to the previous location else reject it. Draw another rectangle of same shape around. Call it template_ now.

4. Repeat. If the algorithm loose the animal, one can click on the mouse to correct it.

The plotting in real-time is low-passed version of trajectory.

The approach doesn't work if animal stops moving ( mouse freezes when they are not comfortable). In such cases, when optical flow is less than a threshold, don't update the location and template_.

The key idea is how to find corners in images.

Dependencies:

   0. python-opencv

This application uses python-gnuplotlib (https://github.com/dkogan/gnuplotlib) to plot the data in real-time using gnuplot. This is very lightweight compared to Animation class of matplotlib. If you ever want to plot simple vector based data during simulation, I'd strongly recommend using it. Set terminal to 'x11' instead of `qt` or `wx` otherwise each call will steal the focus from current terminal.

A demo is here : https://www.youtube.com/watch?v=Eng82w9g9-w
Source code is here : https://github.com/dilawar/object_tracking

     Dilawar
     NCBS Bangalore


Reply all
Reply to author
Forward
0 new messages