I am trying to find the dominant color pixel in an image using php. Actually I am hoping someone else has done it before I have to... This is just a fun project. I am thinking of this in php or perl. I haven't gotten beyond the idea process yet. Any suggestions or code? Here is a sample image. (I am going to try to ignore white and black. That being said reducing an image down to one pixel will not always work.)
7/25/2007 8:44:13 PM
What do you mean by dominant color?The most prevalent color, or the most contrasting/obvious color?
7/25/2007 11:07:39 PM
Yea, are we talking the MEAN or the AVERAGE? Big difference there, either way it's relatively easy to calculate, although if you are talking mean, it's likely there will be a LOT of ties and you'll probably have to do an averaged mean along the color scale to pick just one
7/26/2007 2:34:24 AM
The most prevalent color
7/26/2007 7:10:54 AM
That means the Median color, if I am understanding you correctly, right?? The color that shows up on the most pixels in the image??
7/26/2007 7:57:25 AM
that would be the mode
<?php$im = ImageCreateFromJpeg("example.jpg");$width = imagesx($im);$height = imagesy;for($i=1; $i<=$width; $i++) { for($j=1; $j<=$height; $j++) { $index=($i-1)*$height+$j; $rgb[$index] = ImageColorAt($im, $i, $j); }}?>
7/26/2007 8:07:02 AM
$im = ImageCreateFromgif("ys.gif");$width = imagesx($im);$height = imagesy($im);for($i=1; $i<=$width; $i++) { for($j=1; $j<=$height; $j++) { $index=($i-1)*$height+$j; $rgb[$index] = ImageColorAt($im, $i, $j); }}$count = array_count_values($rgb);arsort($count);foreach($count as $key => $val){ $r = ($key >> 16) & 0xFF; $g = ($key >> 8) & 0xFF; $b = $key & 0xFF; echo "<p>$key = $val ".sprintf('#%06x',$key)." r=$r g=$g b=$b";}
184 = 19166 #0000b8 r=0 g=0 b=18423 = 7233 #000017 r=0 g=0 b=2313 = 1896 #00000d r=0 g=0 b=1315 = 728 #00000f r=0 g=0 b=158 = 691 #000008 r=0 g=0 b=8
7/27/2007 3:15:23 PM
haha, not the same by any means, but i liked writing a quick script to make fun of the "drawing w/ html" video
7/27/2007 5:28:36 PM
^^you get no reds at all? or just in the top 5...what happens if you just spit out R values after:$rgb[$index] = ImageColorAt($im, $i, $j);in other words check to see if there is an r value, and only echo if there is. My guess is the counting/sorting is messing something up.
9/28/2007 2:52:58 PM
are you trying to do one of those big pictures made out of a bunch of smaller pictures?
9/28/2007 2:59:13 PM
might have to try that... i was thinking of showing random pics on a website and having the dominant color be the color of borders and things of that nature in the css as well. not related, but a good example. if i have a pic of the cookie monster, the blue would be the dominant color. there are a few other reasons for this, but this is an example... [Edited on September 28, 2007 at 9:44 PM. Reason : adsf]
9/28/2007 9:44:11 PM