MATLAB: Imshow using double or uint8 RGB values – confusing

doubleImage Processing Toolboximshowuint8

Hi,
I'm a bit confused about 'imshow'
My guessing is that imshow can be used with 2 types of RGB data.
  1. RGB values are between [0 1] and they have to be double;
  2. RGB values are between [0 255] and they have to be uint8.
Since that:
I was processing a '.png' file which is an indexed image because when I read in the file using
RGB = imread(fn)
RGB is 400 x 650 uint8 rather than 400 x 650 x 3 uint8. So I know it is an indexed image.
So I read in the file again using code below to convert the indexed image to RGB image
[X,map] = imread(fn)
RGB = ind2rgb(X,map) % And here RGB is already 'double'!
The problem is that here actually all the RGB are ranging between [0 1] instead of [0 255] (This can be proved by ploting RGB in the 3D space).
untitled.jpg
However, when I use
figure
imshow(RGB) % knowing that RGB are between [0 1]
I could still show the image
untitled1.jpg
However, when I use
RGB = RGB*255 % to make RGB in the range of [0 255]
RGB = uint8(RGB)
figure
imshow(RGB)
I can also show the image
untitled1.jpg
I never realized that imshow can deal with 'double' data… just a little confused.

Best Answer

imshow() exists mostly to be "smart" about displaying images.
When imshow() is passed a 2D array, it invokes image() with CDataMapping 'scaled'. It does this regardless of whether the data is double or uint8 and regardless of the range of the data.
When imshow() is passed a 3D array, it invokes image() with CDataMapping 'direct'
The short summary is that 'direct' values are assumed to be colormap entry numbers usually in the range 1 to the number of colors (remember, this could be double!), and that 'scaled' is interprolated through the color limits to proportion of the color map.
imshow() is also smart about tossing in colormap(gray) when passed 2D data. The default for the underlying function image() would otherwise be to use the current colormap.
Related Question