フィルターのクリア

Ho to write 2D double arrays to image files

19 ビュー (過去 30 日間)
Yong
Yong 2022 年 6 月 12 日
コメント済み: Yong 2022 年 6 月 13 日
Hello,
I have a set of 2D arrays of double values. The size of the arrays are 129-by-129-by-1. I would like to write each of the 2D arrays into an image. However, to my understanding, imwrite(X, 'myImgFile.JPEG or PNG or TIFF) will scale the values in X and write them with 8-bit values into the image files. Using 8-bit type losses the precision in my data and is not appropriate for my problem. But I do need to convert array X into images for the rest of my codes (non-Matlab).
Is there any way to write 2D array X into an image with double precision?
Many thanks,
Yong

採用された回答

Image Analyst
Image Analyst 2022 年 6 月 12 日
編集済み: Image Analyst 2022 年 6 月 12 日
You can either save the image as a .mat file if you want to save the precision as double, or you can save it as a floating point TIF image (but you have to convert it to single precision) like this:
% 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))
  1 件のコメント
Yong
Yong 2022 年 6 月 13 日
Thank you! I have verified that your code works correctly.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImage Data についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by