c# - 使用颜色矩阵更改与 Photoshop 相同的图像亮度
问题描述
在 Photoshop 中,我们可以在-150 到 150的范围内更改图像的亮度。
使用 PhotoshopCS4:图像 → 调整 → 亮度/对比度。
我们为特定图像 (A.jpg)将此级别设置为30并保存图像 (A_30Bright.jpg)
对 C# 应用程序使用相同的输入,我们已将值标准化。参考这个:https ://stats.stackexchange.com/questions/178626/how-to-normalize-data-between-1-and-1
我们的归一化函数如下所示
public static float GetNormalizedValue(float OldValue)
{
float OldMax = 150, OldMin = -150, NewMax = 255F, NewMin = -255F;
float OldRange = (OldMax - OldMin);
float NewRange = (NewMax - NewMin);
float NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin;
return NewValue;
}
下面是使用颜色矩阵的示例代码
public static double ChangeImageBrightness(string inputImagePath, float value)
{
imagePath = inputImagePath;
Bitmap _bmp = new Bitmap(inputImagePath);
float FinalValue = GetNormalizedValue(value);
FinalValue = FinalValue / 255.0f;
float[][] ptsArray ={
new float[] { 1.0f, 0, 0, 0, 0},
new float[] {0, 1.0f, 0, 0, 0},
new float[] {0, 0, 1.0f, 0, 0},
new float[] {0, 0, 0, 1.0f, 0},
new float[] { FinalValue, FinalValue, FinalValue, 0, 1}};
ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.ClearColorMatrix();
imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
Bitmap adjustedImage = new Bitmap(_bmp.Width, _bmp.Height);
Graphics g = Graphics.FromImage(adjustedImage);
g.DrawImage(_bmp, new Rectangle(0, 0, adjustedImage.Width, adjustedImage.Height), 0, 0, _bmp.Width, _bmp.Height, GraphicsUnit.Pixel, imageAttributes);
adjustedImage.Save(Path.GetFileName(imagePath)); }
PhotoShop 和 C# .Net 的结果图像不匹配。
C# 中的亮度包含更多的白色阴影。
例如 原始图像
我们如何从 C# 中获得与 Photoshop 相同的结果。
解决方案
推荐阅读
- ansible - 如何使用循环加速任务
- java - 如何使用 Java 打开以 .tif 图像文件形式给出的非常大的整个幻灯片图像?
- excel - If Statement for Error Checking is not working
- docusignapi - 有没有办法删除在发件人视图上删除/修改上传到 DocuSign 的文档的功能?
- linux - Linux - grep -e 在从命令调用 script.sh 时有效,但在从 crontab 调用 script.sh 时失败
- android - 预览隐藏的实时视频时获得绿色图像
- javascript - 使用类数组的更简单方法
- react-native - React Native:删除 Expo Cookie
- jquery - 在一行中将多个按钮插入页面
- c# - 如何在wpf中具有大层次结构的子控件和父窗口之间进行通信?