most colors used (color analysis)

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
denisnikulin

most colors used (color analysis)

Post by denisnikulin »

Hi all.
i read http://valokuva.org/?p=72 here how to find out which colors are used most in any picture.
but, i don't know php at all and want to ask how can to do the same thing from console version of imagemagick?

the main task is to find 5 (for example) color ranges, which are used in picture most of all. to find out their rgb codes and the _percentage_ of each found color?
thanks!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: most colors used (color analysis)

Post by Bonzo »

This will give you the first 10 colours from the histogram but you would need to add some more code for what you want to do:

Code: Select all

<?php
exec("convert http://valokuva.org/wp-content/uploads/2007/11/strawberry.png -colors 256 -depth 8 -format \"%c\" histogram:info:", $a);

for ( $i=0; $i <= 9; $i++){
$find = preg_match("/#(.*)r/", $a[$i], $matches);
$colour = "#".trim($matches['1']);
$name = trim($matches['1']).".jpg";
exec("convert -size 100x20 xc:$colour $name");
echo '<br />' . $matches['1'];
echo "<img src=\"$name\"><br>";
}
?>
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Re: most colors used (color analysis)

Post by dognose »

That's a nice start, but Mikko's results are quiet different, and at least seemingly to better match the picture.

If I just use 8 colors, I get a better range of colors, but the seem duller than the original.

I'm not sure I even know the command equivalent of $bright->modulateImage ( 125, 200, 100 );
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: most colors used (color analysis)

Post by Bonzo »

As you say changing the code to say -colors 20 you get more of a range of colours. The results of my code seem to be nearer to Mikko's "Dark" range. You could always do a bit of extra work to get a "bright" range; adding a black border and reducing the size of the images make them look a bit better.
I seem to remember Anthony saying the colours will not be correct from the command line so you may need to add a "fudge factor" anyway.

You could probably modify the code to pick up the rgb colour as that would be simpler to work with ?

Sample array output:
Array
(
[0] => 23161: (237,240,243) #EDF0F3 rgb(237,240,243)
[1] => 11783: (147, 11, 6) #930B06 rgb(147,11,6)
[2] => 11252: (172, 17, 10) #AC110A rgb(172,17,10)
[3] => 9399: (211,220,221) #D3DCDD rgb(211,220,221)
[4] => 9262: (164,176,157) #A4B09D rgb(164,176,157)
[5] => 8004: (177, 45, 25) #B12D19 rgb(177,45,25)
[6] => 4732: (187,201,199) #BBC9C7 rgb(187,201,199)
[7] => 3612: (199, 89, 42) #C7592A rgb(199,89,42)
[8] => 3544: (209,173,147) #D1AD93 rgb(209,173,147)
[9] => 3425: (118, 14, 10) #760E0A rgb(118,14,10)
[10] => 3242: (201,113, 81) #C97151 rgb(201,113,81)
[11] => 2810: (205,147,105) #CD9369 rgb(205,147,105)
[12] => 2803: (149,147,110) #95936E rgb(149,147,110)
[13] => 2555: ( 92,101, 51) #5C6533 rgb(92,101,51)
[14] => 2151: (181,197,181) #B5C5B5 rgb(181,197,181)
[15] => 1752: (174,161, 49) #AEA131 rgb(174,161,49)
[16] => 1585: (175, 86, 60) #AF563C rgb(175,86,60)
[17] => 1448: (117,140, 83) #758C53 rgb(117,140,83)
[18] => 1355: (180, 81, 40) #B45128 rgb(180,81,40)
[19] => 1325: (208,203,173) #D0CBAD rgb(208,203,173)
[20] =>
)
Image
denisnikulin

Re: most colors used (color analysis)

Post by denisnikulin »

how is it possible to get the percentage of colors?
for example:

Code: Select all

convert picture.jpg -colors 2 -unique-colors txt:- 
it gives 2 most colors, used in picture. but what is the most of them? )
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: most colors used (color analysis)

Post by fmw42 »

denisnikulin wrote:how is it possible to get the percentage of colors?
for example:

Code: Select all

convert picture.jpg -colors 2 -unique-colors txt:- 
it gives 2 most colors, used in picture. but what is the most of them? )


convert picture.jpg -colors 2 -format "%c" histogram:info:
denisnikulin

Re: most colors used (color analysis)

Post by denisnikulin »

Code: Select all

