Compcam: Take a picture in an unexpected place or time

I spent a lot of time trying to get the Processing with Android and IP Camera code to work and wasn’t particularly successful.  I did have one funny success, which was capturing the IP Camera in the classroom at about three in the morning when I finally got it working, and this is what I saw:

It took me a moment to realize it was a MacBook.  It was kind of creepy.  It might have worked well with the idea I wanted to try, which actually is a more sort of augmented reality idea, but I wanted to take on the of the IP cameras and mix it with another video and use the movement of people in the camera (though the hallway at ITP for example) to create the image of ghosts coming through the the video.  Here’s a sample of the code I worked out in processing, which tracks the changes in pixels in my webcam and adds the colors to the video that is playing.  Right now it kind of looks like it is just using alpha to overlay, but I want to make it look more like crunched pixels or something.  I tried to work this into my IP Camera code, but haven’t gotten very far.

The code for my IP Camera is taken from the link sent out by Luca and Elena, here.

My processing code is an adjustment to the background cancellation example from the video library:

 * 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.


Movie dance;
PImage img;

int numPixels;
int[] backgroundPixels;
int[] moviePixels;
Capture video;

void setup() {
  // Change size to 320 x 240 if too slow at 640 x 480
  size(640, 480); 

  video = new Capture(this, width, height);

  numPixels = video.width * video.height;
  // Create array to store the background image
  backgroundPixels = new int[numPixels];
  moviePixels = new int[numPixels];
  // Make the pixels[] array available for direct manipulation
  dance = new Movie(this, "");

  img = createImage(width, height, RGB);

void draw() {
  image(img, 0, 0);

  if (video.available()) {; // 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 > 16) & 0xFF;
      int currG = (currColor >> 8) & 0xFF;
      int currB = currColor & 0xFF;
      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
      pixels[i] = color(diffR, diffG, diffB, 100);
      img.pixels[i] = pixels[i] + dance.pixels[i];

      // The following line does the same thing much faster, but is more technical
      //pixels[i] = 0xFF000000 | (diffR << 16) | (diffG << 8) | diffB;
    updatePixels(); // Notify that the pixels[] array has changed
    println(presenceSum); // Print out the total amount of movement
  arraycopy(video.pixels, backgroundPixels);

// When a key is pressed, capture the background image into the backgroundPixels
void keyPressed() {
  arraycopy(video.pixels, backgroundPixels);

void movieEvent(Movie m) {;

  for (int j = 0; j < m.width; j++) {
    for (int i = 0; i < m.height; i++) {
      moviePixels[j*i] = m.get(i, j);

Author: owen ribbit


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s