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 › Creating a picture with rgb values either 0 or 255
Page Index Toggle Pages: 1
Creating a picture with rgb values either 0 or 255 (Read 955 times)
Creating a picture with rgb values either 0 or 255
Jan 27th, 2007, 2:13pm
 
Hello!

I am trying to code a program which first loads a picture and then checks every pixel's rgb values.

If the red value < 128, the red value of this pixel in the new picture will be 0. Otherwise it will be 255.
Same for green and blue.

Finally the program creates a new picture with rgb values either 0 or 255.

The code I wrote:

Code:

PImage pic;

void setup(){
pic = loadImage("http://processing.org/img/processing_beta_cover.gif");
size(800,800);
}

void draw(){

background(255);

for(int row=0; row<pic.width; row=row+1) {
for(int col=0; col<pic.height; col=col+1) {

color pixelcolor=pic.get(row, col);

if ( red(pic) < 128) { red(pixelcolor) = 0; }
else { red(pic) = 255; }

if ( green(pic) < 128) { green(pixelcolor) = 0; }
else { green(pic) = 255; }

if ( blue(pic) < 128) { blue(pixelcolor) = 0; }
else { blue(pic) = 255; }

}
}

image(pic,0,0);
}



When starting the program I get this error:

Semantic Error: No applicable overload for a method with signature "red(processing.core.PImage)" was found in type "processing.core.PApplet". Perhaps you wanted the overloaded version "float red(int $1);" instead?

Can anybody give me a hint what's wrong?
Re: Creating a picture with rgb values either 0 or
Reply #1 - Jan 27th, 2007, 2:30pm
 
i think you meant to use red(pixelcolor) not red(pic), and the same for green() and blue().

red(pic) is trying to get the red value for the entire image, not just the one pixel.
Re: Creating a picture with rgb values either 0 or
Reply #2 - Jan 27th, 2007, 2:35pm
 
your testing and setting of pixel colours is wrong in 2 ways.

1: red(pic) is wrong because pic is an image, not a specific colour.

2: red(pixelcolour)=0; will have no effect, what you're effectively saying is 126 = 0; red(pixelcolour) just returns a value, it doesn't give you access to change the colour.

Here's a better method:
Code:
//inside your loop...
color pixelcolor=pic.get(row,col);
int newRed, newGreen, newBlue;
if(red(pixelcolor)<128)
newRed=0;
else
newRed=255;

if(green(pixelcolor)<128)
newGreen=0;
else
newGreen=255;

if(blue(pixelcolor)<128)
newBlue=0;
else
newBlue=255;

pic.set(row,col,color(newRed,newGreen,newBlue);


Also you probably should put pic.loadPixels(); after background(...), and pic.updatePixels(); before the image(...); to make sure that your changes are noticed.
Re: Creating a picture with rgb values either 0 or
Reply #3 - Jan 27th, 2007, 3:33pm
 
Thank you very much for your answers.

Quote:
Also you probably should put pic.loadPixels(); after background(...), and pic.updatePixels(); before the image(...); to make sure that your changes are noticed.

Seems to work properly without those changes.

Here is the working source code:

Code:

PImage pic;

void setup(){
pic = loadImage("http://www.google.com/intl/en_ALL/images/logo.gif");

size(400,400);
}

void draw(){

background(255);

for(int row=0; row<pic.width; row=row+1) {
for(int col=0; col<pic.height; col=col+1) {

color pixelcolor=pic.get(row,col);

int newRed, newGreen, newBlue;

if(red(pixelcolor)<128) newRed=0;
else newRed=255;

if(green(pixelcolor)<128) newGreen=0;
else newGreen=255;

if(blue(pixelcolor)<128) newBlue=0;
else newBlue=255;

pic.set(row,col,color(newRed,newGreen,newBlue));

}
}

image(pic,0,0);
}


I will use that code for further programing.
Re: Creating a picture with rgb values either 0 or
Reply #4 - Jan 27th, 2007, 7:30pm
 
yes, loadPixels() is a good idea, even though it's not necessary now, but will be for future releases. size() should also be first in setup()!

a geekier/speedier version:

Code:

PImage pic, thresh;

void setup() {
size(400, 400); // this should *always* come first, see the reference

pic = loadImage("http://www.google.com/intl/en_ALL/images/logo.gif");
pic.loadPixels();

thresh = createImage(pic.width, pic.height, RGB);
int count = pic.width*pic.height;
for (int i = 0; i < count; i++) {
int p = pic.pixels[i];
int t = 0xFF000000;

if ((p & 0x800000) != 0) {
t |= 0xFF0000;
}
if ((p & 0x8000) != 0) {
t |= 0xFF00;
}
if ((p & 0x80) != 0) {
t |= 0xFF;
}
thresh.pixels[i] = t;
}
thresh.updatePixels();
image(thresh, 0, 0);
}
Re: Creating a picture with rgb values either 0 or
Reply #5 - Jan 27th, 2007, 8:08pm
 
Uruk wrote on Jan 27th, 2007, 2:13pm:
If the red value < 128, the red value of this pixel in the new picture will be 0. Otherwise it will be 255.


What you're describing is a 2-level posterize, see the reference for filter() for a real easy solution.

Another option  is you could use my image adjuster lib to create a customize posterize variant.  See the threshold "recipe" here for further notes:  http://www.davebollinger.com/works/imageadjuster
Page Index Toggle Pages: 1