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 › color in an image
Page Index Toggle Pages: 1
color in an image (Read 515 times)
color in an image
Nov 24th, 2007, 10:11pm
 
hi. i am looking for some help getting color data out of an image. i have a large picture of an orchid (jpg 1280x1120), and from this picture I would like to be able to figure out a color palatte. I am mainly looking to find the three most commons colors in order of frequency. Ideally, I would like the rgb values for these colors, but other values will work too.
Re: color in an image
Reply #1 - Nov 25th, 2007, 3:28am
 
I don't wanna kill your enthusiasm but it's not really easy.
I mean, getting the colour of a pixel is piece of cake ( http://processing.org/reference/get_.html ), but you'll have to look into statistics algorithms. Quantitisation, differentiation, stuff like that.

But to get you started, do a for loop through all the pixels in your image and get their color wit get(). Compare every pixel with all the previous ones. If you have it in your list, increment its count. If not, add the colour to the list.

At the end, check the one with the highest count and you have the most frequent colour. When you get there, let us know.
Re: color in an image
Reply #2 - Nov 25th, 2007, 5:02am
 
Cosmin is right... that's the way to go.

If you use an image with 16 million possible colors in it, the application will take forever to check all the pixels... maybe even crash.

I did something similar to what you want to do but to simplify it, I decided to use a GIF because It can only have a maximum of 256 colors.

If it's only a palette you want, I suggest using a GIF, you'll get a pretty close match to the general palette of the image.

I'll upload my code tomorrow so you can use it! Smiley
Re: color in an image
Reply #3 - Nov 25th, 2007, 5:14pm
 
Here's the code

Code:
int[][] colorList = new int[0][2]; //array that will contain all the colors in the image
PImage imageAnalyzed; //image to analyze
int colorToConvert,r,g,b;

void setup() {
size(400,400);
background(0);

imageAnalyzed = loadImage("image01.gif");

if (createPalettte()) {
for (int k=0; k<colorList.length; k++ ) {
println("Color: " + colorList[k][0] + " Count: " + colorList[k][1]); //prints the colors and the number of times it is prsent in the image (you'll notice that the values are not in rgb format)
}

//to convert it to RGB format you need to do this (and here I'll just do it for the first color)
colorToConvert = colorList[0][0];

r = colorToConvert >> 16 & 0xFF; //gets the RED
g = colorToConvert >> 8 & 0xFF; //gets the GREEN
b = colorToConvert & 0xFF; //gets the BLUE

println("Color: " + colorList[0] + " => r = " + r + " g = " + g + " b = " + b);
}
}

void draw() {
}

//function which will populate the array
boolean createPalettte() {
//loop through all the pixels of the image
for (int i = 0; i < imageAnalyzed.pixels.length; i++) {

boolean colorExists = false; //bollean variable that checks if the color already exists in the array


//loop through the values in the array
for (int j = 0; j < colorList.length; j++) {
if (colorList[j][0] == imageAnalyzed.pixels[i]) {
int count = colorList[j][1];
colorList[j][1] = count +1;

colorExists = true; //color already exists in the array
}
}

//if the color hasn't been added to the array
if (colorExists == false) {
colorList = (int[][])append(colorList, new int[]{imageAnalyzed.pixels[i],1}); //add it
}
}
return true;
}


It extracts all the different colors in a GIF image and keeps a count of them. The code should work for any image type, but like I said I really suggest using a gif in order to save time.

Cheers
Re: color in an image
Reply #4 - Nov 25th, 2007, 9:23pm
 
Thanks to you both! Steven... thank you so much. This is exactly what I was looking for. I had done as Cosmin had suggested, but it was keeping track of existing colors that gave me trouble.

Thanks again!
- noobist
Page Index Toggle Pages: 1