Capturing movement in recorded video

edited October 2016 in Library Questions

Hi!

I would like to capture movement in a recorded (not captured) video. I tried to transform a code from Learning Processing site as to match it to the recorded video - but it doesnt work, I have some strange erros. The code is as follows:

import processing.video.*;
Movie kot;
PImage prevFrame;
float threshold = 50;

void setup() {
  size(2000, 2000);
  kot = new Movie(this, "breathing.mp4");
  kot.loop();

  prevFrame = createImage(kot.width, kot.height, RGB);
}

void movieEvent(Movie kot) {

  prevFrame.copy(kot, 0, 0, kot.width, kot.height, 0, 0, kot.width, kot.height); // Before we read the new frame, we always save the previous frame for comparison!
  prevFrame.updatePixels();  // Read image from the camera
  kot.read();
}

void draw() {

  loadPixels();
  kot.loadPixels();
  prevFrame.loadPixels();


  for (int x = 0; x < kot.width; x ++ ) {
    for (int y = 0; y < kot.height; y ++ ) {

      int loc = x + y*kot.width;            // Step 1, what is the 1D pixel location
      color current = kot.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();
}
Tagged:

Answers

  • @mtkostecki -- Re: "it doesn't work, I have some strange errors" -- what are they? Are they error messages in the console? If so, please provide them.

    If the sketch is behaving in one way, and you expect it to behave in a different way, could you describe a bit more what you are expecting, and what you see happening instead?

Sign In or Register to comment.