User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » find dominant pixel color in image php Page [1]  
LeGo
All American
3916 Posts
user info
edit post

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

moron
All American
34144 Posts
user info
edit post

What do you mean by dominant color?

The most prevalent color, or the most contrasting/obvious color?

7/25/2007 11:07:39 PM

Noen
All American
31346 Posts
user info
edit post

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

LeGo
All American
3916 Posts
user info
edit post

The most prevalent color

7/26/2007 7:10:54 AM

robster
All American
3545 Posts
user info
edit post

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

BigMan157
no u
103354 Posts
user info
edit post

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);
}
}
?>


then find the mode of the array

or something, i dunno

[Edited on July 26, 2007 at 8:17 AM. Reason : that's the approach my sleep addled mind would take right now]

[Edited on July 26, 2007 at 8:19 AM. Reason : whoops, mode]

7/26/2007 8:07:02 AM

LeGo
All American
3916 Posts
user info
edit post


$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";
}


right now I am getting these as the top 5

184 = 19166 #0000b8 r=0 g=0 b=184
23 = 7233 #000017 r=0 g=0 b=23
13 = 1896 #00000d r=0 g=0 b=13
15 = 728 #00000f r=0 g=0 b=15
8 = 691 #000008 r=0 g=0 b=8


For some reason I am not getting any R or G. Any suggestions?

7/27/2007 3:15:23 PM

jaZon
All American
27048 Posts
user info
edit post

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

rynop
All American
829 Posts
user info
edit post

^^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

Smath74
All American
93278 Posts
user info
edit post

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

LeGo
All American
3916 Posts
user info
edit post

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

 Message Boards » Tech Talk » find dominant pixel color in image php Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.39 - our disclaimer.