Save an image from a matrix

209 ビュー (過去 30 日間)
Federico Fioretti
Federico Fioretti 2022 年 1 月 4 日
回答済み: Image Analyst 2022 年 1 月 9 日
Hi. I have a matrix of data type double in matlab. I have to save this matrix as an image in .jpg or .png format, but when i try to save it, the image saved is completely white. I would like the saved image to have a specific colormap as a function of the values of the starting matrix. How can i do? thanks in advance
  1 件のコメント
Federico Fioretti
Federico Fioretti 2022 年 1 月 4 日
編集済み: Federico Fioretti 2022 年 1 月 4 日
I forgot to say that the starting matrix is made of noninteger values.

サインインしてコメントする。

採用された回答

Turlough Hughes
Turlough Hughes 2022 年 1 月 4 日
Here's one way - convert to an 8 bit array with values between 0 and 255 and then save:
I2 = 255*(I - min(I(:))) ./ (max(I(:)) - min(I(:))); %scale values between 0 and 255
I2 = cast(I2,'uint8');
imwrite(I2,'myImage.png')
  1 件のコメント
Federico Fioretti
Federico Fioretti 2022 年 1 月 4 日
編集済み: Federico Fioretti 2022 年 1 月 4 日
Thank you. This is a progress. But i would the pixels of the saved image to have the same values of the elements of the starting matrix. Is this possible?
Furthermore, i would know how can I change the saved image with a different color palette from the grayscale one.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 1 月 9 日
To save a floating point image as a TIFF format file:
% Create floating point image.
rgbImage = rand (10, 20, 3);
% Image must be single precision.
rgbImage = single(rgbImage);
% Display it.
imshow(rgbImage, 'InitialMagnification', 1000)
axis('on', 'image');
% Create tiff object.
fileName = '_floatingPointImage.tif';
tiffObject = Tiff(fileName, 'w')
% Set tags.
tagstruct.ImageLength = size(rgbImage,1);
tagstruct.ImageWidth = size(rgbImage,2);
tagstruct.Compression = Tiff.Compression.None;
tagstruct.SampleFormat = Tiff.SampleFormat.IEEEFP;
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
tagstruct.BitsPerSample = 32;
tagstruct.SamplesPerPixel = size(rgbImage,3);
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
tiffObject.setTag(tagstruct);
% Write the array to disk.
tiffObject.write(rgbImage);
tiffObject.close;
% Recall image.
m2 = imread(fileName)
% Check that it's the same as what we wrote out.
maxDiff = max(max(m2-rgbImage)) % Should be zero.

カテゴリ

Help Center および File ExchangeImport, Export, and Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by