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 & HelpPrograms › capture images and then sequence them
Page Index Toggle Pages: 1
capture images and then sequence them? (Read 1677 times)
capture images and then sequence them?
Jul 22nd, 2009, 8:51pm
 
I want to use a camera (for now it can just be my webcam) to capture a fixed amount (e.g 16) still images at triggered events (e.g. mouse clicks).

Then I want to display those images in the sequence they were captured at a set interval.

Any tips people would have for implementing this?

My first instinct  is to use the Video library to capture the video stream and then use save(filename-####.jpg) to save specific frames.

What would be the best way to stop the video capture and then load and display the images?
Re: capture images and then sequence them?
Reply #1 - Jul 23rd, 2009, 3:34am
 
i'll make an example using opencv; you can do the same with any video lirary simply changing the opencv calls with you chosen library calls.
to capture at mouse click use something like:

Code:

PImage[] captured_frames;
...
int cnt = 0;
...
void mousePressed(){
captured_frames[cnt] = opencv.image();
cnt++;
}


to playback, use something like:
Code:

...
int cnt2 = 0;
...
void draw(){
...
my_playback();
...
}
...
void my_playback(){
image(captured_frames[cnt2],0,0);
cnt2++;
}
Re: capture images and then sequence them?
Reply #2 - Jul 23rd, 2009, 8:19am
 
thanks for the info!

I tried the code but I have an error "exception in animation thread" whenever I try to save the frame:

Code:
import hypermedia.video.*;

PImage[] captured_frames;
OpenCV opencv;

int cnt = 0;
boolean playmode = false;


void setup() {
  size( 320, 240 );
   opencv = new OpenCV( this );
   opencv.capture( width, height );  // open video stream


}

void draw(){
if (playmode) {
  my_playback();
} else {
opencv.read();
image( opencv.image(), 0, 0 );
 }
}


void my_playback(){
  for (int i = 0; i<cnt; i++) {
  image(captured_frames[i],0,0);
   delay(200);
  }
 
}

void keyPressed() {
 if (key == 'z') {
    captured_frames[cnt] = opencv.image();
    cnt++;
    println("Z key pressed");
 } else if (key == 'x'){
   playmode = true;
 }
}


any idea what's wrong?
Re: capture images and then sequence them?
Reply #3 - Jul 23rd, 2009, 9:56am
 
intellijel wrote on Jul 23rd, 2009, 8:19am:
I have an error "exception in animation thread"
[...]
any idea what's wrong

Reporting at least the first few lines of an exception always helps in pinning down "what's wrong"...

Now, looking harder at your code, I can guess the exception is on captured_frames array which seems never initialized.
Re: capture images and then sequence them?
Reply #4 - Jul 23rd, 2009, 10:05am
 
PhiLho  wrote on Jul 23rd, 2009, 9:56am:
intellijel wrote on Jul 23rd, 2009, 8:19am:
I have an error "exception in animation thread"
[...]
any idea what's wrong

Reporting at least the first few lines of an exception always helps in pinning down "what's wrong"...

Now, looking harder at your code, I can guess the exception is on captured_frames array which seems never initialized.


here's the full error:

Code:
Exception in thread "Animation Thread" java.lang.NullPointerException
at sketch_jul23a.mousePressed(sketch_jul23a.java:65)
at processing.core.PApplet.handleMouseEvent(PApplet.java:1608)
at processing.core.PApplet.dequeueMouseEvents(PApplet.java:1545)
at processing.core.PApplet.handleDraw(PApplet.java:1437)
at processing.core.PApplet.run(PApplet.java:1328)
at java.lang.Thread.run(Thread.java:613)

Re: capture images and then sequence them?
Reply #5 - Jul 23rd, 2009, 10:13am
 
ok that was a dumb mistake (not initializing the array)

but I now have a different problem where it will only playback a single image and stop.

When I click 'x' it just shows me the last image saved, not a sequence of the last five.


here is the corrected code:

Code:
import hypermedia.video.*;

int maxFrames = 255;

PImage[] captured_frames= new PImage[maxFrames] ;
OpenCV opencv;

int cnt = 0;
boolean playmode = false;


void setup() {
size( 320, 240 );
opencv = new OpenCV( this );
opencv.capture( width, height ); // open video stream


}

void draw(){
if (playmode) {
my_playback();
} else {
opencv.read();
image( opencv.image(), 0, 0 );
}
}


void my_playback(){
for (int i = 0; i<cnt; i++) {
image(captured_frames[i],0,0);
delay(200);
}

}

void keyPressed() {
if (key == 'z') {
captured_frames[cnt] = opencv.image();
cnt = (cnt+1)%maxFrames;
println("Z key pressed");
} else if (key == 'x'){
playmode = true;
}
}

Re: capture images and then sequence them?
Reply #6 - Jul 23rd, 2009, 10:26am
 
That's the classical problem of animations in Processing: we all try (in our early days of Processing) to draw several things in draw(), adding delay() to ensure proper rate. But it just doesn't work: screen is updated only at the end of draw().
You have to add a logic to draw an image, count a number of frames, draw next image, etc.
Re: capture images and then sequence them?
Reply #7 - Jul 23rd, 2009, 4:32pm
 
PhiLho  wrote on Jul 23rd, 2009, 10:26am:
That's the classical problem of animations in Processing: we all try (in our early days of Processing) to draw several things in draw(), adding delay() to ensure proper rate. But it just doesn't work: screen is updated only at the end of draw().
You have to add a logic to draw an image, count a number of frames, draw next image, etc.


awesome, this was the approach I needed to know about!
Page Index Toggle Pages: 1