Recitation 010

I attended the Video+Image Manipulation workshop.

I used the made a bouncing ball that will move with the movement tracked in the camera.

The basic idea is actually tracking the movement in the camera by recognizing pixel color changing and turn these “movement happening” pixels into black. To restore to a normal vision, we simply cover the original image on the pixel layer.

code:

// based on Example 16-13: Simple motion detection
// http://learningprocessing.com/examples/chp16/example-16-13-MotionPixels
//https://youtu.be/QLHMtE5XsMs

import processing.video.*;
String[] cameras = Capture.list();
// Variable for capture device
Capture video;
// Previous Frame
PImage prevFrame;
//mirror Camera
PImage videoMirror;
// How different must a pixel be to be a "motion" pixel02workshop02workshop02workshop
float threshold = 50;

float u;
float v;// for the x&y value of the circle
float speedV = 5;

void setup() {
  size(640, 480);
  video = new Capture(this, cameras[0]);
  video.start();
  // Create an empty image the same size as the video
  prevFrame = createImage(video.width, video.height, RGB);
  // Create an empty image the same size as the video
  videoMirror = createImage(video.width, video.height, RGB);
  //initial ball posititon in the x axix
  u = random(width);
  v = 50;
}


void draw() {
  if (video.available() == true) {
    // Save previous frame for motion detection!!
    prevFrame.copy(videoMirror, 0, 0, videoMirror.width, videoMirror.height, 0, 0, videoMirror.width, videoMirror.height); // Before we read the new frame, we always save the previous frame for comparison!
    prevFrame.updatePixels();  // Read image from the camera
    video.read();
  }
  loadPixels();
  video.loadPixels();

  //Mirroring the image
  for (int x = 0; x < video.width; x++) {
    for (int y = 0; y < video.height; y++) {
      videoMirror.pixels[x+y*video.width] = video.pixels[(video.width-(x+1))+y*video.width];
    }
  }
  videoMirror.updatePixels();
  prevFrame.loadPixels();

  // Begin loop to walk through every pixel
  for (int x = 0; x < videoMirror.width; x ++ ) {
    for (int y = 0; y < videoMirror.height; y ++ ) {
      int loc = x + y*videoMirror.width;            // Step 1, what is the 1D pixel location
      color current = videoMirror.pixels[loc];      // Step 2, what is the current color
      color previous = prevFrame.pixels[loc]; // Step 3, what is the previous color

      // Step 4, compare colors (previous vs. current)
      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);
      //dist between the ball and the pixels moving
      float dist = dist(x, y, u, v);

      // Step 5, How different are the colors?
      // If the color at that pixel has changed, then there is motion at that pixel.
      if (diff > threshold) {
        // If motion, display black
        pixels[loc] = color(0);
        if (dist < 50) {
          bounce();
        }
      } else {
        // If not, display white
        pixels[loc] = color(255);
      }
    }
  }
  // Notify that the pixels[] array has changed
  updatePixels();
  //comment this to see the silhouette
  image(videoMirror, 0, 0);
  thing();
  move();
  if (v < 0+50|| v > height-50) {
    bounce();
  }
}

void thing() {
  fill(0);
  circle(u, v, 100);
}
void move() {
  v=v+speedV;
}
void bounce() {
  speedV = -speedV;
}

Leave a Reply

Your email address will not be published. Required fields are marked *