【问题标题】:RGB / XYZ and XYZ-LAB color space conversion algorithmRGB/XYZ和XYZ-LAB色彩空间转换算法
【发布时间】:2020-03-16 01:23:21
【问题描述】:

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

  • 输入 RGB 值 = (50,125,50)
  • 收到结果 LAB 值 = (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]);
        }

【问题讨论】:

    标签: c# rgb cielab xyz


    【解决方案1】:

    应该是0.055,而不是.0055

    逆伽玛运算错误:

    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

    【讨论】: