How to get the dominant color in an image?

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
GoncaloM
Posts: 18
Joined: 2019-05-23T08:20:39-07:00
Authentication code: 1152

How to get the dominant color in an image?

Post by GoncaloM »

Hello.
I'm trying to get the dominant colour (hexa code) in an image using the Magick.NET.
How can I do this?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to get the dominant color in an image?

Post by snibgo »

What do you mean by "dominant colour"? Perhaps you can link to sample images and tell us what the dominant colour is.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to get the dominant color in an image?

Post by fmw42 »

Scale the image to say 50x50. Then reduce colors to some small number, say 8 at 8-bit depth. The get the histogram and sort it by decreasing count and take the top one.

Sorry, I do not know Magick.NET


In unix command line:

Code: Select all

convert image -scale 50x50! -depth 8 +dither -colors 8 -format "%c" histogram:info: |\
sed -n 's/^[ ]*\(.*\):.*[#]\([0-9a-fA-F]*\) .*$/\1,#\2/p' | sort -r -n -k 1 -t ","
For example:

Code: Select all

convert logo: -scale 50x50! -depth 8 +dither -colors 8 -format "%c" histogram:info: |\
sed -n 's/^[ ]*\(.*\):.*[#]\([0-9a-fA-F]*\) .*$/\1,#\2/p' | sort -r -n -k 1 -t ","
1975,#FEFEFE
117,#3F435E
93,#E1D9CF
87,#223D8F
84,#BEA7A3
59,#CE6357
54,#59668A
31,#AB9765

Code: Select all

convert -size 100x100 xc:"#FEFEFE" dominant.gif
So the dominant color is #FEFEFE, which is near white as expected from the logo: image.


See my bash unix shell script, dominantcolor, at my link below for examples.
GoncaloM
Posts: 18
Joined: 2019-05-23T08:20:39-07:00
Authentication code: 1152

Re: How to get the dominant color in an image?

Post by GoncaloM »

snibgo wrote: 2019-05-24T16:14:26-07:00 What do you mean by "dominant colour"? Perhaps you can link to sample images and tell us what the dominant colour is.
Hello.

The dominant colour is the colour that appears more times on an image, just like fmw42 shown.
I just wanted to know how to do it using Magick.NET.
GoncaloM
Posts: 18
Joined: 2019-05-23T08:20:39-07:00
Authentication code: 1152

Re: How to get the dominant color in an image?

Post by GoncaloM »

Hello everyone.

I was able to implement it so here's the code if someone else needs to perform this action.

Code: Select all

using (MagickImage image = new MagickImage(ssimageBinary))
{
	Dictionary<MagickColor, int> dict = image.Histogram();

	var keyOfMaxValue = dict.Aggregate((x, y) => x.Value > y.Value ? x : y).Key;

	System.Drawing.Color c = keyOfMaxValue.ToColor();
	ssdominantColourCode = "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
}
Cheers
Post Reply