首页 > 解决方案 > 使用颜色矩阵更改与 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# 亮度(色矩阵因子=0.2)[+30] 在此处输入图像描述

Photoshop 亮度 [+30] 在此处输入图像描述

我们如何从 C# 中获得与 Photoshop 相同的结果。

标签: c#.netphotoshopbrightnesscolormatrix

解决方案


推荐阅读