Quantcast
Channel: magick Discussions Rss Feed
Viewing all articles
Browse latest Browse all 3693

New Post: Quantization & Unique (Dominant) Colors

$
0
0
If you want to use the most dominant colors you are probably better of using the map method:
// Read source image.using (MagickImage image = new MagickImage(@"D:\images\png\rainbow.png"))
{
  // Create collection to store top 8 colors.using (MagickImageCollection colors = new MagickImageCollection())
  {
    // Get the histogram of the image (key = color, value = count).
    Dictionary<MagickColor, int> histogram = image.Histogram();

    // Add the 'top 8 colors' to the collection.foreach (MagickColor color in (from c in histogram
                                   orderby c.Value descendingselect c.Key).Take(8))
    {
      colors.Add(new MagickImage(color, 1, 1));
    }

    QuantizeSettings settings = new QuantizeSettings
    {
      // Due to a bug you have to set it to null instead of DitherMethod.No.// This will be fixed in next release.
      DitherMethod = null
    };

    // Create a color map with the 'top 8 colors'.using (MagickImage colorMap = colors.AppendHorizontally())
    {
      // Map image to the colors in the color map.
      image.Map(colorMap, settings);
    }
  }

  // Save output
  image.Write(@"D:\images\png\rainbow-8-colors.png");
}
Output: https://www.dropbox.com/s/r9q74nqji6kdk9w/rainbow-8-colors.png

Viewing all articles
Browse latest Browse all 3693

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>