RGB / XYZ and XYZ-LAB color space conversion algorithm

試著忘記壹切 提交于 2020-01-24 23:57:08

问题


I tried to convert RGB color space to CIE-LAB color space. But there is a mistake.

  • input RGB values = (50,125,50)
  • received result LAB vaues = (41,-38,34)

LAB values should be (46.41,-39.24,33.51)

I checked this values from 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]);
        }


回答1:


Instead of .0055, it should be 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



来源:https://stackoverflow.com/questions/58952430/rgb-xyz-and-xyz-lab-color-space-conversion-algorithm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!