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.
IndexProgramming Questions & HelpVideo Capture,  Movie Playback,  Vision Libraries › Storing Camera input for delayed playback
Page Index Toggle Pages: 1
Storing Camera input for delayed playback (Read 491 times)
Storing Camera input for delayed playback
Jun 3rd, 2008, 11:57am
 
Hey,
I'm having a problem with storing the current frame of the camera input and having it played back with a certain delay.
As for now I have three "Images" (the current, one 1 frame delayed and one with a larger delay) which are combined again into one image using each image as a color channel.
The problem is that the images I write into the buffer/array all contain the current frame and not different frames and just the last element (delayPast-1) contains the current frame. I just can't figure out what's wrong, as the delay with just the last frame ist working flawlessly.
I also found this thread http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Video;action=display;num=1159143757
but the code mentioned there doesn't work for me either at least with a capture event, with a movie it does...


Code:


import processing.video.*;

int numPixels;
PImage prevFrame;
int delayPast = 24; //amount of frames to delay
PImage[] pastFrames = new PImage[delayPast];

Capture video;

void setup() {
 size(640, 480);
 video = new Capture(this, width, height, 24);
 numPixels = video.width * video.height;
 prevFrame = createImage(video.width, video.height, RGB);
 
 for(int i = 0; i < delayPast; i++){
   pastFrames[i] = createImage(video.width, video.height, RGB);
 }
 
}

void draw() {
 loadPixels();
 if (video.available()) {
   video.read();
   
//shifting the elements of the array to the left
   for(int i = 1; i < delayPast; i++) {
     pastFrames[i-1] = pastFrames[i];
   }
   
   video.loadPixels();
   
   for (int i = 0; i < numPixels; i++) {
     color currColor = video.pixels[i];
     color prevColor = prevFrame.pixels[i];
     color pastColor = pastFrames[0].pixels[i];
     
     //extract the green channel of the realtime video
     int currG = (currColor >> 8) & 0xFF;
   
     //extract the blue channel of the 1-frame delayed video
     int prevB = prevColor & 0xFF;

     //extract the red channel of the 24-frame delayed video
     int pastR = (pastColor >> 16) & 0xFF;
     
     //combine all channels to get a "full" RGB result
     pixels[i] = color(pastR, currG, prevB);
     //pixels[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB;
     //current Frame is now the last Frame
     prevFrame.pixels[i] = currColor;  

     //current Frame is attached at the end of the delay array
     pastFrames[delayPast-1].pixels[i] = currColor;
   }
   
   updatePixels();

 }
}

Re: Storing Camera input for delayed playback
Reply #1 - Jun 3rd, 2008, 2:44pm
 
I am not sure, the base of the code seems OK. But a possible issue might be in the way you shift the frames. As you probably know, you don't do a copy of the frames, but just move around some pointers.
The way I see it (make a drawing with the array as pointers, arrows to the created images in memory) is that in a few iterations (the number of frames) all the array slots will point to the last created image (created in the setup).
I think the solution would be to make a circular buffer instead: after the shifting loop, add:
pastFrames[delayPast-1] = pastFrames[0];
Re: Storing Camera input for delayed playback
Reply #2 - Jun 3rd, 2008, 6:02pm
 
Thanks, that did solve the problem.
So if I understood you right, it was just that the
pastFrames[delayPast-1]
always pointed to the same image and this pointer got copied to the whole array?

But yeah now it works, thanks
Page Index Toggle Pages: 1