PHP Developers Network

Determine dominant image color
Page 1 of 2

Author:  JayBird [ Mon Oct 17, 2005 11:02 am ]
Post subject:  Determine dominant image color


I currently have a plan to build a stock photo library that is going to be an addon to our internal intranet.

One feature i would like to have is...when the image is uploaded...the image is somehow analyzed, and the dominant color is somehow determined and stored in the database.

Then, users can search for "bluey" images or "yellowy" images etc.

I have seen this done on a stock photo site, but i have no idea if this is possible with PHP and GD, and even how to go about it.

Any help appreciated.

Author:  Skara [ Mon Oct 17, 2005 12:07 pm ]
Post subject: 

Ok, set up variables as a point system. Something like..
$blue = 0;
$red = 0;
$green = 0;
Then analyze each pixel in the image. Only including red, blue and green, you'd just see which has the highest value, then give the appropriate value a 'point.' To check for light/dark, you'd just add the three up and see if it's above/below a certain number.

As long as you can look at individual pixels and see their color value, it's possible.

Seems a lot to do, though. You sure the site you're looking at doesn't just use keywords?

Author:  onion2k [ Mon Oct 17, 2005 2:16 pm ]
Post subject: 

Author:  JayBird [ Mon Oct 17, 2005 3:19 pm ]
Post subject: 

Mate, your a legend! Image

Thanks, looks like just what i need!!

Author:  onion2k [ Mon Oct 17, 2005 6:04 pm ]
Post subject: 

Author:  feyd [ Mon Oct 17, 2005 6:46 pm ]
Post subject: 

grrrraphics. ;)

Author:  Chris Corbyn [ Mon Oct 17, 2005 7:28 pm ]
Post subject: 

Author:  JayBird [ Tue Oct 18, 2005 3:48 am ]
Post subject: 

Author:  hukkas [ Sat Apr 19, 2008 5:52 am ]
Post subject:  Re: Determine dominant image color

I haven't been to York for ages....

Back on topic. This is almost what I'm looking for. I found this class: , which extracts the colours from an image as hex values, in descending order of frequency.

What I want to do is this;
- find the most dominant colours in an image
- maintain a finite list of colours in the database, that people can search against (probably around 15)

So as I see it, there are several things I need to do;

(I hope the following is relatively coherent - I'm just writing down what's going on in my head!!)

1. from the list extracted, work out which ones are dominant / frequent enough to warrant being stored against the image
2. given a search colour from a limited set, work out which ones are close enough to provide a match

Now presumably, from the number of pixels in the image and the frequency of a particular colour, I can work out a percentage, then skim off the most dominant colours accordingly. Question is, what on earth should the cut-off point be? Should it be a pre-determined threshold (e.g. 25%)? Should I simply take the x (e.g. 3,4) most frequent colours?

Then of course, I need to either:
- only store the colours that provide a reasonable match to one of my searchable colours, or
- only flag a match when a colour stored against an image is close enough to the colour being searched for

It's starting to sound like I need to come up with some pre-determined, hard-coded values; e.g., a colour is relevant if it comprises more than 25% of an image, and a colour is a match if ABS(".$r."-rVal)+ABS(".$g."-gVal)+ABS(".$b."-bVal) is less than delta.

Anyone got any thoughts as to how I do this, or indeed how I go about choosing these values?

Author:  onion2k [ Sat Apr 19, 2008 6:36 am ]
Post subject:  Re: Determine dominant image color

You shouldn't really bump a three year old thread..

What you've suggested there is the right approach anyway.

Author:  midwest22 [ Thu Jan 13, 2011 8:57 pm ]
Post subject:  Re: Determine dominant image color

I hate to re-bump a thread that is yet again over 2 years old since the last comment but Im looking for this solution as well.

Did you come up with the solution? If so, would you mind posting it so others (like me) can see how its done?


Author:  rmfloris [ Tue Jan 17, 2012 2:57 am ]
Post subject:  Re: Determine dominant image color

I tried the above code, but I do get an error message:
Parse error: syntax error, unexpected '&' in /home/webrage/public_html/foto/test/out.php on line 8

Removing the line, the script works perfect. What is wrong with the section:
Syntax: [ Download ] [ Hide ]
$g   = ($rgb >>  & 0xFF;


Author:  JayBird [ Tue Jan 17, 2012 3:07 am ]
Post subject:  Re: Determine dominant image color

Author:  XeroXer [ Tue Jan 17, 2012 3:41 pm ]
Post subject:  Re: Determine dominant image color

Author:  simonclark [ Wed Jan 09, 2013 2:15 am ]
Post subject:  Re:

Page 1 of 2 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group