convert picture.jpg -colors 8 -format "%c" histogram:info:
     97453: (61184,60758,60615) #EF00ED56ECC7 rgb(93.3608%,92.7108%,92.4926%)
     50455: (53641,41937,38071) #D189A3D194B7 rgb(81.8509%,63.9918%,58.0926%)
     27207: (51248,49702,48930) #C830C226BF22 rgb(78.1994%,75.8404%,74.6624%)
     17174: (50589,37617,32045) #C59D92F17D2D rgb(77.1939%,57.3999%,48.8975%)
     13658: (46876,35492,31058) #B71C8AA47952 rgb(71.5282%,54.1573%,47.3915%)
      9184: (11480,10128, 9827) #2CD827902663 rgb(17.5174%,15.4543%,14.995%)
      8162: (45638,41478,40078) #B246A2069C8E rgb(69.6391%,63.2914%,61.1551%)
      4155: (33062,32857,31055) #81268059794F rgb(50.4494%,50.1366%,47.3869%)
it is not the percentage of each color, it is RGB-code, but in unusual format..
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: most colors used (color analysis)

Post by fmw42 »

denisnikulin wrote:

Code: Select all

convert picture.jpg -colors 8 -format "%c" histogram:info:
     97453: (61184,60758,60615) #EF00ED56ECC7 rgb(93.3608%,92.7108%,92.4926%)
     50455: (53641,41937,38071) #D189A3D194B7 rgb(81.8509%,63.9918%,58.0926%)
     27207: (51248,49702,48930) #C830C226BF22 rgb(78.1994%,75.8404%,74.6624%)
     17174: (50589,37617,32045) #C59D92F17D2D rgb(77.1939%,57.3999%,48.8975%)
     13658: (46876,35492,31058) #B71C8AA47952 rgb(71.5282%,54.1573%,47.3915%)
      9184: (11480,10128, 9827) #2CD827902663 rgb(17.5174%,15.4543%,14.995%)
      8162: (45638,41478,40078) #B246A2069C8E rgb(69.6391%,63.2914%,61.1551%)
      4155: (33062,32857,31055) #81268059794F rgb(50.4494%,50.1366%,47.3869%)
it is not the percentage of each color, it is RGB-code, but in unusual format..
to get percentage, just divide the counts in the first column by the total number of pixels (area of the image)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: most colors used (color analysis)

Post by anthony »

Bonzo wrote:I seem to remember Anthony saying the colours will not be correct from the command line
I never said that!

I said that color quantization (reducing the number of colors) will generally pick colors in the middle of color groups. that is average colors.

As a result of this you do not get pure, or ultra bright colors, as these ly on th outside edges of any color group! However if a LOT of one specific color is used, IM will likely decide to use that color. This allows better handling of GIF images and avoids dither speckle. That does not help with color gradients however.


As for the discussion. I am watching this very closely, but don't have anything much to say at this point. The previous posts have been very good.

I would like to make one point. It is very hard to figure out how many colors will give a good coverage for an image. Too many and you get a
lot of closely matching colors. Too few and you get two very different sets of colors merging together to form a horrible (and probably gray looking) average.

A statistical measure of the 'best' number of colors may be something that should looked at and explored, in any color analysis.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
denisnikulin

Re: most colors used (color analysis)

Post by denisnikulin »

fmw42 wrote:
denisnikulin wrote:

Code: Select all

convert picture.jpg -colors 8 -format "%c" histogram:info:
     97453: (61184,60758,60615) #EF00ED56ECC7 rgb(93.3608%,92.7108%,92.4926%)
     50455: (53641,41937,38071) #D189A3D194B7 rgb(81.8509%,63.9918%,58.0926%)
     27207: (51248,49702,48930) #C830C226BF22 rgb(78.1994%,75.8404%,74.6624%)
     17174: (50589,37617,32045) #C59D92F17D2D rgb(77.1939%,57.3999%,48.8975%)
     13658: (46876,35492,31058) #B71C8AA47952 rgb(71.5282%,54.1573%,47.3915%)
      9184: (11480,10128, 9827) #2CD827902663 rgb(17.5174%,15.4543%,14.995%)
      8162: (45638,41478,40078) #B246A2069C8E rgb(69.6391%,63.2914%,61.1551%)
      4155: (33062,32857,31055) #81268059794F rgb(50.4494%,50.1366%,47.3869%)
it is not the percentage of each color, it is RGB-code, but in unusual format..
to get percentage, just divide the counts in the first column by the total number of pixels (area of the image)
wow! great! Thank you!
kamelkev

