imshow using double or uint8 RGB values - confusing

15 views (last 30 days)
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.
  1 Comment
Rik
Rik on 8 May 2019
It is a good write-up of your thoughts, but I don't really see the question.
It looks like you already found the answer (which is also in the documentation): for double images, imshow will scale between 0 and 1, and for uint8, imshow will scale between 0 and 255.
This is not only true for RGB images, but also of gray scale images.
So what is your question? What confuses you still? A lot of higher level functions (like imshow and plot) provide an easy way to create the lower level objects (like image and line). It is therefore not very strange that such a fundamental function can handle multiple data types.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 8 May 2019
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.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!