Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get dominant colors from image discarding the background

What is the best (result, not performance) algorithm to fetch dominant colors from an image. The algorithm should discard the background of the image.

I know I can build an array of colors and how many they appear in the image, but I need a way to determine what is the background and what is the foreground, and keep only the second (foreground) in mind while read the dominant colors.

The problem is very hard especially for gradient backgrounds or backrounds with patterns (not plain)

like image 428
astropanic Avatar asked Sep 14 '09 14:09

astropanic


3 Answers

Isolating the foreground from the background is beyond the scope of this particular answer, but...

I've found that applying a pixelation filter to an image will draw out a really good set of 'average' colours.

Before

before

After

enter image description here

I sometimes use this approach to derive a pallete of colours with a particular mood. I first find a photograph with the general tones I'm after, pixelate and then sample from the resulting image.

(Thanks to Pietro De Grandi for the image, found on unsplash.com)

like image 104
Drew Noakes Avatar answered Nov 15 '22 05:11

Drew Noakes


The colour summarizer is a pretty sweet spot for info on this subject, not to mention their seemingly free XML Web API that will produce descriptive colour statistics for an image of your choosing, reporting back the following formatted with swatches in HTML or as XML...

  • what is the average color hue, saturation and value in my image?
  • what is the RGB colour that is most representative of the image?
  • what do the RGB and HSV histograms look like?
  • what is the image's human readable colour description (e.g. dark pure blue)?

The purpose of this utility is to generate metadata that summarizes an image's colour characteristics for inclusion in an image database, such as Flickr. In particular this tool is being used to generate metadata for Flickr's Color Fields group.

enter image description here

In my experience though.. this tool still misses the "human-readable" / obvious "main" color, A LOT of the time. Silly machines!

like image 37
Alex Gray Avatar answered Nov 15 '22 04:11

Alex Gray


I would say this problem is closer to "impossible" than "very hard". The only approach to it that I can think of would be to make the assumption that the background of an image is likely to consist of solid blocks of similar colors, while the foreground is likely to consist of smaller blocks of dissimilar colors.

If this assumption is generally true, then you could scan through the whole image and weight pixels according to how similar or dissimilar they are to neighboring pixels. In other words, if a pixel's neighbors (within some arbitrary radius, perhaps) were all similar colors, you would not incorporate that pixel into the overall estimate. If the neighbors tend to be very different colors, you would weight the pixel heavily, perhaps in proportion to the degree of difference.

This may not work perfectly, but it would definitely at least tend to exclude large swaths of similar colors.

like image 5
MusiGenesis Avatar answered Nov 15 '22 03:11

MusiGenesis