gpt4 book ai didi

c++ - sRGB <-> 线性RGB转换精度

转载 作者:行者123 更新时间:2023-12-02 10:03:25 38 4
gpt4 key购买 nike

我正在尝试了解图像/视频处理的基础知识,最近我了解到所有处理都应应用于图像的线性表示。因此,我为 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;
}
}

为了测试它,我尝试了这个:

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';
}
}
}

但似乎值越接近 0,结果就越不准确。输出是:

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

我哪里错了?

最佳答案

因为您是通过将线性浮点值转换为整数表示来对其进行量化,所以转换确实具有破坏性。

但是,发生这种情况应该不足为奇:这实际上是使用 sRGB 逆电光传递函数 (EOTF) 对数据进行编码时的意图。目的是通过 8 位信号传输数据,同时保持良好的感知均匀性,同时降低整体带宽。

我会引用 Charles Poynton 的 Gamma Faq和 Poynton, C., & Funt, B. (2014)。数字图像表示和显示中的感知均匀性。色彩研究与应用,39(1), 6–15。 https://doi.org/10.1002/col.21768

注意这里的“对话”应该是“转换”

关于c++ - sRGB <-> 线性RGB转换精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61430174/

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