How to save actual values in a matrix as an image in matlab?

9 visualizaciones (últimos 30 días)
marjan ss
marjan ss el 30 de Oct. de 2017
Comentada: DGM el 13 de Dic. de 2022
I have a matrix in matlab with 10 different values. All values in my array are integer from 1 to 10. I need to save this matrix as an image (image.jpg). However I want to have the same values ( integers from 1 to 10) in my image when I read the image into the matlab. When I use imwrite or save or saveas I create an image but when I read it into matlab, values are from 0-255 no form 1-10.

Respuestas (1)

Image Analyst
Image Analyst el 30 de Oct. de 2017
imwrite should work. It's not enough that they are integers, they must be uint8 integers, not double values that happen to have integer values. Try this:
imwrite(uint8(myData), 'my image.png');
  8 comentarios
Ayse
Ayse el 13 de Dic. de 2022
After implementing the suggestions, I am still getting a png fil that is completely black.
My code is:
M=randi([10,93],4,4);
YM=uint8(M);
imwrite(uint8(YM), 'YM.png')
I am trying to save the matrix ouput I see on matlab (like the one below) as a png image as it is.
33 23 77 76
55 91 21 90
90 90 45 65
91 50 86 12
I would appreciate any suggestions,
Thank you.
DGM
DGM el 13 de Dic. de 2022
It depends what the intent is. If the goal is merely to transport the array, then what you've written will work.
M = randi([10,93],4,4); % values are within [0 255] (good!)
YM = uint8(M); % values are unchanged, but class is changed to uint8
imwrite(YM,'YM.png') % write the image
YMrec = imread('YM.png'); % read the image
isequal(YM,YMrec) % arrays are identical
ans = logical
1
imshow(YM) % display the "image"
I don't see how you'd get a completely black image out of it. The redundant call to uint8() has no effect. If you're not familiar with working with data as images, it's worth understanding that the display (and writing) of image data rests on the assumption of data scale and class. If the data is presented as an integer class, then imshow() and imwrite() will assume that "black" and "white" are represented by the minimum and maximum values which can be represented by the class (e.g. 0-255 for uint8). If the data is presented as a floating-point class (e.g. M in the above example), then black is 0 and white is 1. If you had done
imwrite(M,'M.png')
Then M.png would be an all-white image, and the original values would be unrecoverable.

Iniciar sesión para comentar.

Categorías

Más información sobre Images en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by