Can you explain the inconsisent behaviour of matlab im2double function?
Basically this function given as input a matrix A of int it normalizes the values in the range 0…1:
Example:
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
But now If i provide a matrix of double:
I1 =
0.1000 0.2000
1.5000 2.2000
The result of im2double is the same matrix I1 (so without any normalization). Can I understand this inconsisent behaviour?
Type
help im2double. The first few lines areSo the behavior is intended. Indeed, when opening
im2double.min the editor, you’ll seeWhether it is intuitive, well, that’s debatable 🙂
Anyway, there’s a good reason for this behavior. With inputs of type
int8you 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 mostdoublevalues,values/realmax << realmin, so there’s little sense in re-scaling.You could argue that
255would 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.