/** * Bounce using background subtraction * * based on: * Background Subtraction * by Golan Levin. * * Detect the presence of people and objects in the frame using a simple * background-subtraction technique. To initialize the background, press a key. */

import processing.video.*;

int numPixels;int[] backgroundPixels;Capture video;

Ball ball;

PImage differenceImage;

void setup() { size(640, 480);

ball = new Ball(100,100,0,0);

// This the default video input, see the GettingStartedCapture // example if it creates an error //video = new Capture(this, 160, 120); video = new Capture(this, width, height);

// The following line does the same thing much faster, but is more technical //pixels[i] = 0xFF000000 | (diffR << 16) | (diffG << 8) | diffB; } differenceImage.updatePixels(); // Notify that the pixels[] array has changed

// check brightness and bounce the ball

int c=differenceImage.get((int)ball.x,(int)ball.y);

float b=brightness (c);

if (b>50) { ball.dx= -ball.dx; ball.dy= -ball.dy; }

image(video,0,0);

ball.draw();

println(presenceSum); // Print out the total amount of movement }}

// When a key is pressed, capture the background image into the backgroundPixels// buffer, by copying each of the current frame's pixels into it.void keyPressed() { video.loadPixels(); arraycopy(video.pixels, backgroundPixels); ball.dx=5; ball.dy=9;}