"double" vs. "uint8" input using "imshow" function

159 visualizzazioni (ultimi 30 giorni)
When using the command, "imshow(image)", I get an error when "image" has type "double". If I convert "image" to type "uint8", then "imshow" produces the image. Why is there an error with type "double"?
  4 Commenti
DGM
DGM il 21 Mag 2022
The short answer is "neither"
It's certainly arguable that 210 is not white. You could say it's a light gray maybe. Likewise, 20 is not black, but maybe you can say it's dark gray. It all depends on how specific "black" and "white" mean in context. The following are two small images with the specified gray levels shown above solid black and solid white, respectively.
So maybe you can say that all depends on how picky you want to be in calling things "black" or "white". There's one other thing to note. Let's say we do literally what's described. Take a pixel of each color and display them with imshow(), or image(). What do we get?
% an image with two pixels (plus a couple in between)
pq = uint8([20 80 150 210]);
% use imshow() with defaults
imshow(pq);
% do the same thing, but use implicit 'DisplayRange' option
figure
imshow(pq,[]);
% use image() instead
figure
image(pq);
So depending on what display tools you use, the options you specify, and the colormap that's in use, you might get shades of gray, or you might actually get black and white, or even different colors. The tiny 4-pixel image only has one channel (grayscale). It's important to note how these display tools can scale and apply colormaps (including a gray colormap) to these images.

Accedi per commentare.

Risposta accettata

Denis Vreshtazi
Denis Vreshtazi il 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.
  4 Commenti
Raka Mukherjee
Raka Mukherjee il 27 Gen 2020
Thank you so much for your answer. I was converting an unit16 image into unit8 image but the new image does not have 256 unique values, it only has 4 to 5 unique values , so when I want to deal with the colour aspect of the image how can it be dealt with? I want to deal with Red Green and Blue bands seperately which should have 0-255 values.
Walter Roberson
Walter Roberson il 27 Gen 2020
rgb2ind to get a color number. Possibly followed by ind2rgb to put a different coloring on.

Accedi per commentare.

Più risposte (9)

Junaid
Junaid il 1 Dic 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);
  4 Commenti
Ali Erdengiz
Ali Erdengiz il 29 Ott 2017
I have the same problem but converting uint8 to double changes its value. I am reading an RGB image in uint8 format, but I need to calculate the variance of the RGB pixels. The var function won't let me do it. I convert the pixels in double, but their values are totally off. Because I think on 8 bits there is one bit for the sign, so the new value is between +/- 127. That's not what I want. I am looking for pixel values around 140 - 150, but they are all gone. If for instance I have a pixel value of 255, how do I get 255 in double?

Accedi per commentare.


Vinai Datta Thatiparthi
Vinai Datta Thatiparthi il 27 Mar 2019
Modificato: Vinai Datta Thatiparthi il 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');
imshow(im);
imshow(double(im));
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 :)
  1 Commento
Walter Roberson
Walter Roberson il 27 Mar 2019
Modificato: Walter Roberson il 27 Mar 2019
TIF images and animated GIF can be 4D, as can be images that assign a color to each location in a voxel array. DICOM can be 4D, especially for ultrasound.

Accedi per commentare.


Mahas Arbasia
Mahas Arbasia il 17 Apr 2020
Modificato: Mahas Arbasia il 17 Apr 2020
i found some useful information, might help

Alireza Mounesisohi
Alireza Mounesisohi il 24 Giu 2016
All you need to do is, adding this code to it:
A =im2double(B);
Where B is your unit8 matrix and A is going to be your double matrix.
Good luck.

HONG CHENG
HONG CHENG il 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
  2 Commenti
ALI JUNAID
ALI JUNAID il 1 Giu 2021
bs(i1)=double(bs(i1));
please tell correct code
Walter Roberson
Walter Roberson il 1 Giu 2021
The data type of a numeric array is the same for the whole array. You cannot have a numeric array in which some elements are double precision but others are not double precision.
You should either write to a new array, like
dbs(i1) = double(bs(i1));
or you should convert the whole array, like
dbs = double(bs);

Accedi per commentare.


Pranit Patil
Pranit Patil il 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.
  1 Commento
Walter Roberson
Walter Roberson il 8 Mar 2018
You cannot pass a file name to imhist: you need to imread() the file into an array and pass the array to imhist.

Accedi per commentare.


MathWorks Support Team
MathWorks Support Team il 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:
imshow('hotcoffee.tif')
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:
imshow('hotcoffee.tif','DisplayRange', [])

mohammad suhaan dar
mohammad suhaan dar il 21 Set 2021
Modificato: Walter Roberson il 24 Gen 2022
Suppose p is a uint8 pixel with value 20 and q is a uint8 pixel with value 210. Which of the following would display as a black pixel?
im2double(p-40)
im2double(p) - 40
p - 40
q + 40
im2double(q-40)
im2double(q) - 40
  5 Commenti
Walter Roberson
Walter Roberson il 25 Mar 2022
In MATLAB, there are only a few possibilities
  1. You have a 3D array that is data type integer, and the third dimension is length 3, and for a given location, all three of the components are exactly the minimum value possible for that integer data type. In this case, the pixel will be black.
  2. You have a 3D array that is data type single() or double(), and the third dimension is length 3, and for any given location, all three of the components are non-positive; in this case, the pixel will be black
  3. You have a 3D array that is data type integer, and the third dimension is length 3, and for any given location, all three of the components are exactly the same as each other and are not zero. In this case, the pixel will be white.
  4. You have a 3D array that is data type single() or double(), and the third dimension is length 3, and for any given location, if you take min(components,1) the results will be non-zero and equal to each other to within one part in 65536; in this case, the pixel will be white. For example, [pi, sqrt(2), 1], min() that against 1 clips to [1, 1, 1] which are all equal; the result would be white
  5. You have a 3D array with third dimension is length 3, but none of the above 4 cases apply; in this case the pixel will be neither black nor white (it will be color)
  6. You have a scalar, vector, or 2D array that is either integer or floating point. In this case, the color that the pixel will appear as will depend upon the axes CLim property (which can be controlled by caxis()), and upon the axes or figure Colormap property (which can be controlled by colormap()). In this case, the color might turn out to be anything that can be expressed through the color map.
The 6 cases you list all fall into that last case, scalar, vector or 2D array, so you do not have enough information to know whether they would show up as black or white or something else completely. For example,
p = uint8(20);
image(p + 50);
colormap(pink)
This is neither black nor white. With a different colormap it could certainly turn out black, or it could turn out white, or it could be blue or it could be violet sky, or it could be purple or it could be anything you want.

Accedi per commentare.


Mahe Jabeen
Mahe Jabeen il 27 Lug 2022
Modificato: Mahe Jabeen il 27 Lug 2022
I want to know what is wrong with my answer. Can you explain? @Walter Roberson
  1 Commento
DGM
DGM il 27 Lug 2022
Modificato: DGM il 27 Lug 2022
Oh I never knew the question was multiple choice when it was asked earlier. I suppose the last choice is also true. Any unit-scale (i.e. in the range [0 1]) floating-point image will be rendered as white if you add 50 to it. It woudn't likely be productive to do so, but it would be white.
I can see why someone might casually overlook that option, based on how nonsensical it appears in the first place.

Accedi per commentare.

Categorie

Scopri di più su Images in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by