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 › Change filter on image
Page Index Toggle Pages: 1
Change filter on image (Read 743 times)
Change filter on image
Oct 15th, 2009, 11:54am
 
Hey

Iam new to processing and trying to learn the basics.

What I'am trying to do is to change the filter on an image.

Ive tried all sorts of ways but it just wont work. Anyone have any suggestion or hints so I can get this right?

I would like to change the Filter by for instance pressing the UP key, for example
GRAY, INVERT, THRESHOLD and back to no filter.

Any help would be appreciated.

Thanks
Re: Change filter on image
Reply #1 - Oct 15th, 2009, 1:12pm
 
newb wrote on Oct 15th, 2009, 11:54am:
Ive tried all sorts of ways but it just wont work.

Show us your best attempt, we might show what went wrong.
Re: Change filter on image
Reply #2 - Oct 15th, 2009, 2:05pm
 
if you didnt write any good code yet, take a look at http://processing.org/reference/switch_.html and you probably figure it out.
Re: Change filter on image
Reply #3 - Oct 15th, 2009, 2:39pm
 
I was trying out this, and I get a message that it can't fint filter class or type, and I think I got why.

But how do I give the filter an value from the array "filt" ?
I was thinking I maby needed something like this in draw(),
filter(filt[intvalue?]);

If I could get that working then I hopefully can figure out how to change value.

I might be way off but this is what I could come up with, with my current knowledge of processing. :)

I will look into swith tomorrow, thanks for the tip.


int count = 0;
PImage[] pix = new PImage [3];


filter[] filt = new filter[3];

int x = 0;
int y = 0;

void setup(){

 filt[0] = filter(GRAY);
 filt[1] = filter(INVERT);
 filt[2] = filter(THRESHOLD);

 pix[0] = loadImage("filename.jpg");
 pix[1] = loadImage("filename2.jpg");
 pix[2] = loadImage("filename3.jpg");
 size(550,550);
 frameRate(60);
}

void draw(){  
 background(255);
 image(pix[count],x,y,550,550);
}

void keyPressed(){
 if(key == 32){
   count++;
 }
 if (count==3){
   count=0;
 }
 else if(key == 65 || key == 97){
   x+=3;
 }
}
Re: Change filter on image
Reply #4 - Oct 15th, 2009, 2:52pm
 
You are making a filter[] array but there is no such thing. You can only declar know datatypes like int,float,long,color, Pimage etc. or user defined classes.

so you have to think of another way to do it. i would say the switch seems to be an easy way to do it.
Re: Change filter on image
Reply #5 - Oct 16th, 2009, 2:22am
 
Cedric is right.
filter(XXX) is a function call, not an object. It acts immediately on the sketch's display, you cannot store it. So you have to apply it in draw(), depending on current choice.
Otherwise, the overall logic is correct, with some little logic fixes:
Code:
void keyPressed(){
if(key == 32){
  count++;
  [color=#ff0000]// Moved check inside test[/color]
  if (count==3){
    count=0;
  }
}
else if(key == 65 || key == 97){
  x+=3;
}
}
Note that you can write simpler (but slightly less readable, at least when you are not used to the idiom): count = ++count % 3;
So you get rid of the filter array, and in draw you do:
Code:
void draw(){  
background(255);
image(pix[count],x,y,550,550);
switch (count) {
// No case 0, I think it can be nice to see unfiltered image in this case
case 1: filter(GRAY); break;
case 2: filter(INVERT); break;
case 3: filter(THRESHOLD); break;
// and so on
default:
}
}

Note the usage of break, to avoid applying the filters in cascade...
Page Index Toggle Pages: 1