Simple Motion Detection in AE

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
vertigo5
Posts: 2
Joined: September 23rd, 2014, 3:47 am

September 23rd, 2014, 6:09 am

I am trying to write a script that takes a clip and analyzes how much "motion" is in it by simple motion detection (comparing pixels on consecutive frames).
Roughly I want to use the same idea that this Processing sketch uses,
however I'm not sure if the methods used here exist or are possible in AE,
any tips or hints on feasibility would be greatly appreciated!

This is the compressed Processing code of what I plan to do (just left the relevant parts):

Code: Select all


// Previous Frame
PImage prevFrame;
// How different must a pixel be to be a "motion" pixel
float threshold = 50;

void draw() {
 // Capture video
  if (video.available()) {
    // Save previous frame for motion detection!!
    prevFrame.copy(video,0,0,video.width,video.height,0,0,video.width,video.height); // Before we read the new frame, we always save the previous frame for comparison!
    prevFrame.updatePixels();
    video.read();
  }
  
  loadPixels();
  video.loadPixels();
  prevFrame.loadPixels();
  
  // Begin loop to walk through every pixel
  for (int x = 0; x < video.width; x ++ ) {
    for (int y = 0; y < video.height; y ++ ) {
      
      int loc = x + y*video.width;            // Step 1, what is the 1D pixel location
      color current = video.pixels[loc];      // Step 2, what is the current color
      color previous = prevFrame.pixels[loc]; // Step 3, what is the previous color
      
      // Step 4, compare colors (previous vs. current)
      float r1 = red(current); float g1 = green(current); float b1 = blue(current);
      float r2 = red(previous); float g2 = green(previous); float b2 = blue(previous);
      float diff = dist(r1,g1,b1,r2,g2,b2);
      
      // Step 5, How different are the colors?
      // If the color at that pixel has changed, then there is motion at that pixel.
      if (diff > threshold) { 
        // If motion, do something
     } else {
        // If not do something else
      }
    }
  }
  updatePixels();
}
beginUndoGroup
Posts: 81
Joined: November 27th, 2012, 6:41 am

September 25th, 2014, 2:21 am

After Effects scripting has no access to pixel color in layers.
Expressions have though : layer.sampleImage() (see this post for instance : http://blogs.adobe.com/aftereffects/200 ... eimag.html).

So your script should add a color control to the layer, then in a loop over pixel coordinates (x and y)
- put a sampleImage expression in the color control corresponding to (x,y)
- harvest the result.
At the end of the sccript, remove the color control.
Needless to say, if the video is wide and/or long, this will be very very slow.
Xavier.
vertigo5
Posts: 2
Joined: September 23rd, 2014, 3:47 am

September 25th, 2014, 4:40 am

Thanks for the answer, with this new information I think an AE script is not the ideal environment for what I have in mind,
so I will try other approaches such as Processing, open Frameworks, or Max Jitter.

Thanks!
Post Reply