brgvv_MotionDetectorProcessing-/ballmotion.pde
import processing.video.*;
 
Capture video;
 
PImage prevFrame;
 
float threshold = 150;
int Mx = 0;
int My = 0;
int avg = 0;
 
int ballX = width/8;
int ballY = height/8;
int r = 5;
 
void setup() {
  size(320, 240);
  video = new Capture(this, width, height, 30);
  video.start();
  prevFrame = createImage(video.width, video.height, RGB);
}
 
void draw() {
 
 
  if (video.available()) {
 
    prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height); 
    prevFrame.updatePixels();
    video.read();
  }
 
  loadPixels();
  video.loadPixels();
  prevFrame.loadPixels();
 
  Mx = 0;
  My = 0;
  avg = 0;
 
 
  for (int x = 0; x < video.width; x ++ ) {
    for (int y = 0; y < video.height; y ++ ) {
 
      int loc = x + y*video.width;            
      color current = video.pixels[loc];      
      color previous = prevFrame.pixels[loc]; 
 
 
      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);
 
 
      if (diff > threshold) { 
        pixels[loc] = video.pixels[loc];
        Mx += x;
        My += y;
        avg++;
      } 
      else {
 
        pixels[loc] = video.pixels[loc];
      }
    }
  }
  fill(255);
  rect(0, 0, width, height);
  if (avg != 0) { 
    Mx = Mx/avg;
    My = My/avg;
  }
  if (Mx > ballX + r/2 && Mx > 50) {
    ballX+= r;
  }
  else if (Mx < ballX - r/2 && Mx > 50) {
    ballX-= r;
  }
  if (My > ballY + r/2 && My > 50) {
    ballY+= r;
  }
  else if (My < ballY - r/2 && My > 50) {
    ballY-= r;
  }
 
  updatePixels();
  noStroke();
  fill(255, 0, 0);
  ellipse(ballX, ballY, 20, 20);
}
