Ho to write 2D double arrays to image files

32 次查看(过去 30 天)
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))

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by