|
Author |
Topic: videoEvent() and loop() timing relationship (Read 530 times) |
|
kensuguro
|
videoEvent() and loop() timing relationship
« on: Nov 9th, 2004, 9:35am » |
|
I'm doing a sketch that does basic white balancing. But my problem is that I get lots of flicker, as if processing is drawing an image on screen before the image is done being updated. here's the code so far: Code: int whiter; int whiterp; int whiteg; int whitegp; int whiteb; int whitebp; int fixr; int fixb; int fixg; BImage balanced = new BImage(320, 240); boolean newFrame; void setup() { whiter=1; whiteg=1; whiteb=1; whiterp=1; whitegp=1; whitebp=1; fixr=100; fixg=100; fixb=100; newFrame=false; size(320, 240); beginVideo(320, 240,30); framerate(30); } void mousePressed() { colorMode(RGB, 255); for(int i=0; i<20; i++){ for(int j=0; j<20; j++){ whiter = int( red(video.pixels[i*640+j]) / 3.5 ); whiteg = int( green(video.pixels[i*640+j]) / 3.2 ); whiteb = int( blue(video.pixels[i*640+j]) / 2.8 ); fixr = int( (whiter+whiterp) / 2 ); fixg = int( (whiteg+whitegp) / 2 ); fixb = int( (whiteb+whitebp) / 2 ); whiterp = whiter; whitegp = whiteg; whitebp = whiteb; } } } void videoEvent() { newFrame = true; } void loop() { if(newFrame == true){ image(balanced, 0, 0); for(int i=0; i<320*240; i++){ balanced.pixels[i]=color( int(red(video.pixels[i])*100/fixr), int(green(video.pixels[i])*100/fixg), int(blue(video.pixels[i])*100/fixb)); } newFrame=false; } } |
| It looks to me, that the code in loop() got too heavy, and the frames are bing pushed out(displayed) before "balanced" gets updated with the rgb scaling loop. Is this how it is? I'm wondering because I've done some other more FX rich experiments without the flickering problem, although I got lower framerates.
|
|
|
|
fjen
|
Re: videoEvent() and loop() timing relationship
« Reply #1 on: Nov 9th, 2004, 2:50pm » |
|
have you tried not to set the framerate in setup? or try to speed the code in loop by accessing pixels the way i explained yesterday. bit-operations are faster then red, green, blue and color() ... /F
|
|
|
|
fjen
|
Re: videoEvent() and loop() timing relationship
« Reply #2 on: Nov 9th, 2004, 2:56pm » |
|
i was going thru your code again and there are some more things you could do to speed things: in: (int i=0; i<320*240; i++) precalc the value ... best would be outside loop(), since it's calculated every (320*240)-times at the moment in: int(blue(video.pixels[i])*100/fixb) - no need casting to int() since it's allready. - you could precalculate 100/fixb (and for fixr, fixg) in mouseReleased /F
|
|
|
|
fjen
|
Re: videoEvent() and loop() timing relationship
« Reply #3 on: Nov 9th, 2004, 3:04pm » |
|
another thought, allthough i think it should not make a difference, try putting the image() call after the balanced.pixels-for-loop .. /F
|
|
|
|
kensuguro
|
Re: videoEvent() and loop() timing relationship
« Reply #4 on: Nov 9th, 2004, 3:23pm » |
|
thanx for your help, again. Problem solved. I was talking to setpixel today, an we got the flickering problem figured out. The problem was that video was being refreshed before the code finished reading from it. So, all I had to do was to make a copy of video, and then the rest didn't matter much. (in terms of speed) I will definitely implement the optimizations you speak of. It's blazing fast once I got the flicker problem solved, but it's always good to be able to tell efficient coding from what's not.
|
|
|
|
fjen
|
Re: videoEvent() and loop() timing relationship
« Reply #5 on: Nov 9th, 2004, 4:46pm » |
|
jikes! sure ... the obvious. blind me.
|
|
|
|
fjen
|
Re: videoEvent() and loop() timing relationship
« Reply #6 on: Nov 9th, 2004, 6:42pm » |
|
oh, btw if you are interessted in optimization: http://www.microjava.com/articles/techtalk/optimization it's focused on j2me development but the techniques are the same for any java development ... /F
|
|
|
|
|