Automatic motion trail effect with OpenCV

You know that effect in some videos where objects moving in the scene leave a trail of their old position?

It is usually called “echo effect” or “trail effect” or “motion trail”, some examples of it are:

Effects like this are normally created in post-production: isolating in some way the objects which will leave the trail, most of the time recording with a green-screen, and then messing around with frames.

However, since I am too lazy to be an artist, I tried to re-create it automatically and in real-time using some computer vision to isolate the moving objects.

opencv_trail_effect is an experiment using OpenCV to draw a trail after performing foreground segmentation.

Segmentation in opencv_trail_effect can done using either thresholding or background subtraction.

A few conditions are required to create a decent effect:

  1. the camera should not move;
  2. the lighting in the scene should be quite stable (when doing background subtraction it is recommended to disable auto-gain in the camera, and avoid any artifact introduced by the power line frequency);
  3. the subject should enter the scene only after the background learning phase has finished, i.e. after opencv_trail_effect has shown the preview window.

Examples of use

The effect in “Blame It On The Boogie” could be described as a short faded trail and can be achieved with this command line:

$ ./opencv_trail_effect -l 12 -s background -d fadeaccumulate

The effect in “WTF?” is equivalent to an infinite trail in which the isolated foreground is copied on the background and stays there, it can be achieved with this command line:

$ ./opencv_trail_effect -l -1 -s background -d copy

The effect in “L'Anima Vola” seems to have a “catch-up” behavior; that one can be done by drawing the trail in reverse, with this command line:

$ ./opencv_trail_effect -l 30 -s background -d copy -r

Another effect seen in movies can be obtained by doing the average of the frames in the trail, possibly with the most recent frame drawn on top:

$ ./opencv_trail_effect -l 10 -s background -d average -B

Here is a video showing the examples from above:

I am thinking about making this effect more widely available in open source video frameworks, like GStreamer, maybe via frei0r which GStreamer can already use.

I see that there is already a Light Graffiti effect in frei0r and the trail effect can be seen as a generalization of it, so maybe some code can be shared.

Any suggestion, or help, is welcome.

Side note

These effects can bring to mind other cool effects in music videos, like the frame shuffling effect in Peter Gabriel - Sledgehammer, that one can be done with the nervousTV plugin from effectv or frei0r, for example via GStreamer with:

$ gst-launch-1.0 -v v4l2src ! videoconvert ! frei0r-filter-nervous ! videoconvert ! autovideosink

Which could be fun to use in video calls too, but this is another story.

CommentsSyndicate content

Hi Antonio, Talking about

Alan's picture

Hi Antonio,

Talking about OpenCV, do you know about libccv?

It is similar to OpenCV but for embedded systems:

I never used it yet, but I hope to take a look at it soon!

Ah, never heard of libccv,

ao2's picture

Ah, never heard of libccv, I'll check it out, thanks.

Ciao ciao, Antonio

Hello Antonio, breaking my

Anton's picture

Hello Antonio,

breaking my head to compile your code,
but can't figure out how to fix getopt function which is not available under Windows.

I see you updated this project lately. Can you help with port to Windows please?
Hardcoding parameters will do. Much appreciated anyways!

I'll try to make the code

ao2's picture

I'll try to make the code compile for Windows.

Ciao, Antonio

Hey. I don't know whether you

John's picture

Hey. I don't know whether you update this page/project anymore, but i'd like to ask, what is the trail_t that can be found in your Trails.hpp file. Is it something related to Linux only?

Hi John, trail_t is not

ao2's picture

Hi John,

trail_t is not linux-specific: it is a new type defined as a list of pointers to Frame objects.

I defined the new type as an abbreviation, to avoid writing the original type definition every time.

My C++ is a little C-ish sometimes, and it is usual to suffix types with _t in C.

Ciao, Antonio

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Enter the code without spaces.