1

我正在使用 matlab 对图像进行一些计算,我的第一印象是从 jpeg 文件读取后存储在 matlab 矩阵中的值是经过伽马校正的。因此,我应该编写以下代码:

im = (double((imread('Myimage.jpg')))/255).^2.2;

为了得到一个具有线性空间值的矩阵。但是,我尝试了以下方法:生成线性渐变:

for x=1:256
gradient(:,x) = ones(128,1) * (x-1)/255;
end

然后我将其写入 jpeg 文件并再次读取:

imwrite(gradient, 'gradient.jpg', 'Quality', 100);
gradient_jpg = double(imread('gradient.jpg'))/255;

现在我希望 gradient 和 gradient_jpeg 会有所不同,因为后者在编写为 jpg 文件时进行了伽马校正,而另一个则没有。事实证明这两个矩阵是相同的。而我这是我不明白的地方。在之前的测试中,我尝试从 matlab 和 HDRShop 打开相同的 jpg 图像。两个图像在屏幕上看起来相同,但是当我查找图像中的值时,它们不是一样。matlab中的值恰好是我在HDRShop中以2.2的幂得到的值(大约)。所以我的问题是......当matlab读取jpeg文件时是否将值存储在线性空间中,或者我有专门应用伽马项 (.^2.2) 以获得线性值?

提前致谢

4

2 回答 2

0

This is a good question. I'm here for the same reason.

In the docs for imread, I see no options for gamma correction, and it's silent on the whole issue.

I get the same values in the matrix that I read off the screen (from imshow, using a color checker utility). And the values I see on screen are very close (but not exactly the same) to what I see in other image viewing apps.

So I'm not sure this is an answer, but there's some more info.

于 2015-07-15T16:37:12.660 回答
0

实际上,MATLAB 提供了rgb2linlin2rgb函数来进行伽马解压缩/压缩。在下面的例子中,当直接写渐变,并在下面添加一个 0/​​1 的棋盘格时,可以很容易地看到 50% 的灰度(也就是棋盘格值)不对应渐变中的 0.5:

在此处输入图像描述

但是如果你在写之前应用lin2rgb,这就是你得到的,50% gray现在是中间值:

在此处输入图像描述

这是MATLAB代码:

gradient = zeros(128,256);
for x=1:256
    gradient(1:64,x) = ones(64,1) * (x-1)/255;
    for y = 64:128
        gradient(y,x) = mod(x+y,2);
    end
end
imwrite(gradient, 'gradient.png');
gradient_g = lin2rgb(gradient);
imwrite(gradient_g, 'gradient_g.png');
于 2020-07-06T17:08:26.977 回答