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();
}
  • http://chetanbhasin.com/ Chetan Bhasin

    Which programming language is this? I landed on this page through Google so don’t really know what’s going on.

  • Kye

    is this apply to processing 2.1.1??

  • Nguyen Tung Son

    Mine still doesn’t work after I add video.start()? Can you help me?

  • christian

    is there a way to get this working with P3D?

  • Sebastian Vox

    put the “video.start();” line at the end of “void setup()” just before the ” } “

  • Pip Kolmar

    This code has been incredibly helpful to me, but I am currently experiencing a snag. Instead of comparing the last frame to current frame, I am comparing the current frame to a still image to trigger an action based on when someone steps into the frame. I am only able to pick up drastic pixel differences in the bottom right hand corner of the window. If someone walks in from the left, it is only after their image travels across and they hit the right edge of the window that the action is triggered. I can follow up with additional info and code if necessary.

  • yl

    Is this issue ever solved for you?

  • amber

    is there any chance that i can do that?just like the special effect in the vedio.
    i wonder how the code works.

  • amber

    if u see this message,can u please sent me the code sample?sent it to “chocolate0417@gmail.com” thank you!

  • Jonathanljl

    Hi everyone,

    I am in real need of help here. Currently I am on a project and I don’t seem to get anything out from what the project want and now the deadline I near and I really hope someone kind soul other there could help to to create the progra that my project needs .

    My email is rave_dome@hotmail.com

  • http://www.jevonmahoneyid.blogspot.co.uk Jevon Mahoney

    I’m desperately trying to find a way for the camera to detect a single image, which will be added to the library, within its field. I have the camera initiated. Not particularly sure this is the correct thread but i’ve found nothing on this anywhere.

  • Goda

    Hello guys, I am having an issue with the code. I am trying to put 2 videos playing at the same time, one colour, one black and white, at the same time have motion detection (it’s a projection, more issues to come, because it multiplies the screen when webcam is faced to projection), however, I tried to make a mask, running black and white video as main one, and colour under it, so once motion is detected, the part that its captured lets the colour video come up (not entire screen, but just parts where motion is detected), and it seems not to work I am getting desperate, because my dead line is in a week!!!! Please Please Please! here is the code:

    //motion detection and video capture

    import processing.video.*;

    Capture video;

    boolean cheatScreen;

    PImage prevFrame;

    Movie myMovie;

    Movie myMovie2;

    float threshold = 50;

    void setup() {

    size(1280, 720);

    myMovie = new Movie(this, “short.mp4″);

    myMovie.loop();

    myMovie2 = new Movie(this, “bnw.mp4″);

    myMovie2.loop();

    video = new Capture(this, width, height, 30);

    prevFrame = createImage(video.width, video.height, RGB);

    video.read();

    }

    void draw() {

    image(myMovie, 0, 0);

    image(myMovie2, 0, 0);

    // 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 threshold) {

    // If motion, display black

    //pixels[loc] = color(0);

    myMovie.loop();

    } else {

    // If not, display white

    //pixels[loc] = color(255);

    myMovie2.loop();

    }

    }

    }

    }

    }

  • Çağhan Büyükavcı

    thanks for sharing this example
    but i got an issue; i made it work within the Processing IDE but when i exported it for Windows (8.1) and double-click the executable, nothing happens… no warnings, no errors, it just doesn’t show. i can’t figured it out