c# - 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
解决方案
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.php和http://www.fmwconcepts.com/imagemagick/index.php。感谢 Fred 的“色彩平衡”脚本,这是一个非常好的示例,说明如何使用 ImageMagick 命令行执行此操作。
推荐阅读
- javascript - 有什么方法可以在 webworker 中加载一个类,而不仅仅是普通的 javascript 文件?
- docker - Docker nginx 502 GET/Post 请求的网关错误,但不是 websocket
- java - HikariPool-1 - 驱动程序不支持获取/设置连接的网络超时。(com.mysql.jdbc.JDBC4Connection.getNetworkTimeout()I)
- c++ - Problem with understanding a vector initialization
- serialization - 运行 Streams 应用程序时 Serdes 中的序列化错误
- c++ - 模板参数绑定
- c++ - "如何显示 cout<<"Fever"; 因为我不熟悉字符串数组并且它也卡在选择部分?
- android - Google 地方信息自动填充返回 PLACES_API_ACCESS_NOT_CONFIGURED
- wordpress - Shortcode exclude category
- drupal - 将一种内容类型中的值自动填充到另一种内容类型