简体   繁体   English

sRGB <-> 线性 RGB 转换精度

[英]sRGB <-> linear RGB conversion precision

I am trying to understand the basics of image/video processing and recently I learned that all the processing should be applied to a linear representation of an image.我正在尝试了解图像/视频处理的基础知识,最近我了解到所有处理都应该应用于图像的线性表示。 So, I wrote two functions for sRGB -> RGB and RGB -> sRGB:因此,我为 sRGB -> RGB 和 RGB -> sRGB 编写了两个函数:

void sRGB2lRGB(std::vector<unsigned char>& in, std::vector<unsigned char>& out)
{
    std::vector<double> temp(in.begin(), in.end());

    for (int i = 0; i < temp.size(); i++)
    {
        temp[i] /= 255.0;
    }

    for (int i = 0; i < temp.size(); i++)
    {
        if (temp[i] <= 0.04045)
        {
            temp[i] /= 12.92;
        }
        else
        {
            temp[i] = std::pow((temp[i] + 0.055) / 1.055, 2.4);
        }
    }

    for (int i = 0; i < temp.size(); i++)
    {
        out[i] = temp[i] * (int)255 + 0.5;
    }
}

void lRGB2sRGB(std::vector<unsigned char>& in, std::vector<unsigned char>& out)
{
    std::vector<double> temp(in.begin(), in.end());

    for (int i = 0; i < temp.size(); i++)
    {
        temp[i] /= 255.0;
    }

    for (int i = 0; i < temp.size(); i++)
    {
        if (temp[i] <= 0.0031308)
        {
            temp[i] *= 12.92;
        }
        else
        {
            temp[i] = 1.055 * std::pow(temp[i], 1.0 / 2.4) - 0.055;
        }
    }

    for (int i = 0; i < temp.size(); i++)
    {
        out[i] = temp[i] * (int)255 + 0.5;
    }
}

To test it I tried this:为了测试它,我尝试了这个:

int main()
{
    std::vector<unsigned char> in(255);
    std::vector<unsigned char> out(255);
    std::vector<unsigned char> out2(255);

    for (int i = 0; i < 255; i++)
    {
        in[i] = i;
    }

    sRGB2lRGB(in, out);
    lRGB2sRGB(out, out2);

    for (int i = 0; i < 255; i++)
    {
        if (out2[i] != i)
        {
            std::cout << "was: " << (int)in[i] << ", now: " << (int)out2[i] << '\n';
        }
    }
}

But it appeared that the closer the value to 0, the more inaccurate the result would be.但似乎值越接近 0,结果就越不准确。 The output is: output 是:

was: 1, now: 0
was: 2, now: 0
was: 3, now: 0
was: 4, now: 0
was: 5, now: 0
was: 6, now: 0
was: 7, now: 13
was: 8, now: 13
was: 9, now: 13
was: 10, now: 13
was: 11, now: 13
was: 12, now: 13
was: 14, now: 13
was: 15, now: 13
was: 16, now: 13
was: 17, now: 13
was: 18, now: 22
was: 19, now: 22
was: 20, now: 22
was: 21, now: 22
was: 23, now: 22
was: 24, now: 22
was: 25, now: 22
was: 26, now: 28
was: 27, now: 28
was: 29, now: 28
was: 30, now: 28
was: 31, now: 28
was: 32, now: 34
was: 33, now: 34
was: 35, now: 34
was: 36, now: 34
was: 37, now: 38
was: 39, now: 38
was: 40, now: 38
was: 41, now: 42
was: 43, now: 42
was: 44, now: 42
was: 45, now: 46
was: 47, now: 46
was: 48, now: 50
was: 49, now: 50
was: 51, now: 50
was: 52, now: 53
was: 54, now: 53
was: 55, now: 56
was: 57, now: 56
was: 58, now: 59
was: 60, now: 61
was: 62, now: 61
was: 63, now: 64
was: 65, now: 64
was: 67, now: 66
was: 68, now: 69
was: 70, now: 71
was: 72, now: 73
was: 74, now: 73
was: 76, now: 75
was: 78, now: 77
was: 80, now: 79
was: 82, now: 83
was: 84, now: 85
was: 87, now: 86
was: 89, now: 88
was: 91, now: 92
was: 94, now: 95
was: 97, now: 96
was: 100, now: 99
was: 103, now: 104
was: 107, now: 106
was: 111, now: 112
was: 116, now: 117
was: 123, now: 124

Where am I wrong?我哪里错了?

Because you are quantizing the linear floating-point values by casting them to integer representation, the conversion will indeed be destructive.因为您通过将线性浮点值转换为 integer 表示来量化线性浮点值,所以转换确实具有破坏性。

However, there should be no surprise that this occurs: it is actually the intent when encoding data with the sRGB inverse electro-optical transfer function (EOTF).但是,发生这种情况应该不足为奇:这实际上是使用 sRGB 逆电光传输 function (EOTF) 对数据进行编码时的意图。 The purpose is to carry data over 8-bit signals while maintaining a good perceptual uniformity with an overall reduced bandwidth.其目的是在 8 位信号上传输数据,同时保持良好的感知均匀性,同时降低整体带宽。

I would refer to Charles Poynton's Gamma Faq and Poynton, C., & Funt, B. (2014).我会参考 Charles Poynton 的Gamma Faq和 Poynton, C., & Funt, B. (2014)。 Perceptual uniformity in digital image representation and display.数字图像表示和显示中的感知均匀性。 Color Research and Application, 39(1), 6–15.颜色研究与应用,39(1),6-15。 https://doi.org/10.1002/col.21768 https://doi.org/10.1002/col.21768

Note that "Conversation" should be "conversion" here.注意这里的“对话”应该是“转化”

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

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