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
Problem with Capture and PImage copy (Read 993 times)
Problem with Capture and PImage copy
Nov 4th, 2009, 2:54pm
 
I am suspecting that my webcam is outputting dublicate frames. So I wanted to compare the incoming frame with a stored copy of the previous frame.

However I came across a curious problem when I tried to copy the camera frame into a PImage. When I compare the brightness after having copied the new frame from the camera into my PImage. It is always off by one...

Here is my code

import processing.video.*;

Capture cam;
boolean newFrame = false;
PImage oldCam;

void setup(){
 size(320, 240);  
 cam = new Capture(this, width, height);
 oldCam = new PImage(width, height);
}

void draw(){
 if(newFrame){
   //compare brightness before copy
   println("before copy    new: " + brightness(cam.pixels[0]) + "   old: " + brightness(oldCam.pixels[0]));
   oldCam.copy(cam,0,0,width,height,0,0,width,height);
   //oldCam.loadPixels();
   //oldCam.updatePixels();
   //compare brightness after copy - it should now be the same however it is always off by one
   println("after copy    new: " + brightness(cam.pixels[0]) + "   old: " + brightness(oldCam.pixels[0]));
   newFrame = false;
 }
 
 image(cam, 0, 0);
}

void captureEvent(Capture c){
 c.read();
 newFrame = true;
}


And here is an example of my console output:
before copy    new: 63.0   old: 68.0
after copy    new: 63.0   old: 62.0
before copy    new: 63.0   old: 62.0
after copy    new: 63.0   old: 62.0
before copy    new: 58.0   old: 62.0
after copy    new: 58.0   old: 57.0

As you can see after having copied the image the brightness is always off by one - even though they should be identical. I have tried the old load and update pixels trick but no luck.

Any ideas?

/lars
Re: Problem with Capture and PImage copy
Reply #1 - Nov 11th, 2009, 8:47am
 
hey lars,

Out of curiosity, do you get the same result if you say:
Code:
oldCam = cam;
//or if you can't do that
oldCam = cam.get();


instead of using copy to copy the pixel array?
Re: Problem with Capture and PImage copy
Reply #2 - Nov 12th, 2009, 10:50am
 
oldCam = cam.get();

did the trick Smiley Thanks

Quite strange - it seems that I get quite a few dublicate frames from my webcam. Well - at least now I can detect it properly.
Re: Problem with Capture and PImage copy
Reply #3 - Nov 12th, 2009, 12:12pm
 
yeah the copy method was using another method called blend()? i think, it was doing some stuff to the image I think under the assumption that you were resizing it possibly. glad that did the trick Smiley
Page Index Toggle Pages: 1