We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
getting correct frame from buffer (Read 1049 times)
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
Re: getting correct frame from buffer
Reply #1 - Jan 8th, 2009, 3:10pm
 
i played a little bit around and now i can see, that somehow i can't store the correct frame in a new PImage or Capture object.

when i'm using video.save()at the marked position it's doing correct but when trying to store it in a new object it's taking the initial video, which got captured - not the changed one. (also when using things like video.get()...).

does anybody now where the changed image is stored and how i can get to it? is this a bug?
Re: getting correct frame from buffer
Reply #2 - Jan 8th, 2009, 7:20pm
 
I think I don't get this videothing right - whenever i'm picking up this "Capture video" storage, it will hand me back the initital values but not the changed ones... if i do e.g.:

void draw() {
 
 if (video.available()) {
   
   video.read();

   loadPixels();
   Backgroundsubstraction();//doing background substraction
   updatePixels();
   
   loadPixels();
   Pixelate();//trying to work with this BS
   updatePixels();
   
   }
}


the Pixelate() funktion would take the initial picture instead of the changed one. even so that both are getting to the same "Capture video" storage. strange no?


_______________________________________________________
//Here is the code for Background Substraction

void Backgroundsubstraction(){
   
 //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, 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 > thresholdR || diffG > thresholdG || diffB > thresholdB) { // 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
}

________________________________________________________


//Here is the Code for Pixelate

void Pixelate(){
 
 num_NewPixels = width / blockSize;
 myMovieColors = new color[num_NewPixels * num_NewPixels];
 
 int num_NewPixels = width / blockSize;;
 
 
 video.read();
 //video.loadPixels();
 
 for (int j = 0; j < num_NewPixels; j++) {      //Reihen
   for (int i = 0; i < num_NewPixels; i++) {    //Spalten
     myMovieColors[j*num_NewPixels + i] = video.get(((i*blockSize)), (j*blockSize));
   }
 }
 //video.updatePixels();
 
 for (int j = 0; j < num_NewPixels; j++) {
   for (int i = 0; i < num_NewPixels; i++) {
     fill(myMovieColors[j*num_NewPixels + i]);
     rect(i*blockSize, j*blockSize, blockSize-1, blockSize-1);
   }
 }
}
Re: getting correct frame from buffer
Reply #3 - Jan 8th, 2009, 7:22pm
 
somebody got an idea?
Re: getting correct frame from buffer
Reply #4 - Mar 24th, 2009, 4:59pm
 
Hi,
I'm new to processing as well and I have a possible idea although I don't know the proper way to write the code yet. If the video is short enough, say 10 seconds, one can grab each frame while playing it with available(), and save all frames as images in an image array and later refers to the image array for frames. Will this work?
Page Index Toggle Pages: 1