"double" vs. "uint8" input using "imshow" function
757 views (last 30 days)
Denis Vreshtazi on 11 Nov 2018
Converting Image Storage Classes
You can convert uint8 and uint16 data to double precision using the MATLAB function, double. However, converting between storage classes changes the way MATLAB and the toolbox interpret the image data. If you want the resulting array to be interpreted properly as image data, you need to rescale or offset the data when you convert it.
For easier conversion of storage classes, use one of these toolbox functions: im2double, im2uint8, and im2uint16. These functions automatically handle the rescaling and offsetting of the original data. For example, this command converts a double-precision RGB image with data in the range [0,1] to a uint8 RGB image with data in the range [0,255].
RGB2 = im2uint8(RGB1);
Losing Information in Conversions
When you convert to a class that uses fewer bits to represent numbers, you generally lose some of the information in your image. For example, a uint16 intensity image is capable of storing up to 65,536 distinct shades of gray, but a uint8 intensity image can store only 256 distinct shades of gray. When you convert a uint16 intensity image to a uint8 intensity image, im2uint8 quantizes the gray shades in the original image. In other words, all values from 0 to 127 in the original image become 0 in the uint8 image, values from 128 to 385 all become 1, and so on. This loss of information is often not a problem, however, since 256 still exceeds the number of shades of gray that your eye is likely to discern.
Converting Indexed Images
It is not always possible to convert an indexed image from one storage class to another. In an indexed image, the image matrix contains only indices into a colormap, rather than the color data itself, so no quantization of the color data is possible during the conversion.
For example, a uint16 or double indexed image with 300 colors cannot be converted to uint8, because uint8 arrays have only 256 distinct values. If you want to perform this conversion, you must first reduce the number of the colors in the image using the imapprox function. This function performs the quantization on the colors in the colormap, to reduce the number of distinct colors in the image. See Reducing Colors in an Indexed Image for more information.
More Answers (9)
Junaid on 1 Dec 2011
uint8 is used unsigned 8 bit integer. And that is the range of pixel. We can't have pixel value more than 2^8 -1. Therefore, for images uint8 type is used. Whereas double is used to handle very big numbers. There are many functions they only take double as input to ovoid memory out of range.
It is easy to convert double to uint8 or otherway.
let say I have matrix of uin8 A.
then to convert it to double you just have to do this
A = double(A);
or to convert it back to uint8.
A = uint8(A);
Vinai Datta Thatiparthi on 27 Mar 2019
Edited: Vinai Datta Thatiparthi on 28 Feb 2020
Hi! To add to the thread,
All images are stored and represented as a matrix (2-D or 3-D). the default setting for storing matrix or array information in Matlab is double precision. It uses 64 bits to store any number. However, for the internsity of a pixel of images, the range is generally 0-255. 8 bits are suffecient to describe this information. As a means to save memory & for more effective representation, we use uint8, uint16, uint32, uint64 and so on...
im = imread('random_picture.jpg');
You can better understand the difference in Uint8 & double through this example.
Also, you can typecast easily from one class to another using terms as commands directly. For greater detail, we can have the intensity of pixels ranging up till (2^16 -1), (2^32 -1) and so on...
As an extension, the 8 bits here represent the number of bits required to store the image information of one plane. For RGB images, Bit depth, which is the total number of bits required to represent all of the image information, will then become 24. And for a grayscale image, the Bit depth would simply be 8, since it has only one plane.
Hope this helps :)
Alireza Mounesisohi on 24 Jun 2016
All you need to do is, adding this code to it:
Where B is your unit8 matrix and A is going to be your double matrix.
HONG CHENG on 26 Apr 2017
I think maybe you encounter them when you read an image. if it is true, when you read an image use
A = imread(“picture.png”)
function,then you will get a Variable A like this a*b*3 unit8,,whose value vary from 0 to 255 but if you convert this Variable A use
B = im2double(A)
then you will get some kind Variable B like a*b*3 double,whose value vary from 0 to 1
and after you use the function
grey = rgb2gray(I2)
you will get a Variable like a*b double
Pranit Patil on 8 Mar 2018
I read an rgb image i wanted the histogram of the image but when i gave the command imhist('xyz.jpg') it gave me error by plotting just the x -y axis and not the graph in it.It was totally white.Please help me and the image is already in uint8.
MathWorks Support Team on 27 Feb 2020
An all-white image display occurs when image data is not in the default image display range. For images of class "double", the range is [0,1]. To work around this, use the ‘DisplayRange’ parameter with "imshow". For example, consider the following command where the input argument is a thermal camera image from the Image Processing Toolbox:
If you examine the pixel values in the image, they are out of the default range, such as 23.6268, and therefore the image display is all white. If you specify the display range using empty brackets , then "imshow" calculates the display range and displays the image correctly: