how to convert a double matrix image to a unit8 matrix ?

Hi everybody :) I have an image ,a matrix with values between -0.0934 and 0 and i want to convert it to unit8 matrix with values between 0 ans 255.

 Accepted Answer

A=imread('yourimage')
B=uint8(A)
EDIT
d=linspace(min(im(:)),max(im(:)),256)
im1=uint8(arrayfun(@(x) find(abs(d(:)-x)==min(abs(d(:)-x))),im))

4 Comments

I already have read the image then preprocee it and it's saved as a matrix all the pixels are negatif or null .whan i directly perform the unit8 function,the result is black image . i have also tried this code : u=abs(z); u=u*255; imshow(u) the result is the inverse graysclae of the preprocessed one.i also tried this: u=1-abs(z); u=u*255; imshow(u) I obtained a white image
It's not unit8 function it's uint8
yes i tried uint8 it was just an error
Try this
%If im is your image
d=linspace(min(im(:)),max(im(:)),256)
im1=uint8(arrayfun(@(x) find(abs(d(:)-x)==min(abs(d(:)-x))),im))

Sign in to comment.

More Answers (1)

You can use the mat2gray() function:
image8Bit = uint8(255 * mat2gray(floatingPointImage));
It will scale your values to the range 0-255.

10 Comments

ohhh thank you lot it s exactly what i need :))))))
Sir Could please explain a little bit about the code that you have written for conversion of double into Uint8.
mat2gray() maps the lowest value of the array to 0, the highest value of the array to 1, and everything in between linearly between 0 and 1. The output is a n array of the same dimensions as floatingPointImage.
If you then multiply this by 255, now the output matrix will go from 0 to 255, but it will be double, and some numbers may have fractional parts.
So, to make it a pure integer, I cast it to uint8 with the uint8() function. If you had wanted a 16 bit image (instead of an 8 bit image), you'd have used 65535 and uint16().
Sir ImageAnalyst thank you very much for your contribution to this comunity. Everytime I find your answer I know it's gonna be on spot.
Sorry but didn't you ment 65536 for int16?
Can I have one additional question - I'm doing an image compression and every time I do some mathematical operation to a uint16 matrix, it automatically convets it to a double with floating point numbers. Than things like RLC stoppes working.
Harry, yes it should have been 65,635 instead of 16525. Not sure why I typed that but I fixed it - thanks for catching it. It would not be 65536 because even though there are that many values, they start at 0 and end at 65535.
I'd have to see your code to determine why casting it to 16 bit produces a double. Both im2uint16() and uint16() produce uint16 variables, not doubles.
Thank you, it was perfect for me!!

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!