Borrar filtros
Borrar filtros

Ho to write 2D double arrays to image files

16 visualizaciones (últimos 30 días)
Yong
Yong el 12 de Jun. de 2022
Comentada: Yong el 13 de Jun. de 2022
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

Respuesta aceptada

Image Analyst
Image Analyst el 12 de Jun. de 2022
Editada: Image Analyst el 12 de Jun. de 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;
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 comentario
Yong
Yong el 13 de Jun. de 2022
Thank you! I have verified that your code works correctly.

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by