I'm working on this and can't figure it out. I'm assuming it has something to do with the loadPixels(), get(), or pixels[] functions. Basically it's a background subtraction program that thresholds. I want to figure out the percentage of white pixels in the whole frame and print it in the console.
I'm assuming it's something along the lines of:
----------------------------------
numPixels = video.width * video.height;
backgroundPixels = new int[numPixels];
for (int i = 0; i < numPixels; i++) {
get(i);
}
----------------------------------
I figured I'd just post to look for some help while I'm working on it.
Code:
import processing.video.*;
int numPixels;
int[] backgroundPixels;
Capture video;
void setup() {
// Change size to 320 x 240 if too slow at 640 x 480
size(640, 480);
String[] devices = Capture.list();
video = new Capture(this, width, height, devices[1]);
numPixels = video.width * video.height;
// Create array to store the background image
backgroundPixels = new int[numPixels];
// Make the pixels[] array available for direct manipulation
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
// This is the change I've made:
int threshold = 65;
pixels[i] = (diffR + diffG + diffB) > threshold ? color(255) : color(0);
// the ? : stuff if you haven't seen it before means -> (is it like this)? yes : no;
// Because a web cam image tends to be grainy, it means that what we get isn't so clear
// cut as we would expect - so you use a threshold value to tweak for the output you want
}
updatePixels(); // Notify that the pixels[] array has changed
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);
}
Note: I'm using an external isight. If you have a built in isight, change:
video = new Capture(this, width, height, devices[1]);
to
video = new Capture(this, width, height, 24);