Detect Motion - Moving object with camera input

Hi,

i would like to have the camera position at one corner and moving an object from left-right and right-left in the middle of the screen.

However i struggling to manage not having at the top a full line with the camera input, this comes from loadpixels(), and when i make a move in the camera the ball comes to the top left corner and doesn’t stay in the midle.

import processing.video.*;
 
Capture video;
 
PImage prevFrame;
 
float threshold = 150;
int Mx = 0;
int My = 0;
int ave = 0;
 
int ballX = 800/2;

int ballY = 800/2;

int rsp = 20;
 
void setup() {
  size(800, 800);
  video = new Capture(this, 320, 240, 30);
  video.start();
  prevFrame = createImage(video.width, video.height, RGB);

}
 
void draw() {
 
  background(255,255,255);
 
  if (video.available()) {
 
    prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height); 
    prevFrame.updatePixels();
    video.read();
  }
 
 image(video,0,0);
  loadPixels();
  video.loadPixels();
  prevFrame.loadPixels();
 
  Mx = 0;
  My = 0;
  ave = 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;
        ave++;
      } 
      else {
 
        pixels[loc] = video.pixels[loc];
      }
    }
  }
  fill(255);
  rect(0, 0, width, height);
  if (ave != 0) { 
    Mx = Mx/ave;
    My = My/ave;
  }
  if (Mx > ballX + rsp/2 && Mx > 50) {
    ballX+= rsp;
  }
  else if (Mx < ballX - rsp/2 && Mx > 50) {
    ballX-= rsp;
  }
  if (My > ballY + rsp/2 && My > 50) {
    ballY+= rsp;
  }
  else if (My < ballY - rsp/2 && My > 50) {
    ballY-= rsp;
  }
 
  updatePixels();
  noStroke();
  fill(0, 0, 255);
  ellipse(ballX, ballY, 20, 20);
}

Please format your code. Before you copy it from the PDE, make sure you press ctrl+t to properly format the tab spacings there.

Kf

Thank you for the info. I’ve done it!