首页 > 解决方案 > Magick.NET Evaluate Red Channel

问题描述

I'm using Magick.NET to apply colour corrections to photographs. I adjust red, green and blue channels by adding or subtracting a percentage to each using the Evaluate method. The value here is the +/- amount of change to apply to the specified channel.

        image.Evaluate(channel, EvaluateOperator.Add, new Percentage(value));

Adding colour to a channel is fine, but removing colour from a channel will change the colour balance of white in the image (remove red, the image becomes green/blue). I need to be able to apply the adjustment to each channel without changing white.

I've tried applying Level after Evaluate, and also ContrastStretch, thinking that I could specify a black/white point below/above which the adjustment is ignored.

ColorMatrix looks promising but gives really weird results and Modulate does colour rotation, which isn't right.

Thanks

标签: c#imagemagickmagick.net

解决方案


tldr; 创建一个白色蒙版并使用 .WriteMask() 将其应用于图像,以便从 .Evaluate(..) 调用中排除任何白色区域。

        var newImage = magickImage.Clone();
        var stats = newImage.Statistics().GetChannel(PixelChannel.Composite);
        var mean = stats.Mean / (stats.Maximum - stats.Minimum);
        var stDev = stats.StandardDeviation / (stats.Maximum - stats.Minimum);
        var whiteThreshold = new Percentage(100 - (mean + 0.5 * stDev));
        var blackThreshold = new Percentage(mean - 0.5 * stDev);

        newImage.ColorFuzz = new Percentage(3);
        newImage.WhiteThreshold(whiteThreshold);
        newImage.BlackThreshold(blackThreshold);

        newImage.Opaque(MagickColors.Black, MagickColors.Green);
        newImage.Opaque(MagickColors.White, MagickColors.Black);
        newImage.InverseOpaque(MagickColors.Black, MagickColors.White);

        magickImage.WriteMask = newImage;

有用的网站包括https://www.imagemagick.org/script/index.phphttp://www.fmwconcepts.com/imagemagick/index.php。感谢 Fred 的“色彩平衡”脚本,这是一个非常好的示例,说明如何使用 ImageMagick 命令行执行此操作。


推荐阅读