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

조회 수: 9 (최근 30일)
marjan ss
marjan ss 2017년 10월 30일
댓글: DGM 2022년 12월 13일
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.

답변 (1개)

Image Analyst
Image Analyst 2017년 10월 30일
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
Ayse
Ayse 2022년 12월 13일
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 2022년 12월 13일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by