Effacer les filtres
Effacer les filtres

Ho to write 2D double arrays to image files

21 vues (au cours des 30 derniers jours)
Yong le 12 Juin 2022
Commenté : Yong le 13 Juin 2022
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,

Réponse acceptée

Image Analyst
Image Analyst le 12 Juin 2022
Modifié(e) : Image Analyst le 12 Juin 2022
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;
% Write the array to disk.
% Recall image.
m2 = imread(fileName)
% Check that it's the same as what we wrote out.
maxDiff = max(max(m2-rgbImage))
  1 commentaire
Yong le 13 Juin 2022
Thank you! I have verified that your code works correctly.

Connectez-vous pour commenter.

Plus de réponses (0)




Community Treasure Hunt

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

Start Hunting!

Translated by