timm
Junior Member
Offline
Posts: 81
getting correct frame from buffer
Jan 8th , 2009, 1:09pm
Hello everybody, i got a small problem, which i don't really understand. i'm trying to store a frame of a background substraction in an image in order to call it in an second window. now i tried it like this: import processing.video.*; PFrame f; PImage f_image; secondApplet s; color black = color(0); color white = color(255); int threshold = 50; int numPixels; int[] backgroundPixels; Capture video; void setup() { size(320, 240, P2D); PFrame f = new PFrame(); video = new Capture(this, width, height, 24); numPixels = video.width * video.height; backgroundPixels = new int[numPixels]; loadPixels(); } void draw() { boolean loadimage = true; 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); if (diffR > threshold || diffG > threshold || diffB > threshold) { // If the pixel is brighter than the pixels[i] = white; // threshold value, make it white } else { // Otherwise, pixels[i] = black; // make it black } } video.updatePixels(); // Notify that the pixels[] array has changed f_image = video.getFrame(); <-- HERE IS THE PROBLEM! s.image(f_image, 0, 0); s.redraw(); } } void keyPressed() { video.loadPixels(); arraycopy(video.pixels, backgroundPixels); } _________________________________________ Second window: public class PFrame extends Frame { public PFrame() { setBounds(100,100,400,300); s = new secondApplet(); add(s); s.init(); show(); } } public class secondApplet extends PApplet { public void setup() { size(400, 300); noLoop(); } public void draw() { } } now, when saying "f_image = video;" or anything similar i'm allways getting the initial capture picture, but not the when which is changed, when messing around with pixels. what is wrong? cheers