Hey,
i changed the example to do color-keying instead of background-subtraction.
Just click somewhere inside of the image to pick a color.
If you want an image instead of that solid red-background just replace the background() with a call of image() or something. And adjust the value of 'thresh' to fit your needs.
- import processing.video.*;
int numPixels;
Capture video;
int keyColor = 0xff000000;
int keyR = (keyColor >> 16) & 0xFF;
int keyG = (keyColor >> 8) & 0xFF;
int keyB = keyColor & 0xFF;
int thresh = 20; // tolerance of
void setup() {
size(640, 480, P2D);
video = new Capture(this, width, height, 24);
numPixels = video.width * video.height;
}
void draw() {
if (video.available()) {
background(0xffff0000);
loadPixels();
video.read(); // Read a new video frame
video.loadPixels(); // Make the pixels of video available
for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
// Fetch the current color in that location
color currColor = video.pixels[i];
int currR = (currColor >> 16) & 0xFF;
int currG = (currColor >> 8) & 0xFF;
int currB = currColor & 0xFF;
// Compute the difference of the red, green, and blue values
int diffR = abs(currR - keyR);
int diffG = abs(currG - keyG);
int diffB = abs(currB - keyB);
// Render the pixels wich are not the close to the keyColor to the screen
if((diffR + diffG + diffB)> thresh){
pixels[i] = video.pixels[i];
}
}
updatePixels(); // Notify that the pixels[] array has changed
}
}
void mousePressed() {
keyColor = get(mouseX, mouseY);
keyR = (keyColor >> 16) & 0xFF;
keyG = (keyColor >> 8) & 0xFF;
keyB = keyColor & 0xFF;
}
Hop this helps understanding the principle.