how to convert a double matrix image to a unit8 matrix ?
Mostra commenti meno recenti
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.
Risposta accettata
Più risposte (1)
Image Analyst
il 8 Mar 2013
You can use the mat2gray() function:
image8Bit = uint8(255 * mat2gray(floatingPointImage));
It will scale your values to the range 0-255.
10 Commenti
Mariem Harmassi
il 8 Mar 2013
Image Analyst
il 8 Mar 2013
Mark it as Accepted then.
Nhat Nguyen Thanh Minh
il 29 Mag 2018
Thanks Image Analyst !
AKSHAY VARSHNEY
il 17 Giu 2018
Sir Could please explain a little bit about the code that you have written for conversion of double into Uint8.
Image Analyst
il 17 Giu 2018
Modificato: Image Analyst
il 18 Feb 2019
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().
harrynecek
il 22 Dic 2018
Modificato: harrynecek
il 22 Dic 2018
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.
Rafee Muhammad
il 18 Feb 2019
Thank you so much sir
Image Analyst
il 18 Feb 2019
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.
Juan Manuel Miguel
il 6 Ago 2020
Thank you, it was perfect for me!!
Bruno Aravena Pérez
il 29 Apr 2022
Thanks!
Categorie
Scopri di più su Convert Image Type in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!