Page 1 of 1

Detect Main Color of Image

Posted: 2010-08-27T10:12:50-07:00
by blank
Hello,

I am writing a script to detect the most predominant color of an image. And it works great,
I'm currently using the convert script in this way:

convert "download/file.php?avatar=1663_1180055717.gif" -colors 16 -geometry x50 histogram:- | identify -format %c - | sort -r > all_colors_for_this_image.txt

Then I do a regular expression to find the first color in the text file all_colors_for_this_image.txt

This method works brilliantly, but what I'm finding is that I get too many colors back. Instead of one specific color, I wish to generalize the color. So that instead of getting 100 blacks (#000000, #000001 etc) the colors are rounded to the closest whatever.

I can probably do this using a mathematical algorithm by rounding down the RGB values, and I'm sure it would be a solution, but not a very good one.

So my question is - is there a way to take only a certain amount of colors, and merge many colors into one?

I tried making the image only 16 colors as you can see, but it didnt work so well I also found that if I made this variable 4 colors then it would return lots of grays.

Thanks!

Andrew

p.s. I've done some research online, and I've looked in the docs, but have not found a solution yet :( Maybe I looked in the wrong place

Re: Detect Main Color of Image

Posted: 2010-08-27T10:29:34-07:00
by fmw42
Not an easy solution.

see color quantization at http://www.imagemagick.org/Usage/quantize/

see -segment at http://www.imagemagick.org/script/comma ... hp#segment

sese -unique-colors at http://www.imagemagick.org/script/comma ... que-colors and http://www.imagemagick.org/Usage/files/#txt

You may want to look outside IM for other approaches.

Re: Detect Main Color of Image

Posted: 2010-08-29T18:50:52-07:00
by anthony
The problen is basically that -color does not find a 'pre-dominate-color' but it finds a range of colors to 'best represent' the image.

That is if you take a picture of a tree, and color reduce you may only get one or two brown colors for the bark, but you will gate lots of different 'greens' for the leaves.

Segment_image is really a 'color segmention' algorthim and will probably work better. But not always, and its options have never been very clear.