Page 1 of 1
Finding the average colour of an Image
Posted: Sat Feb 28, 2004 9:38 pm
by Pointybeard
Hi guys, i just want to throw a question at you...I saw some guys site once where he allowed you to search for images based on the averge colour information stored about it. IE put in #00ff00 and it will find a bunch of images that come close to containing alot of green. I tried to emulate this by writing the following function
Code: Select all
<?php
//Finds the average colour of all pixels in an image
//and returns that as a hex value. n is the step for the
//for loops.
function findAvCol($img_res, $n=1){
$r = 0; $g = 0; $b = 0;
$count =0;
$imgX = imageSX($img_res);
$imgY = imageSY($img_res);
for($xx = 0; $xx < $imgX; $xx+=$n){
for($yy = 0; $yy < $imgY; $yy+=$n){
$rgb = ImageColorAt($img_res, $xx, $yy);
$r += ($rgb >> 16) & 0xFF;
$g += ($rgb >> & 0xFF;
$b += $rgb & 0xFF;
$count++;
}
}
return dechex($r/$count) .
dechex($g/$count) .
dechex($b/$count);
}
?>
There is one problem with this algorithm....give it an image with a very large spectrum of colours and you get a colour close to grey as the result. Anyone out there think of some ways to make this algorithm more effective?
Granted, this isnt a coding forum i know. Im just chasing some algorithm design ideas. Thanks guys
-PB
Posted: Sat Feb 28, 2004 10:58 pm
by ilovetoast
This is certainly appropriate here as it is an advanced algorithm issue. My 2 cents on that.
I have nothing to add tonight. Maybe tomorrow I'll think of something.
I love that sig. Is that from anything? My Spoon is too big. LMAO.
peace
Posted: Sun Feb 29, 2004 12:47 am
by Pointybeard
Thanks ilovetoast. Nothing to stress over. Its just somthing i was conjuring up in my spare time. Want to add that sort of search functionality to a photo gallery i run. tis' all.
I love that sig. Is that from anything?
haha. yea. Its from a quirky cartoon some guy did, ill see if i can find the link. Its basically a bunch of very random skits. it reminded me of the ticks catch cry "SPOON". lol
Re: Finding the average colour of an Image
Posted: Sun Feb 29, 2004 1:13 am
by penguinboy
Pointybeard wrote:
There is one problem with this algorithm....give it an image with a very large spectrum of colours and you get a colour close to grey as the result. Anyone out there think of some ways to make this algorithm more effective?
Hmm,
Perhaps you could write 2 algorithms:
The 1st for seaching for all colors.
The 2nd for searching for all non-grayscale colors.
Then you could do something like:
Code: Select all
<?php
if((!searching_for_a_grayscale_color)&&
(image_color_spectrum > a_defined_limit))
//use 2nd algorithm
else
//use 1st algorithm
?>
Edit:
The cartoon:
Rejected
Posted: Sun Feb 29, 2004 2:23 am
by Pointybeard
The cartoon: Rejected
Thats the one.
As for the algorithm, its not so much that theres lots of grey in the images, its that the bigger the spectrum of colours, the more likely grey is going to be the result. Of course it does work better with an image which is made up mostly of a single colour, than its not too bad.
Maybe if i find which colour is more prevalent and use weights to decide the final colour rather than just using a basic average idea. ??? Any thoughts? Thanks again guys
Posted: Sun Feb 29, 2004 2:32 am
by Pointybeard
Hmm, intersting. By playing around with the saturation of the resultant colour in photoshop, im getting the sort of range of colours im looking for. SO maybe weighting R, G or B accordingly would help.
Posted: Tue Mar 02, 2004 10:01 pm
by bg
I don't know how well this may or may not work, but try taking the the median or mode and seeing if works better.
Posted: Tue Mar 02, 2004 10:13 pm
by ilovetoast
Is it feasible to index the images as a spider does pages - and then use a mode + stnd. deviation from the mode of the hue (h/s/b) as a % releveance to return results.
Is there some sort of map like expression of the color spectrum? Then you could set the mode as a point on that map and use the search to return those images whose own mode is closest in map distance to the search color.
Some thoughts.
Posted: Tue Mar 02, 2004 10:19 pm
by Pointybeard
Well i was playing around with the algorithm and i changed it slightly. Now instead of just adding all the channels up, i add only the more dominant channel and 0 for the other 2. Heres what it looks like now.
Code: Select all
<?php
//Finds the average colour of all pixels in an image
//and returns that as a hex value. n is the step for the
//for loops.
function findAvCol($img_res, $n=1){
$r = 0; $g = 0; $b = 0;
$count =0;
$imgX = imageSX($img_res);
$imgY = imageSY($img_res);
for($xx = 0; $xx < $imgX; $xx+=$n){
for($yy = 0; $yy < $imgY; $yy+=$n){
$rgb = ImageColorAt($img_res, $xx, $yy);
$r_tmp = ($rgb >> 16) & 0xFF;
$g_tmp = ($rgb >> & 0xFF;
$b_tmp = $rgb & 0xFF;
if($r_tmp>$g_tmp && $r_tmp>$b_tmp)
$r += $r_tmp;
elseif($g_tmp>$r_tmp && $g_tmp>$b_tmp)
$g += $g_tmp;
elseif($b_tmp>$r_tmp && $b_tmp>$g_tmp)
$b += $b_tmp;
$count++;
}
}
$r = $r/$count;
$g = $g/$count;
$b = $b/$count;
/*This bit just makes the channels brighter
so i can see the colour when i display it in the
browser*/
/*
$rp = ($r / 255) * 100;
$gp = ($g / 255) * 100;
$bp = ($b / 255) * 100;
$rp += ($rp * .7);
$gp += ($gp * .7);
$bp += ($bp * .7);
$r += $rp;
$g += $gp;
$b += $bp;
*/
$r = min($r, 255);
$g = min($g, 255);
$b = min($b, 255);
return dechex($r) .
dechex($g) .
dechex($b);
}
?>
Posted: Wed Mar 03, 2004 1:17 am
by Pointybeard
ilovetoast wrote:Is it feasible to index the images as a spider does pages - and then use a mode + stnd. deviation from the mode of the hue (h/s/b) as a % releveance to return results.
Hmm..thats an interesting idea.
ilovetoast wrote:Is there some sort of map like expression of the color spectrum? Then you could set the mode as a point on that map and use the search to return those images whose own mode is closest in map distance to the search color.
Yea, i think i need to head in that direction. I need a way to get percentage matches. I'll keep working away at it. See what i can come up with. Thanks again...
Posted: Wed May 12, 2004 10:36 am
by dave420
I wrote code to do this. The average-colour-finding thing is easy enough to do, as all you have to do is go over a grid of pixels in the image (you don't have to do every one, as a spacing of 2x2 will still give you roughly the same colour). Once you have the average colour, you can use a function to find the relative distance between the searched-for colour and the colour of your image. Caching this in the database upon creation speeds up matching. In my gallery, you could search for images closest to a given colour, and it'd return all the images sorted by similarity.
You can add up all the reds, add up all the greens and add up all the blues, divide all by the number of samples you took, and there's your average colour. Experimenting with the spacing of the samples will yield the best match of colour-accuracy and processing speed. You can search google for the colourspace-distance algorithm (I did

)
Posted: Thu May 13, 2004 3:53 am
by Pointybeard
hey, thanks for the idea. Actaully i posted some more info about what i have been doing, but unfortunately that was lost in the forum crash. Nps tho. I have done alot more on it. Now i have histograms for each channel and also std dev, mean and median. Ive been looking into Histogram intersection. Slowly getting there. :p Thanks for the tips tho. I'll check it out and let you know how i go. ....hmm looks like my avatar was lost too...better fix that.
-PB
Posted: Thu May 13, 2004 4:04 am
by JayBird
Would love to see this code when you have it working
Mark
Posted: Thu May 13, 2004 4:19 am
by Pointybeard
hehe. Well i dont plan on hording it. So yea, i'll post it up when im done.

Donno when that will be, soon i hope, ive just been working on it in my spare time (which i have increasingly less of). The main reason i want it is so i can have my website change img & colour scheme depending on the time of day. Each hour would be mapped to a colour which would be used to dynamicly generate the style sheet, selecting images from a large repositry. Idea ive been throwing around for a while now. Course doesnt have to be time of day, could be
weather or song im playing or mood im in. heh.
Posted: Thu May 13, 2004 4:20 am
by JayBird
Pointybeard wrote:hehe. Well i dont plan on hording it. So yea, i'll post it up when im done.

Donno when that will be, soon i hope, ive just been working on it in my spare time (which i have increasingly less of). The main reason i want it is so i can have my website change img & colour scheme depending on the time of day. Each hour would be mapped to a colour which would be used to dynamicly generate the style sheet, selecting images from a large repositry. Idea ive been throwing around for a while now. Course doesnt have to be time of day, could be
weather or song im playing or mood im in. heh.
I like your thinking
Excellent
Mark