简体   繁体   English

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

[英]RGB / XYZ and XYZ-LAB color space conversion algorithm

I tried to convert RGB color space to CIE-LAB color space.我试图将 RGB 颜色空间转换为 CIE-LAB 颜色空间。 But there is a mistake.但是有一个错误。

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

LAB values should be (46.41,-39.24,33.51) LAB 值应为 (46.41,-39.24,33.51)

I checked this values from http://colorizer.org/我从http://colorizer.org/检查了这个值

Where did I make a mistake?我在哪里做错了?

If you check the following code and answer me.如果您检查以下代码并回答我。 I will be glad.我会很高兴的。 Thanks.谢谢。

 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]);
        }

Instead of .0055 , it should be 0.055 .而不是.0055 ,它应该是0.055

The inverse gamma operation is wrong:逆伽玛运算是错误的:

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

Should be:应该:

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

See: sRGB reverse gamma transformation参见: sRGB 反向伽马变换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM