imshow using double or uint8 RGB values - confusing
16 views (last 30 days)
Show older comments
Salad Box
on 8 May 2019
Answered: Walter Roberson
on 8 May 2019
Hi,
I'm a bit confused about 'imshow'
My guessing is that imshow can be used with 2 types of RGB data.
- RGB values are between [0 1] and they have to be double;
- 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).
However, when I use
figure
imshow(RGB) % knowing that RGB are between [0 1]
I could still show the image
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
I never realized that imshow can deal with 'double' data... just a little confused.
1 Comment
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.
Accepted Answer
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'
See https://www.mathworks.com/help/matlab/ref/matlab.graphics.primitive.image-properties.html for the complete definitions of direct and scaled.
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.
0 Comments
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!