3

你能解释一下matlabim2double函数的不一致行为吗?
基本上,这个函数作为输入给出一个 int 的矩阵 A,它对 0...1 范围内的值进行归一化:

例子:

I1 = reshape(uint8(linspace(1,255,9)),[3 3])
I2 = im2double(I1)

I1 =

    1   96  192
   33  128  223
   65  160  255


I2 =

    0.0039    0.3765    0.7529
    0.1294    0.5020    0.8745
    0.2549    0.6275    1.0000

但是现在如果我提供一个double矩阵:

I1 =

    0.1000    0.2000
    1.5000    2.2000

的结果im2double是相同的矩阵 I1(因此没有任何归一化)。我能理解这种不一致的行为吗?

4

2 回答 2

4

Type help im2double. The first few lines are

IM2DOUBLE takes an image as input, and returns an image of class double. If the input image is of class double, the output image is identical to it. If the input image is not double, IM2DOUBLE returns the equivalent image of class double, rescaling or offsetting the data as necessary.

So the behavior is intended. Indeed, when opening im2double.m in the editor, you'll see

function d = im2double(img, typestr)
...
if isa(img, 'double')
    d = img;
...

end

Whether it is intuitive, well, that's debatable :)

Anyway, there's a good reason for this behavior. With inputs of type int8 you know what the ceiling is (255). With that information you can re-scale the output (values/255). Similar for int16 (values/65535), etc.

However, when you're given a double, you don't have a realistic ceiling anymore. For most double values, values/realmax << realmin, so there's little sense in re-scaling.

You could argue that 255 would be a good default to scale to, with a warning saying that if 16-bit data is intended, you should give an extra argument or so. But well...that gets ugly and makes the function needlessly complicated. I for one can understand Mathworks' decision to keep the original in such events.

于 2013-01-21T15:51:16.687 回答
4

图像处理工具箱支持两种不同的图像表示。图像可以存储为整数 RGB 值(通常是 8 位 0..255 值)。或者它们可以存储为 0..1 范围内的实数。

im2double确保您拥有第二种类型的表示 - 如果您想将图像视为真实数组并对其执行各种数学运算。

因此,如果图像已经是类型double,则它的值在 0..1 范围内,无需执行任何操作。如果不是,则该函数将输入转换为实数并通过乘以 1/255 重新调整它。

于 2013-01-21T18:03:11.373 回答