Hello.
I'm trying to get the dominant colour (hexa code) in an image using the Magick.NET.
How can I do this?
How to get the dominant color in an image?
-
- 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?
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
- 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?
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:
For example:
1975,#FEFEFE
117,#3F435E
93,#E1D9CF
87,#223D8F
84,#BEA7A3
59,#CE6357
54,#59668A
31,#AB9765
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.
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 ","
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 ","
117,#3F435E
93,#E1D9CF
87,#223D8F
84,#BEA7A3
59,#CE6357
54,#59668A
31,#AB9765
Code: Select all
convert -size 100x100 xc:"#FEFEFE" dominant.gif
See my bash unix shell script, dominantcolor, at my link below for examples.
Re: How to get the dominant color in an image?
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.
Re: How to get the dominant color in an image?
Hello everyone.
I was able to implement it so here's the code if someone else needs to perform this action.
Cheers
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");
}