views:

155

answers:

3

I would like to extract the most used colors inside an image, or at least the primary tones Could you recommend me how can I start with this task? or point me to a similar code? I have being looking for it but no success.

+3  A: 

I agree with the comments - a programming solution would definitely need more information. But till then, assuming you'll obtain the RGB values of each pixel in your image, you should consider the HSV colorspace where the Hue can be said to represent the "tone" of each pixel. You can then use a histogram to identify the most used tones in your image.

Jacob
A: 

Well, I assume you can access to each pixel RGB color. There are two ways you can so depending on how you want it.

First you may simply create some of all pixel's R, G and B. Like this.

A pseudo code.


int Red   = 0;
int Green = 0;
int Blue  = 0;
foreach (Pixels as aPixel) {
    Red   += aPixel.getRed();
    Green += aPixel.getGreen();
    Blue  += aPixel.getBlue();
}

Then see which is more.

This give you only the picture is more red, green or blue.

Another way will give you static of combined color too (like orange) by simply create histogram of each RGB combination.

A pseudo code.


Map ColorCounts = new();
foreach (Pixels as aPixel) {
    const aRGB   = aPixel.getRGB();
    var   aCount = ColorCounts.get(aRGB);
    aCount++;
    ColorCounts.put(aRGB, aCount);
}

Then see which one has more count. You may also reduce the color-resolution as a regular RGB coloring will give you up to 6.7 million colors.

This can be done easily by given the RGB to ranges of color. For example, let say, RGB is 8 step not 256.

A pseudo code.



function Reduce(Color) {
    return (Color/32)*32; // 32 is 256/8 as for 8 ranges.
}
function ReduceRGB(RGB) {
    return new RGB(Reduce(RGB.getRed()),Reduce(RGB.getGreen() Reduce(RGB.getBlue()));
}

Map ColorCounts = new();
foreach (Pixels as aPixel) {
    const aRGB   = ReduceRGB(aPixel.getRGB());
    var   aCount = ColorCounts.get(aRGB);
    aCount++;
    ColorCounts.put(aRGB, aCount);
}

Then you can see which range have the most count.

I hope these technique makes sense to you.

NawaMan
Nice! thank you very much, really useful!
biquillo
+2  A: 

You can get very good results using an Octree Color Quantization algorithm. Other quantization algorithms can be found on Wikipedia.

Mark Ransom
This is exactly what I was looking for! Thank you very much!
biquillo