Page 1 of 1

GD dominant colo(u)r function

Posted: Wed Nov 14, 2007 8:12 am
by mikeeeeeeey
Hi guys,

Anyone have any idea how to start a function which selects the dominant colour in a 50x50 square? I can store all RGB values into an array using a foreach() and this...

Code: Select all

$rgb = imagecolorat($img, $xpos, $ypos;
		$r   = ($rgb >> 16) & 0xFF;
		$g   = ($rgb >>  & 0xFF;
		$b   = $rgb & 0xFF;
however I'm not sure how to put this together :cry:

Any help appreciated, thanks in advance.

Posted: Wed Nov 14, 2007 9:28 am
by Kieran Huggins
so by dominant colour do you want the mean or mode?

Posted: Wed Nov 14, 2007 10:31 am
by feyd
Generally speaking, you would convert the color data to a histgram format. The largest stack is the dominant color. :)

Posted: Wed Nov 14, 2007 10:36 am
by Kieran Huggins
you could resample the image to 1x1 and test the resulting pixel color for the "mean" or average colour.

The mode would be trickier, but doable. I'd start by converting to an indexed format, thereby letting the image algorithm do the heavy lifting for you. Then you can simply tabulate the number of pixels that map to each colour and sort to find the most common.