...
Code Block | ||||
---|---|---|---|---|
| ||||
import processing.video.*; Capture video; PImage backgroundImage; float threshold = 20; void setup() { size(640, 480); // start video captire video = new Capture(this, width, height, 30); video.start(); // prepare image to save background backgroundImage = createImage(video.width, video.height, RGB); } void draw() { // read camera image if available if (video.available()) { video.read(); } loadPixels(); video.// active pixel manipulation of canvas loadPixels(); // get pixel data from video and background image video.loadPixels(); backgroundImage.loadPixels(); // loop through video pixel by pixel for (int x=0; x < video.width; x++) { for (int y=0; y < video.height; y++) { int// loc get pixel array location int loc = x + y * video.width; color fgColor = video.pixels[loc]; // get foreground color (video) color bgColorfgColor = backgroundImagevideo.pixels[loc]; // floatget r1background =color red(fgColorimage); floatcolor g1bgColor = green(fgColor);backgroundImage.pixels[loc]; float// b1get = blue(fgColor);individual colors float r2r1 = red(bgColorfgColor); float g2g1 = green(bgColorfgColor); float b2b1 = blue(bgColorfgColor); float diffr2 = dist(r1, g1, b1, r2, g2, b2red(bgColor); float g2 = green(bgColor); float b2 = blue(bgColor); // calculate spacial distance between the two colors float dist = dist(r1, g1, b1, r2, g2, b2); // check if distance is above threshold if (diff > threshold) {dist > threshold) { // write foreground pixel pixels[loc] = fgColor; } else { // set pixel to black pixels[loc] = color(0); } } } // write pixel back to canvas updatePixels(); } void mousePressed() { // copy current video frame into background image backgroundImage.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height); backgroundImage.updatePixels(); } |
...