Extract dominant / most commonly used colors from an image

I would like to extract the most used colors inside the image, or at least the basic tones. Could you recommend me how can I start with this task? or point me to a similar code? I am looking for him, but have not succeeded.

+5
source share
3 answers

You can get very good results using the Octree color quantization algorithm . Other quantization algorithms can be found on Wikipedia .

+4
source

- . , , RGB , HSV, , " " . histogram, .

+3

, , RGB-. , .

R, G B. .

.


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

, .

, .

(, ), RGB.

.


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

Then look which one is bigger. You can also reduce the color resolution since regular RGB dyeing will give you up to 6.7 million colors.

This can be done easily by setting RGB color ranges. For example, let's say RGB has 8 steps, not 256.

Pseudocode



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 has the most.

I hope this technique makes sense to you.

+2
source

All Articles