gpt4 book ai didi

c# - RGB/XYZ和XYZ-LAB色彩空间转换算法

转载 作者:行者123 更新时间:2023-12-04 10:59:03 24 4
gpt4 key购买 nike

我试图将 RGB 色彩空间转换为 CIE-LAB 色彩空间。但是有一个错误。

  • 输入 RGB 值 = (50,125,50)
  • 收到结果 LAB vaues = (41,-38,34)

  • LAB 值 应该是 (46.41,-39.24,33.51)

    我从 http://colorizer.org/ 检查了这个值

    我在哪里做错了?

    如果您检查以下代码并回答我。我会很高兴的。谢谢。
     public static Vector4 RGBToLab(Vector4 color)
    {
    float[] xyz = new float[3];
    float[] lab = new float[3];
    float[] rgb = new float[] { color[0], color[1], color[2], color[3] };

    rgb[0] = color[0] / 255.0f;
    rgb[1] = color[1] / 255.0f;
    rgb[2] = color[2] / 255.0f;

    if (rgb[0] > .04045f)
    {
    rgb[0] = (float)Math.Pow((rgb[0] + .0055) / 1.055, 2.4);
    }
    else
    {
    rgb[0] = rgb[0] / 12.92f;
    }

    if (rgb[1] > .04045f)
    {
    rgb[1] = (float)Math.Pow((rgb[1] + .0055) / 1.055, 2.4);
    }
    else
    {
    rgb[1] = rgb[1] / 12.92f;
    }

    if (rgb[2] > .04045f)
    {
    rgb[2] = (float)Math.Pow((rgb[2] + .0055) / 1.055, 2.4);
    }
    else
    {
    rgb[2] = rgb[2] / 12.92f;
    }
    rgb[0] = rgb[0] * 100.0f;
    rgb[1] = rgb[1] * 100.0f;
    rgb[2] = rgb[2] * 100.0f;


    xyz[0] = ((rgb[0] * .412453f) + (rgb[1] * .357580f) + (rgb[2] * .180423f));
    xyz[1] = ((rgb[0] * .212671f) + (rgb[1] * .715160f) + (rgb[2] * .072169f));
    xyz[2] = ((rgb[0] * .019334f) + (rgb[1] * .119193f) + (rgb[2] * .950227f));


    xyz[0] = xyz[0] / 95.047f;
    xyz[1] = xyz[1] / 100.0f;
    xyz[2] = xyz[2] / 108.883f;

    if (xyz[0] > .008856f)
    {
    xyz[0] = (float)Math.Pow(xyz[0], (1.0 / 3.0));
    }
    else
    {
    xyz[0] = (xyz[0] * 7.787f) + (16.0f / 116.0f);
    }

    if (xyz[1] > .008856f)
    {
    xyz[1] = (float)Math.Pow(xyz[1], 1.0 / 3.0);
    }
    else
    {
    xyz[1] = (xyz[1] * 7.787f) + (16.0f / 116.0f);
    }

    if (xyz[2] > .008856f)
    {
    xyz[2] = (float)Math.Pow(xyz[2], 1.0 / 3.0);
    }
    else
    {
    xyz[2] = (xyz[2] * 7.787f) + (16.0f / 116.0f);
    }

    lab[0] = (116.0f * xyz[1]) - 16.0f;
    lab[1] = 500.0f * (xyz[0] - xyz[1]);
    lab[2] = 200.0f * (xyz[1] - xyz[2]);
    Debug.Log("L:" + (int)lab[0]);
    Debug.Log("A:" + (int)lab[1]);
    Debug.Log("B:" + (int)lab[2]);
    Debug.Log("W:" + (int)color[3]);

    return new Vector4(lab[0], lab[1], lab[2], color[3]);
    }

    最佳答案

    而不是 .0055 ,应该是 0.055 .

    逆 Gamma 运算是错误的:

    rgb[0] = (float)Math.Pow((rgb[0] + .0055) / 1.055, 2.4);

    应该:
    rgb[0] = (float)Math.Pow((rgb[0] + .055) / 1.055, 2.4);

    见: sRGB reverse gamma transformation

    关于c# - RGB/XYZ和XYZ-LAB色彩空间转换算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58952430/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com