Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast way of getting the dominant color of an image [closed]

People also ask

How do you make your color dominant?

By simply using a lot of one color in a design project, it can become dominant. It could be said that a few colors are used more dominantly in design than others as well. Blue and red are widely used in design projects, for example, because of associations with those colors.

What is the dominant color of an image?

Such colors are also called the dominant colors or the main colors as they are the most pervasive in a photo. The output color palette allows you to create beautiful designs and match the style of your JPG.


Your general approach should work, but I'd highlight some details.

Instead of your given list of colors, generate a number of color "bins" in the color spectrum to count pixels. Here's another question that has some algorithms for that: Generating spectrum color palettes Make the number of bins configurable, so you can experiment to get the results you want.

Next, for each pixel you're considering, you need to find the "nearest" color bin to increment. You'll need to define "nearest"; see this article on "color difference": http://en.wikipedia.org/wiki/Color_difference

For performance, you don't need to look at every pixel. Since image elements usually cover large areas (e.g., the sky, grass, etc.), you can get the result you want by only sampling a few pixels. I'd guess that you could get good results sampling every 10th pixel, or even every 100th. You can experiment with that factor as well.

[Editor's note: The paragraph below was edited to accommodate Mike Fairhurst's comment.]

Averaging pixels can also be done, as in this demo:jsfiddle.net/MUsT8/