Re: most colors used (color analysis)

Post by kamelkev »

Does anyone know how to determine "distance" between colors? This might help with the variety issue mentioned above.

I am attempting to generate automatic color paletes from jpgs/pngs etc. My hope is to generate about 5 colors, and have them different enough that they seem like color paletes you would find at the colourlovers site, or something similar.

A problem I have had thus far is that when generating a set of colors its very difficult to determine how large the result set should be in order to return colors that are sufficiently different from one another.

In the above cases the images are being dithered, which helps with this problem - but it doesn't eliminate it. For example it is very possible to get upload a picture of a bear and get multiple browns within the image, and not get say... the green grass the bear is on.

This is a very interesting problem - I've sat here trying to think up of various algorithms to attempt to resolve the problems - but I figure you guys have probably come up with something much better than the crap I am trying :)

Thanks in advance to any smarties out there
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: most colors used (color analysis)

Post by anthony »

Distances between colors depend on the colorspace used. Some colorspaces represent the colors better than others (-colors using the colorspace defined by -quantize setting). RGB (and its complement CMYK) are not good spaces for this.

The distance can be either 'manhattan' which is where you just add the difference in Red Green and Blue distances (or whatever it it for your colorspace). The better metric is pythogorian, which is the teh square root, or the sum or the squares.

EG distance = sqrt( r_diff^2 + g_diff^2 + b_diff^2 )

That works quite well.

ASIDE: I strongly suggest when deciding colors to TURN OF DITHERING! Or dithering will skew your results!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: most colors used (color analysis)

Post by anthony »

kamelkev wrote:In the above cases the images are being dithered, which helps with this problem - but it doesn't eliminate it. For example it is very possible to get upload a picture of a bear and get multiple browns within the image, and not get say... the green grass the bear is on.
If you are after 'areas' of color then you need to start looking at color segmentation, rather color quantization.

The -segment option does do this but is very hard to use.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
kamelkev

Re: most colors used (color analysis)

Post by kamelkev »

I think I just exposed how little I know about what I am trying to do - I had thought dithering was a word used to describe the process of reducing the number of colors within an image, but that doesn't seem to be the case upon further reading.

I'm going to try and see if I can build a simple algorthm off of the segmentation option, everyone's comments have been pretty insightful in getting me started
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: most colors used (color analysis)

Post by anthony »

Quantization is the process of finding a reduced number of colors to best render an image (generally using a dithering process)

Dithering is the art of using two or three colors next to each other in the image to try and make that specific area of the image look like a blend of those colors.

that is Quantization finds the colors, Dither applies though colors.

the -colors operator does both, though you can turn off Dithering so that each pixel of the image will be replaced by the closest color to the original pixel the quantization routine found.

Currently their is no way to simply get a direct list of the colors the quantization rouinte found, and their is no gurantee that after all the pixels in the image have been replaced (dithered or non-dithered) that the image will have used ALL the colors the quantization routine found.

In fact I know that their is currently a BUG in the dither routine where it isn't actually using all the color the quantization routine found!
For example....

Code: Select all

  colors=256   # number of colors for test
  generate=`convert -size 100x$colors gradient:blue-red -format %k info:`
  quantize=`convert -size 100x$colors gradient:blue-red +dither \
            -colors $colors -format %k info:`
  dither_rm=`convert -size 100x$colors gradient:blue-red \
             -dither Riemersma -colors $colors -format %k info:`
  dither_fs=`convert -size 100x$colors gradient:blue-red \
             -dither FloydSteinberg -colors $colors -format %k info:`
cat <<EOF
    number of colors = $colors   colors
     => generate     `printf %3d $generate`  (from gradient:)
     => quantization `printf %3d $quantize`  (+dither -colors $colors)
     => dithering R  `printf %3d $dither_rm` (-dither Riemersma)
     => dithering FS `printf %3d $dither_fs` (-dither FloydSteinberg)
EOF
is returning
number of colors = 256 colors
=> generate 256 (from gradient:)
=> quantization 256 (+dither -colors 256)
=> dithering R 64 (-dither Riemersma)
=> dithering FS 95 (-dither FloydSteinberg)
In other words the image without dither returns 256 colors, but the two dithering methods for some reason is using less than 1/3 or even 1/4 of those colors!!!!

Their is no guarantee that even the un-dithered image would return all the colors.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply