Average colour of an image

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?".
richardhinton

Average colour of an image

Postby richardhinton » 2007-03-29T03:41:07-07:00

I'm looking to find out the average colour of an image. So if the main element in a picture is a green apple, the average colour would come out with a green hex value. Any ideas?

User avatar
anthony
Posts: 8736
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia
Contact:

Re: Average colour of an image

Postby anthony » 2007-03-29T20:13:09-07:00

For a rough average just -resize your image to 1 pixel!!!!

Code: Select all

  convert image.jpg -resize 1x1\! txt:-

For an exact average add -filter box, or use -scale instead of -resize.

Another alturnative is to get Im to return the best color to represent the image
in terms of the current color space by using -colors 1

For example here is a summery of the above for the rose: image...

Code: Select all

convert rose: -resize 1x1\! -depth 8 txt:- | tail -1
0,0: (154, 86, 78)  #9A564E  rgb(154,86,78)
convert rose: -scale 1x1\! -depth 8 txt:- | tail -1
0,0: (146, 89, 80)  #925950  rgb(146,89,80)
convert rose: -colors 1 -depth 8 txt:- | tail -1
69,45: (146, 89, 80)  #925950  rgb(146,89,80)
convert rose: -colorspace GRAY -colors 1 -depth 8 txt:- | tail -1
69,45: (105,105,105)  #696969  rgb(105,105,105)
convert rose: -colorspace CMYK -colors 1 -depth 8 txt:- | tail -1
69,45: (254,216,216)  #FED8D8  rgb(254,216,216)


That last seems to produce a very light color for some unknown reason.

For other methods of determining image types or other image metrics see
IM Examples, Comparing Images (under construction)
http://www.imagemagick.org/Usage/compare/

This includes predominate foreground/background, sky/ground colors, and other known techniques. If you have some small example images, to use or other suggestions for image identification and simplification techniques, then please mail me, add to the forum.

This is an area that a lot of people seem to be interested in, but few contribute or help out with.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/

richardhinton

Re: Average colour of an image

Postby richardhinton » 2007-03-30T03:56:15-07:00

Thanks for the reply.

I'm new to image processing and some of this does go aver the top of my head. Also I'm not using the command line style interface for IM, I'm using Code Projects ImageMagickNET .NET interface.

What I don't get is how to extract information from the ImageMagick.Image object. Even if I just scale the iamge to 1x1 pixels, how do I get the colour information of just that one pixel?

I do think that ImageMagick is a trully stunning piece of software and the image conversion times are lightening fast with excellent results.

After doing a bit of research I've come to the conclusion that the best way to extract the predominant colours in an image (to be used to search a library of images based on colour) is to break the image up into 9 equal sections and then find the average colour of each of those sections. As you have said, scaling each of these sections to just 1x1 pixels will give a basic average colour. Then by storing these 9 colour values against the image you can search the images by colour. There can always be some sort of check done before hand which checks each of the 9 colours against the other colours and if they are not different enough by a given tollerance then discard the colour value so as not to have duplicates.

My problem now is that the ImageMagickNET interface has lots of operations that set values to manipulate the image in some way but not many functions that return values. Is this just that the .NET wrapper doesn't expose enough functionality or that the ImageMagick library underneath doesn't expose the kind of functions that I want.

Thanks for your help
Rich

User avatar
anthony
Posts: 8736
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia
Contact:

Re: Average colour of an image

Postby anthony » 2007-03-30T07:11:01-07:00

So you are after image sorting metrics. That is mostly what 'Comparing Images' in IM examples is about.

Note however that predonimate color is not generally regarded as a good metric, Basic classification would be better. (sketch vs cartoon vs photo). Then more specific.

But for what you describe with a 3x3 color matrix, just 'scale' the image to 3x3\! The exclaimation tells IM to disregard aspect ratio during the process.

Just remeber that colors are themselves a 3 dimensional value, and then you need to decide if you compare basied on RGB color space, or YIQ, or CMYK. And finally, what about images containing 'spam text' in one corner, has extra 'framing', was 'cropped' or 'divided into two', or color changed to highligh some detail. Any of these things can throw of your 'color index'. Don't think it can't happen, I've seen all of these, in a archive of web images I tried to sort out and match up.

As for the API (application interface) you use. What I do on the comamnd line should in principle be able to be done in that API.

As for extractingthe colors, save to a image txt: format as I did in the examples above. In that case I save the 'txt' image to the terminal using '-' as the file name, but color.txt can also be used too. See http://www.imagemagick.org/Usage/formats/#txt for more on that.

In any case, please let the forum, or just me, know what happens and what you decide, and your results. Feedback on this type of thing is rare, and most welcome.

Question, do you have an 18 dimensional (3x3 colors of 3 values each) sorting, clustering or segmentation algorithm?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/


Return to “Users”

Who is online

Users browsing this forum: No registered users and 12 guests