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 › returning gray values using pixels[]
Page Index Toggle Pages: 1
returning gray values using pixels[]? (Read 903 times)
returning gray values using pixels[]?
Mar 27th, 2006, 5:00am
 
I'm trying to obtain a pixel's grayscale value (between 0 and 255) using pixels[].  So far I am getting large negative numbers when I use the code below.  

Is there another way to do this or can I just convert the value returned from pixels[]?

size(500, 500);
background(255);

// Load the font.
PFont fontA = loadFont("HelveticaNeue-Light-48.vlw");
textFont(fontA, 10);
textAlign(CENTER);

//Load the image
PImage a;  // Declare variable "a" of type PImage
a = loadImage("bwimage.jpg"); // Load the images into the program
image(a, 0, 0);
loadPixels();

//text position and spacing
int xstart = 11;
int ystart = 12;
int xspace = 20;
int yspace = 11;

// Create a matrix to display pixel values
background(255);
for(int i=xstart; i<width; i=i+xspace) {
 for(int j=ystart; j<height; j=j+yspace) {
     fill(pixels[j*width+i]);
     text(pixels[j*width+i], i, j);  // I need to convert the value returned from pixels[] or use a different command
 }
}
Re: returning gray values using pixels[]?
Reply #1 - Mar 27th, 2006, 11:21am
 
The value in pixels[x] is a 32-bit integer of the format Alpha Red Green Blue, with each part being an 8 bit value.

To convert this directly into a greyscale value:

Code:

int c=pixels[i+j*width]; // so we don't access the array too much
int r=(c&0x00FF0000)>>16; // red part
int g=(c&0x0000FF00)>>8; // green part
int b=(c&0x000000FF); // blue part
int grey=(r+b+g)/3;


Now this greyscale value is just an average of the red/blue/green values, which is fine for most cases, but I think if you were converting colour images to greyscale, there's a slight weighting to the r/g/b values to amke some have slightly more effect, but I don't know what the values are.
Re: returning gray values using pixels[]?
Reply #2 - Mar 27th, 2006, 3:50pm
 
you can also do

int brightness = max(r, g, b);

or the "luminance" calculations (to which john is referring) to do a proper weighting can be seen in the source code for PImage, in a function contributed by toxi.
Re: returning gray values using pixels[]?
Reply #3 - Mar 27th, 2006, 5:28pm
 
I remember asking about this long ago.

By the max (r, g, b) I'm assuming you mean:
Code:

static final int gray(color p) {
return max((value >> 16) & 0xff, (value >> 8 ) & 0xff, value & 0xff);
}

I ask because I'm using this function in a project (codename: webcam tracking Tetris) and I'm worried about John's mention of 0x000000FF and such whereas I've only the 0xFF in the function I'm using. They're both the same right?
Re: returning gray values using pixels[]?
Reply #4 - Mar 27th, 2006, 7:02pm
 
yep, the gray function you show there is correct. i only meant to replace the last line of the code in the previous post.

you can also use filter(GRAY) on the image, which does this per pixel:
Quote:


int col = pixels[i];
// luminance = 0.3*red + 0.59*green + 0.11*blue
// 0.30 * 256 =  77
// 0.59 * 256 = 151
// 0.11 * 256 =  28
int lum = (77*(col>>16&0xff) + 151*(col>>8&0xff) + 28*(col&0xff))>>8;
pixels[i] = (col & ALPHA_MASK) | lum<<16 | lum<<8 | lum;

Re: returning gray values using pixels[]?
Reply #5 - Mar 28th, 2006, 12:42am
 
Thank you all for the quick response!  It's working now.  
Greg
Page Index Toggle Pages: 1