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 & HelpSyntax Questions › my first sketch...
Page Index Toggle Pages: 1
my first sketch... (Read 689 times)
my first sketch...
Nov 2nd, 2009, 2:00pm
 
hello.
very new to the forum, first post, and i only downloaded processing over the weekend...the idea over the next year is to 'learn as i go' with processing with the end goal being a program that recognises different colours as inputs and outputs control data for a synth i've written in PureData. after the whole weekend i have a working program,below, but was wondering what i could've done better/more efficiently. as the end goal is real-time image processing, im trying to think about efficiency right from the very start.....and this is the very very start  :o

here is my first attempt:
-------------------------------------------------

void setup(){
 size(500,500);
 fill(12,324,54);
 //noLoop();
 PImage org = loadImage("squiggle500.png");
 image(org,0,0);
 PImage org2 = createImage(height,width,RGB);
 color clr2=color(255,0,0);
 color clr=color(0,0,0);

 loadPixels();
 for (int i = 0; i < height*width/2; i++)
 {

   if(red(pixels[i])>30)
   {
     pixels[i]=clr2;
   }
   else
   {
     pixels[i] = clr;
   }
 }
 updatePixels();
 org .get();
 save("proc.jpg");
}

void draw(){

 println(frameRate);
 PImage org2 = loadImage("proc.jpg");
 PImage org = loadImage("squiggle500.png");

 if (mousePressed && (mouseButton == LEFT)) {
   image(org2,0,0);
 }
 else{
   image(org,0,0);

 }
}
--------------------------------------------------------------------


any constructive criticism is very welcome,
thanks,
FJ
Re: my first sketch...
Reply #1 - Nov 2nd, 2009, 11:32pm
 
Performance wise, using loadImag in draw is a no-no (unless carefully done in a conditional way, once, etc.): you load an image, ie. do disk access (unless system cache is kicking in) on each frame!

Little optimization hint: red() is a bit costly (not so much, but hey!), as it does a little more than doing bit shifting. So general advice is to do bit masking & shifting to get a color component.
Re: my first sketch...
Reply #2 - Nov 3rd, 2009, 4:15am
 
great, thanks for the heads up with red(), i've just had a look at the bit shift operations and conceptually red() is obviously easier to understand...but ive noted down the bit shift for later work.

as for the loadImag - i had thought this was the case, that it loads from disk on each interation. not good. if these are initiated in setup, could i then access them in draw? probably a very newbie question - as i assume the answer already is 'yes', but as of this afternoon i haven't been able to work out how to move these around.....?

thanks again for the input, its appreciated - especially at this stage of my learning curve!
FJ
Re: my first sketch...
Reply #3 - Nov 3rd, 2009, 5:44am
 
The trick is to declare the PImage variables outside of any function: they are thus kind of global variables (actually, they are instance variables of the hidden class created by Processing) and can be accessed from any function.
Page Index Toggle Pages: 1