How to convert image from double to uint8 in matlab?

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

  • I=uint8(I)
  • I=im2uint8(I) .

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

+5
source share
1 answer

The im2uint8 function assumes that your double image is scaled to the range [0,1] . If your image has values โ€‹โ€‹greater than 1 or less than 0 , these values โ€‹โ€‹will be cropped. 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); 
+6
source

All Articles