Thursday, February 10, 2011

Motion Templates

Trying to briefly summarize the description from the "Learning OpenCV"
The motion templates records location of blobs at each time-stamp. It estimates the direction of one(local, segmented) or more blobs (global) with the corresponding 'spatial-timestamp gradients'.
It makes use of a motion-history-image keep track of the latest movements together with timestamps. There is a predefined time window of which history will be kept. The detail mechanics could be referred from Davis99, Bradski00.

The 3 functions that together provide motion template technique:

Sample (motempl.cpp)

The demo application uses simple frame-differencing and thresholding to get the object silhouettes. It uses a cyclical buffer to store the successive frames. The difference is computed with respect to the Nth previous frame where N is the buffer size.

Visualization: The values of motion-history-image (mhi) is converted into the Blue channel of a RGB image by scaling the time-stamp values to fit the 256 discrete levels. This results in a blue-on-black image, where newer and older blobs are shown, overlaid with estimated orientations.  Older blob locations will have faded blue colors. Both global and local orientations are shown.

Test Video 1: Road-Side Camera Video
The orientation values perturbed quite a bit even as the direction of where the car is going does not change. Increasing the cyclical buffer size from 4 to 16 helps. The code comment remarked choosing this value according to video frame rate.

Test Video 2: Aquarium
The perturbation is less serious than the road-side-camera video even with default values. The relatively smooth movements of fish is probably the reason.

G Bradski is one of the authors of the Learning OpenCV and also of the Motion Templates paper?

  • Learning OpenCV
  • Motion segmentation and pose recognition with motion history gradients, Bradski and Davis.
  • Real-time motion template gradients using Intel CVLib, Davis and Bradski


  1. Very special post one could get, useful resource!thanks for sharing this with me!!@bose

    Template cv

  2. hi there. Nice post. But can u please tell how to remove the blue/black background and instead display the normal color image?? Please help.

    1. Here is what I think the easiest way of doing this:
      - Use the original frame instead of black background by calling cvCopy(img, dst, NULL) instead of cvZero();
      - Not drawing the blue mask by removing the call to cvMerge().

  3. This comment has been removed by the author.

  4. I am doing my project named " Fall Detection ".. I want to detect fall when motion of person is large. How do I do this? I mean I have to classify the motion in different ranges like low,medium and high,like that

  5. Thank you so much for sharing your summary of motion templates and the method to display the motion templates in RGB.

    It really works=)

  6. Hi, ButterCookies....

    I would like to ask a question about Motion Template. The motion templates run good in Window platform (I use Window Visual Studion 2010). But, I found that there is a minor error when running the motion template code in Linux platform ( for this one, I run it in Raspberry Pi, with OpenCV installed in it), the problem is that the angle of the moving objects is not displayed in the output window, where the angle always zero.....

    I had check through the codes and it is the same as the one running in Window Visual Studio 2010....and I suspect this is caused by the OpenCV function, that this function is not functioning normally in Linux platform?

    So, can you please advise on this problem...any advice is much appreciated=)