Page 1 of 1

Analyzing colors

Posted: 2009-07-09T03:52:39-07:00
by Thomas Jensen
Hi all,

I'am hosting a website with different kinds of artwork and providing a search feature which help people find art.
When users uploads images of their art, i use Imagick to crop and resize into different versions. What i thought could be really cool, was if i could analyze the colors of the image and then make them searchable.
Like:
ImageA: Red: 25%, Green: 20%, Blue: 60%
ImageB: Red 5%, Green 60%, Blue: 10%

Or similar, then i could store these values and let people find images by color.

The problem is that I'am not sure how to get these average color values from a image.

Thanks in advance!

Re: Analyzing colors

Posted: 2009-07-09T04:44:33-07:00
by Thomas Jensen
I tried to use the getColor function, and here is what I've come up with so far:

Code: Select all

$image = new Imagick( "some-image.jpg" );
$it = $image->getPixelIterator();
$it->resetIterator();

while($row = $it->getNextIteratorRow()) {
    foreach ($row as $pixel) {
    	$i++;
        $colors = $pixel->getColor();
        $r += $colors['r'];
        $g += $colors['g'];
        $b += $colors['b'];
        $a += $colors['a'];
    }
}
$red = $r/$i;
$green = $g/$i;
$blue = $b/$i;

$total = $red+$green+$blue;

echo 'Red: ' . $red/$total . '<br />';
echo 'Green: ' . $green/$total . '<br />';
echo 'Blue: ' . $blue/$total . '<br />';
echo 'Light: ' . $total/(255+255+255) . '<br />';
It should calculate the ratio between the colors.
Is this the correct/fastest/easiest solution?

And i still don't have a clue on how to use SQL to search values from many images.

Re: Analyzing colors

Posted: 2009-07-09T05:02:51-07:00
by Bonzo
There is a site below with lots of examples of Imagick use and an artical half way down you may find useful.

http://valokuva.org/?cat=1

Re: Analyzing colors

Posted: 2009-07-09T06:10:16-07:00
by Thomas Jensen
Thanks,

I've read his article, but i found my solution faster and more accurate than quantizeImage.

Re: Analyzing colors

Posted: 2009-12-12T05:36:26-07:00
by Thomas Jensen
Hmm.. Looks a lot like spam to me, he isn't replying anything, and is thanking a post, which was a question and doesn't help anybody and wasn't supposed to.

Re: Analyzing colors

Posted: 2009-12-12T08:41:46-07:00
by fmw42
I don't use Imagick, but in command line, I would separate the rgb channels and just get the mean value of each channel using either string formats or fx calculations.

see
http://www.imagemagick.org/script/escape.php
http://www.imagemagick.org/script/fx.php

convert image -colorspace rgb -separate image_%d
red=`convert image_0 -format "%[mean]" info:`
green=`convert image_1 -format "%[mean]" info:`
blue=`convert image_2 -format "%[mean]" info:`

I don't know if there are the equivalents in Imagick

Re: Analyzing colors

Posted: 2009-12-12T08:46:16-07:00
by Thomas Jensen
I ended up with this solution, which works as supposed. Thanks.

Code: Select all

$image = new Imagick('testfile.jpg');

$it = $image->getPixelIterator();
$it->resetIterator();

$i = 0;
$r = 0;
$g = 0;
$b = 0;
while($row = $it->getNextIteratorRow()) {
    foreach ($row as $pixel) {
    	$i++;
        $colors = $pixel->getColor();
        $r += $colors['r'];
        $g += $colors['g'];
        $b += $colors['b'];
    }
}
$red = $r/$i;
$green = $g/$i;
$blue = $b/$i;

$total = $red+$green+$blue;

echo 'red: ' . $red/$total . '<br />';
echo 'green: ' . $green/$total . '<br />';
echo 'blue: ' . $blue/$total . '<br />';
Feel free to comment and suggest any performance or visually improvements.