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 › Making a convert image to black and white(grey)
Page Index Toggle Pages: 1
Making a convert image to black and white(grey) (Read 1684 times)
Making a convert image to black and white(grey)
Apr 24th, 2010, 3:13pm
 
I load an image and go through each pixel one by one, calculate its value and find the average of its RGB channels, and that average I take as the Grey value for the pixel.

However my program is crashing because there are to many operations, going through each pixel one by one must take a lot of the CPU usage...

here is my code:

PImage img = loadImage("example.jpg");

size(512,512);
image(img,0,0,width,height); // fills the screen with the picture

for(int x=0; x<width; x++)
{
  for(int y=0; y<height; y++)
  {
      color c = get(x,y);
      float red = red(c);
      float green = green(c);
      float blue = blue(c);
      int grey = (int)(red+green+blue)/3;
      set(x,y,grey);
  }
}

Why doesn't it work!!!!
Re: Making a convert image to black and white(grey)
Reply #1 - Apr 25th, 2010, 1:12am
 
try this..... the only difference is you are constraining your y value so that you are not performing so many calculations.

I have to tell you that I can see what you are going for but this isn't going to give you the results you expect. At least now though you can eliminate memory as being the problem. Also read http://processing.org/reference/set_.html  Consider using load pixels for speed. (Also try using P3D or OPENGL this made it work for me)

Keep at it it's a cool idea you can make all sorts of crazy custom black/white filters like this.

PImage img = loadImage("example.jpg");

size(512,512);
image(img,0,0,width,height); // fills the screen with the picture

for(int x=0; x<width; x++)
{

     
 for(int y=50; y<70; y++)   #<------------------- change
 {
     color c = get(x,y);
     float red = red(c);
     float green = green(c);
     float blue = blue(c);
     int grey = (int)(red+green+blue)/3;

     set(x,y,grey);
 }
}
println("done");   #<--- another tip i love statements like these you won't have to sit there going is this thing still going
Re: Making a convert image to black and white(grey)
Reply #2 - Apr 25th, 2010, 1:25am
 
and if you want to cheat heres the rest of the answer


PImage img = loadImage("example.jpg");

size(512,512,P3D);
image(img,0,0,width,height); // fills the screen with the picture
println("start");
for(int x=0; x<width; x++)
{
 for(int y=0; y<height; y++)
 {
     color c = get(x,y);
     float red = red(c);
     float green = green(c);
     float blue = blue(c);
     int grey = (int)(red+green+blue)/3;
     color Color =color(grey,grey,grey);
     set(x,y,Color);
 }
}
println("finsih");
Re: Making a convert image to black and white(grey)
Reply #3 - Apr 25th, 2010, 2:18am
 
PImage b = loadImage("example.jpg");
image(b, 0, 0);
filter(GRAY);
Page Index Toggle Pages: 1