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();
}
  • zohar avgar

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

  • admin

    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/

  • Ad

    Thanks alot, this works great :-)

  • Ephrat

    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?

  • http://www.learningprocessing.com Daniel Shiffman

    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.

  • Ephrat

    Thanks!

  • http://www.fsb.hr Knez

    I wonder if You can help me, and describe a little more (or give a hint) how to take an image (which will be stored somewhere on hdd) when web cam detects motion? Thank you anyway!

  • http://www.learningprocessing.com Daniel Shiffman

    Use the save() method!

    video.save(“motion.jpg”);

    along with a conditional to determine the overall motion (see: http://www.learningprocessing.com/examples/chapter-16/example-16-14/) that should do the trick!

    http://processing.org/reference/save_.html

  • http://www.fsb.hr Knez

    Finished!

    Works like charm. Thanks a lot.

  • http://martablicharz.com Marta Blicharz

    Hi,

    I am trying to do a project that uses webcam motion detection to produce a “flashlight” or some concentrated distortion effect in a separate jpeg image (for example a picture of a car). The place of the motion in the webcam would correspond to the position of the distortion of pixels in the other image. Can you please point me to examples that would enable me to do this effectively?

    Thanks for your time!

  • Pepe100

    This is great, i am currently using processing to control video. Iv only been at it a day but would like to use motion capture to trigger a video to play and then pause when no motion is detected, and so on. I have never used processing before today so getting my head round the coding is proving tough. I have another piece of code that can pause the video using a mouse, is there a way to combine parts of both codes to trigger the video?

  • http://twitter.com/shiffman Daniel Shiffman

    I would take a look at the overall motion example, you could use an if statment to play the video only when the overall average motion is above a certain threshold. good luck!

    http://www.learningprocessing.com/examples/chapter-16/example-16-14/

  • Vetruguj2

    ?

  • RIleeyy

    that guy in the picture is extremely attractive <3 somebody tell me who that hottie is, hook me up ;) lol, love you ryan <3

  • sean

    who is ryan?

  • RIleeyy

    that extremely hot guy in that picture, he is my boyfriend <3

  • sugianto thoeng

    Hello, I just found this example and it works with my webcam camera.
    Is there any way to modify the code so it automatically use my Kinect camera not webcam camera?
    So I could detect x,y,and z coordinate using Kinect, it contains depth sensor.
    Thank you for reply.

  • sugianto thoeng

    any help?

  • Gwyan Rhabyt

    There is a great book that covers a lot of ground with processing and kinect called “Making Things See” by Greg Borenstein. It has a number of programs that will help you tie “Learning Procesing” to the Kinect.

  • shiffman

    Great suggestion!

  • sugianto thoeng

    I have that book!

    I have found the way to capture image/video using Kinect camera.

    Could you help me solve my problem here: https://forum.processing.org/topic/can-not-track-any-clicked-color

    Here I combine from that Book with Learning Processing. But it didn’t work :(

  • Lossy

    when i play this sketch all that appears is a white/grey background. can anyone explain this?

  • shiffman

    add video.start() to setup(). Sorry, I need to update these!

  • Sugianto Thoeng

    could you check my code here? https://forum.processing.org/topic/why-it-can-not-do-motion-detection about motion detection.

  • slickric

    Awesome thread, I was wondering if I could get some advice with a project I am working on.

    I am creating a sign language translator via a kinect and processing. My code currently dectects a hand, creates an outline of a hand and puts a dot on your finder. My issue now is, how do I compare the live video stream to a .jpg database that I have created from stills of the kinect video and output the correct letter? I know this is asking alot but a point in the right direction would be appreciated..

  • shiffman

    maybe look at support vector machines on this syllabus? http://makematics.com/syllabus/2012-fall/

  • Pete

    Is there a way you can combine this to the pixelated video capture example 16-7?

  • nathanimate

    Thank you for this sketch! Is there a way so that the difference from the previous frame is portrayed as particles? Similar to the first effect of this video: http://www.youtube.com/watch?v=SH5AoaNfc8o

  • nathanimate

    I forgot to mention that I have managed to change step 5 so as to get the video feed [color (previous)] and the mask (black). So all I need is to transform the mask (black) to particles, I think, or that would be a start anyway.

  • shiffman

    I think the way to think about this is to first write a sketch that creates particles with an arbitrary color at an arbitrary location. Kind of like:

    Particle p = new Particle(mouseX,mouseY,color(255,0,0));

    This is a red particle at the mouse location.

    Then spawn particles at pixel (x,y) locations when they are motion pixels with the color from the video.

    Take a look at the nature of code particle system examples too.

    http://natureofcode.com/book/chapter-4-particle-systems

  • jose

    how would you flip the video feed?

  • nathanimate

    Thank you! I have combined a few stuff I found and did this sketch (adding lifespan as well – not sure if necessary) though I am trying to make it stop blinking! Could you give me a starting point on how to spawn this as mentioned above? I really appreciate it!

    Particle p;

    void setup() {

    size(640,360);

    p = new Particle(new PVector(mouseX, mouseY, color(255,0,0)));

    }

    void draw() {

    background(100);

    //Operating the single Particle

    p.run();

    }

    class Particle {

    PVector location;

    PVector velocity;

    PVector acceleration;

    //A new variable to keep track of how long the particle has been “alive”

    float lifespan;

    Particle(PVector l) {

    location = l.get();

    acceleration = new PVector();

    velocity = new PVector();

    //We start at 255 and count down for convenience

    lifespan = 255;

    }

    void run() {

    update();

    display();

    }

    void update() {

    velocity.add(acceleration);

    location.add(velocity);

    //Lifespan decreases

    lifespan -= 2.0;

    }

    void display() {

    //Since our life ranges from 255 to 0 we can use it for alpha

    stroke(0,lifespan);

    fill(color(255,0,0),lifespan);

    ellipse(mouseX,mouseY,8,8);

    }

    boolean isDead() {

    if (lifespan < 0.0) {

    return true;

    } else {

    return false;

    }

    }

    }

  • shiffman

    you’ll need to add the particle to an ArrayList as to keep track of it (and others) over time. WIth just the single variable you only have that Particle for one frame.