Page 1 of 1

Video Input, difference threshold, and Ball Bounce

PostPosted: Tue Nov 25, 2014 6:40 pm
by BenChang
Code: Select all
/**
* 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);
 
  // Start capturing the images from the camera
  video.start(); 
 
  numPixels = video.width * video.height;
  // Create array to store the background image
  backgroundPixels = new int[numPixels];
  // Make the pixels[] array available for direct manipulation
 
  differenceImage = createImage (640,480,RGB);
 
  differenceImage.loadPixels();
}

void draw() {
  if (video.available()) {
    video.read(); // Read a new video frame
    video.loadPixels(); // Make the pixels of video available
    // Difference between the current frame and the stored background
    int presenceSum = 0;
    for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
      // Fetch the current color in that location, and also the color
      // of the background in that spot
      color currColor = video.pixels[i];
      color bkgdColor = backgroundPixels[i];
      // Extract the red, green, and blue components of the current pixel's color
      int currR = (currColor >> 16) & 0xFF;
      int currG = (currColor >> 8) & 0xFF;
      int currB = currColor & 0xFF;
      // Extract the red, green, and blue components of the background pixel's color
      int bkgdR = (bkgdColor >> 16) & 0xFF;
      int bkgdG = (bkgdColor >> 8) & 0xFF;
      int bkgdB = bkgdColor & 0xFF;
      // Compute the difference of the red, green, and blue values
      int diffR = abs(currR - bkgdR);
      int diffG = abs(currG - bkgdG);
      int diffB = abs(currB - bkgdB);
      // Add these differences to the running tally
      presenceSum += diffR + diffG + diffB;
      // Render the difference image to the screen
     
     
     
      differenceImage.pixels[i] = color(diffR, diffG, diffB);
     
     
      // 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;
}

The Ball class for this example

PostPosted: Tue Nov 25, 2014 6:40 pm
by BenChang
Code: Select all
class Ball
{
  float x;
  float y;

  float dx;
  float dy;

  float radius;
  float dr;    // radius change

  Ball (float x, float y, float dx, float dy)
  {
    this.x=x;
    this.y=y;
    radius = 20;
    dr=0;

    this.dx=dx;
    this.dy=dy;
   
   
  }

  void draw ()
  {
    // update velocity
    dy=dy+.3;
   
    // update position
    x=x+dx;
    y=y+dy;

    if (y>480)
    {
      y=473;
      dy=-dy;
    }
    if (y<0)
    {
       dy=-dy;
    }
    if (x>640 || x<0)
    {
      dx=-dx;
    }
    ellipse(x, y, radius, radius);
  }
}


Bounce off of red objects

PostPosted: Tue Nov 25, 2014 6:41 pm
by BenChang
Code: Select all
import processing.video.*;

Capture cam;
color c;

Ball ball;
void setup()
{
   size(640,480);
   cam = new Capture(this,640,480);
   cam.start();
   
   ball = new Ball(100,100,2,0);
   
}

void draw()
{
   if (cam.available())
       cam.read();
   
   c=cam.get((int)ball.x,(int)ball.y);
      fill(c);

  float r=red(c);   
   
   if (r>220)
   {
      ball.dx= -ball.dx;
      ball.dy= -ball.dy;
   }
  image (cam,0,0);
   
   
   ball.draw();
}



Bounce off bright objects

PostPosted: Tue Nov 25, 2014 6:41 pm
by BenChang
Code: Select all
import processing.video.*;

Capture cam;
color c;

color targetColor;

Ball ball;
void setup()
{
   size(640,480);
   cam = new Capture(this,640,480);
   cam.start();
   
   ball = new Ball(100,100,2,0);
   
}

void draw()
{
   if (cam.available())
       cam.read();
   
   c=cam.get((int)ball.x,(int)ball.y);
      fill(c);

   float h1 = hue(c);
   float h2 = hue(targetColor);
   
   float s1 = saturation(c);
   float s2 = saturation(targetColor);
   
   float b1 = brightness(c);
   float b2 = brightness(targetColor);
   
   if (b1>240)
   {
      ball.dx= -ball.dx;
      ball.dy= -ball.dy;
   }
  image (cam,0,0);
 
  ball.draw();
 
  fill(targetColor);
  rect(0,0,20,20);
 
}

void mousePressed()
{
   targetColor = cam.get(mouseX,mouseY);
}




Pick a color to bounce off with the mouse

PostPosted: Tue Nov 25, 2014 6:41 pm
by BenChang
import processing.video.*;

Capture cam;
color c;

color t

Ball ball;
void setup()
{
size(640,480);
cam = new Capture(this,640,480);
cam.start();

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

}

void draw()
{
if (cam.available())
cam.read();

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

float r=red(c);

if (r>180)
{
ball.dx= -ball.dx;
ball.dy= -ball.dy;
}
image (cam,0,0);


ball.draw();
}