Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert image from double to uint8 in matlab?

I have an image I which is of type double. I want to convert the image from double to uint8. I have tried using both:

  1. I=uint8(I)
  2. I=im2uint8(I).

When I use the imshow(I) command, I get only a black image and nothing else. What am I doing wrong?

like image 299
devraj Avatar asked Jun 10 '15 13:06

devraj


People also ask

What is uint8 and double in MATLAB?

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.

How do you write uint8 in MATLAB?

Y = uint8( X ) converts the values in X to type uint8 . Values outside the range [0,28-1] map to the nearest endpoint.

How convert uint8 image to logical in MATLAB?

J = im2uint8( I ) converts the grayscale, RGB, or binary image I to uint8 , rescaling or offsetting the data as necessary. If the input image is of class uint8 , then the output image is identical. If the input image is of class logical , then im2uint8 changes true-valued elements to 255.


1 Answers

The im2uint8 function assumes that your double image is scaled to the range [0,1]. If your image has values larger than 1 or smaller than 0, these values will be clipped. See the following example:

im2uint8([-1 0 0.5 1 2])
ans =
    0    0  128  255  255

The solution is to scale the input image to [0,1] by subtracting the minimum value and dividing by the total range:

I = (I - min(I(:))) / (max(I(:)) - min(I(:)));
I = im2uint8(I);
imshow(I);
like image 152
hbaderts Avatar answered Sep 22 '22 10:09

hbaderts