Matlab im2double inconsistency normalization?
Can you explain the inconsistent behavior of the matlab function im2double
?
Basically, this function is given as an input matrix A of int that normalizes 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 double matrix :
I1 = 0.1000 0.2000 1.5000 2.2000
The result im2double
is the same I1 matrix (therefore without any normalization). Can I understand this inconsistent behavior?
source to share
Enter help im2double
. First few lines
IM2DOUBLE takes an image as input and returns an image of the double class. If the input image is of class double, the output image is identical to it. If the input image is not doubled, IM2DOUBLE returns an equivalent class double image, scaling or offsetting the data as needed.
So the behavior is intended. Indeed, when you open im2double.m
in the editor, you will see
function d = im2double(img, typestr)
...
if isa(img, 'double')
d = img;
...
end
Is it intuitive, well, it's debatable :)
If anything, there is a good reason for this behavior. When you enter a type, int8
you know what ceiling ( 255
) is. With this information you can change the scale of the output ( values/255
). Likewise for int16 ( values/65535
) etc.
However, when given to you double
, you no longer have a realistic ceiling. For most double
values values/realmax << realmin
, there is therefore little point in re-scaling.
You can argue that it 255
would be a good default for scaling, with a warning that if 16-bit data is intended, you must give an extra argument or so. But ok ... it gets ugly and makes the function uselessly complex. I can understand Mathworks' solution to keep the original in events like this.
source to share
The imaging toolbar supports two different views of images. Images can be stored as RGB integer values ββ(usually 8 bit 0..255 values). Or they can be stored as real numbers in the range 0..1.
im2double
ensures that you have a second type of representation - if you want to treat the image as a real array and perform various math operations on it.
So, if the image already has a type double
, it has its values ββin the range 0..1 and nothing needs to be done. If not, then the function converts the input to a real one and rescan it by multiplying by 1/255.
source to share