필터 지우기
필터 지우기

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 CenterFile Exchange에서 Image Data에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by