example
Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 16-13: Simple motion detection

import processing.video.*;
// Variable for capture device
Capture video;
// Previous Frame
PImage prevFrame;
// How different must a pixel be to be a "motion" pixel
float threshold = 50;

void setup() {
  size(320,240);
  video = new Capture(this, width, height, 30);
  // Create an empty image the same size as the video
  prevFrame = createImage(video.width,video.height,RGB);
}

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, display black
        pixels[loc] = color(0);
      } else {
        // If not, display white
        pixels[loc] = color(255);
      }
    }
  }
  updatePixels();
}

6 Comments

»

  1. How can you capture a frame from this video to be an image?

    Comment by zohar avgar — November 15, 2008 @ 11:11 am

  2. You can do this by writing the pixels to a new PImage object, like in this example:

    http://www.learningprocessing.com/examples/chapter-15/example-15-10/

    Comment by admin — November 16, 2008 @ 8:29 pm

  3. Thanks alot, this works great :-)

    Comment by Ad — September 26, 2009 @ 3:17 am

  4. How can I tell if there is motion taking place in specific parts of the captured video. For example if my size is (320,240) and I want to know if there is motion when 0 < x < 100 ; 120 < x < 220 ; 240 < x < 320?

    Comment by Ephrat — March 19, 2010 @ 9:02 am

  5. You need to keep track of which x,y locations contain motion. For example, you could use an ArrayList and each time you find a motion pixel, add the (x,y) data (perhaps as a PVector) to that ArrayList. Once you have that you could analyze the points an number of ways — look for overall average location of motion, do blob detection, etc.

    Comment by Daniel Shiffman — March 21, 2010 @ 4:35 pm

  6. Thanks!

    Comment by Ephrat — March 22, 2010 @ 7:53 am

Leave a comment