# How to find the two most dominant colors in an image?

I have an image of some white text on a blue background. Actually the text could be any color and the background color be any color. The text has some aliasing on it as well. I wish to find the color of the text, so the background color would be one dominant color and the text color is another dominant color. How I can find these two colors?

Really in general I would like to know how to find the two most dominant colors in the image.

edit retag close merge delete

Sort by » oldest newest most voted

Another approach:

1. Optional: Quantize all colors of your image into e.g. 16 different colors per channel
2. Use k-means with k=2 and cluster all colors (one 3-d color vector per pixel) based on the euclidean distance between the colors. A small number of iterations should be sufficient. The two resulting clusters then separate the colors within your image into two classes.
3. Use the two cluster centroids (=color means) as your dominant colors.
more

I agree on this approach when not all pixels are exactly the identical value. So when taken pictures with camera, this approach is far better. When using manually made photoshop pictures, the approach of pure histogram voting will be faster.

( 2012-12-11 06:57:24 -0500 )edit

There is aliasing between the font and the background, would this approach work better due to the aliasing being mixed colors between the foreground and background?

( 2012-12-11 07:05:50 -0500 )edit

Best way is to implement them both on dummy data and do a speed test. It depends on the amount of aliasing I guess. But give it a try?

( 2012-12-11 09:46:04 -0500 )edit

It would be very interesting to have some sample code for the approach that you suggest @SR. I understand the idea but I'm strugeling with the code itself (with the k-means function in fact!)

( 2013-02-11 10:43:47 -0500 )edit

more

1

This will be a better approach. You can plot the histogram of the H component to find the two colors with maximum pixels. And it will also take care of saturation and brightness of colors.

( 2012-12-11 10:29:46 -0500 )edit

Perform an RGB histogram calculation of your image. Look inside the R, G and B channel which two components are the largest (background and color of letters). Then combine these largest values to detect the actual color that is dominant by combining the three channels into a single color.

If it is segmentation that you want to achieve, you could also perform a RGB to grayscale conversion and then perform the histogram voting to know the two dominant grayscale values.

Not sure which functions compute the histograms of an input image, but do a search on histogram in the openCV documentation: http://docs.opencv.org/

Cheers!

more

There is an awesome python tutorial added here with visualization using color quantization.

more

If you are are looking for method to isolate forground text from back ground , i read that the best method till now is SWT (Stroke Width Transform ) , the method is announced on 2010 by Epshtein and others in 2010 you can find the implementation and some description in the following link SWT implementation

the method works for any color text on any background , also it works when there is alot of text written in diffirent colors , you can use it as generlized text detection method

more

Official site

GitHub

Wiki

Documentation