/** * Copyright (c) 2008, Benjamin Eckel * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * http://creativecommons.org/licenses/LGPL/2.1/ * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * Or see http://processing.datasingularity.com/gpl.txt */ import processing.video.*; int numPixels; Capture video; int[] previousFrame; int TOLERANCE = 100; boolean go = false; int fake_frame_rate = 0; void setup() { size(720, 480); video = new Capture(this, width, height, 30); // this line may need a little adjustment numPixels = width*height; // for those of you not using an internal camera previousFrame = new int[numPixels]; } void draw() { loadPixels(); video.read(); video.loadPixels(); if (!go) { for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame... pixels[i] = video.pixels[i]; } } else { int movementSum = 0; // Amount of movement in the frame for (int i = 0; i < numPixels; i++) { color currColor = video.pixels[i]; color prevColor = previousFrame[i]; // Extract the red, green, and blue components from current pixel int currR = (currColor >> 16) & 0xFF; int currG = (currColor >> 8) & 0xFF; int currB = currColor & 0xFF; // Extract red, green, and blue components from previous pixel int prevR = (prevColor >> 16) & 0xFF; int prevG = (prevColor >> 8) & 0xFF; int prevB = prevColor & 0xFF; // Compute the difference of the red, green, and blue values int diffR = abs(currR - prevR); int diffG = abs(currG - prevG); int diffB = abs(currB - prevB); // Add these differences to the running tally movementSum = diffR + diffG + diffB; if (movementSum > TOLERANCE) { // Render the difference image to the screen pixels[i] = 0xff000000 | (prevR << 16) | (prevG << 8) | prevB; } // Save the current color into the 'previous' buffer previousFrame[i] = currColor; } } updatePixels(); delay(fake_frame_rate); } void keyPressed() { if (key == ' ') go = !go; }