Analyzing colors

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
Thomas Jensen

Analyzing colors

Post 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!
Thomas Jensen

Re: Analyzing colors

Post 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.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Analyzing colors

Post 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
Thomas Jensen

Re: Analyzing colors

Post by Thomas Jensen »

Thanks,

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

Re: Analyzing colors

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Analyzing colors

Post 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
Thomas Jensen

Re: Analyzing colors

Post 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.
Post Reply