GMU:Eval(nature)/Andre Faupel

From Medien Wiki

Pixel Motion Detection -- File Input

import processing.video.*;
Movie cam;
PImage save, out; 

//// threshold for fine-tuning and artefacts
float thresh = 2;

void setup() {


  ///change to your footage path
  cam = new Movie(this, "E:/vids/MVI_5018.MOV");


  cam.loop();
  cam.jump(random(cam.duration()));
  cam.volume(0);
  
  
  // set windows size to match footage resolution
  size(cam.width, cam.height); 
  

  ///setup previous frame and output-image
  save = createImage(cam.width, cam.height, RGB);
  out = createImage(cam.width, cam.height, RGB);
}

void draw() {

  loadPixels();
  motiondetect();

  out.updatePixels();
  image(out, 0, 0, width, height);
 
}


void motiondetect() {

  cam.loadPixels();
  save.loadPixels();


  ///scann through input pixels
  for (int x = 0; x < cam.width; x ++ ) {
    for (int y = 0; y < cam.height; y ++ ) { 
      int loc = y*cam.width+x;    
      color c = cam.pixels[loc];      
      color c_l = save.pixels[loc]; 

      ///get colors of each pxl
      float r = red(c);
      float g = green(c);
      float b = blue(c);
      ///get colors of each pxl -> prev frame
      float r_l = red(c_l);
      float g_l = green(c_l);
      float b_l = blue(c_l);

      ///compare
      float dif = dist(r, g, b, r_l, g_l, b_l);

      ///exchange pxls to make change visible
      if (dif>thresh) {
        out.pixels[loc] = color (abs(r-r_l), abs(g-g_l), abs(b-b_l));
      } else {
        out.pixels[loc] = color (0);
      }
    }
  }
}


void movieEvent(Movie m) {

  ///get cam image and copy as previous frame
  if (m.available()) {
    save = m.get();
    save.updatePixels();
    m.read();
  }
}