Ask Your Question
6

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

asked 2012-12-11 04:54:37 -0600

Philip gravatar image

updated 2020-11-30 03:01:59 -0600

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 flag offensive close merge delete

5 answers

Sort by » oldest newest most voted
6

answered 2012-12-11 06:35:15 -0600

SR gravatar image

updated 2012-12-12 02:10:46 -0600

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.
edit flag offensive delete link more

Comments

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.

StevenPuttemans gravatar imageStevenPuttemans ( 2012-12-11 06:57:24 -0600 )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?

Philip gravatar imagePhilip ( 2012-12-11 07:05:50 -0600 )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?

StevenPuttemans gravatar imageStevenPuttemans ( 2012-12-11 09:46:04 -0600 )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!)

Jean-François Côté gravatar imageJean-François Côté ( 2013-02-11 10:43:47 -0600 )edit
5

answered 2012-12-11 06:50:36 -0600

Haris gravatar image

updated 2012-12-11 06:51:15 -0600

Converting RGB to HSV colour space may help you for colour based segmentation. You can refer this link for more information http://www.shervinemami.info/colorConversion.html ,where you can download ColorWheelHSV and which may help you to get an visual idea about HSV and RGB colour range.

edit flag offensive delete link more

Comments

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.

unxnut gravatar imageunxnut ( 2012-12-11 10:29:46 -0600 )edit
5

answered 2012-12-11 05:04:50 -0600

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!

edit flag offensive delete link more
2

answered 2016-03-20 15:27:10 -0600

mluthra gravatar image

updated 2016-03-20 15:29:04 -0600

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

edit flag offensive delete link more
0

answered 2016-03-21 05:19:30 -0600

essamzaky gravatar image

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

edit flag offensive delete link more

Question Tools

5 followers

Stats

Asked: 2012-12-11 04:54:37 -0600

Seen: 29,113 times

Last updated: Mar 21 '16