필터 지우기
필터 지우기

Available to write/save/store a half precision as 16 bit TIFF? otherwise 32 bit TIFF?

조회 수: 11 (최근 30일)
I found that half precision can express floating point number in 16 bit. Can I write/save/store a half precision as 16 bit TIFF? otherwise 32 bit TIFF? In my trials, the TIFF writing with 16 bit allows int16 and uint16 only, and int32, uint32, single for the TIFF writing with 32 bit only.
This is related to question below:
clc; clear all; close all;
Img = imread('cameraman.tif');
ImgS = single(Img);
ImgN = ImgS/(max(ImgS,[],"all")+1);
figure; imagesc(ImgN); axis image; colormap('gray'); colorbar;
ImgH = half(ImgN);
t = Tiff('myimageHalf.tif','w');
tagstruct.ImageLength = size(ImgH,1);
tagstruct.ImageWidth = size(ImgH,2);
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
tagstruct.BitsPerSample = 16;
tagstruct.SamplesPerPixel = 1;
tagstruct.SampleFormat = 1;
tagstruct.SMaxSampleValue = double(max(ImgH,[],"all"));
tagstruct.SMinSampleValue = double(min(ImgH,[],"all"));
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
tagstruct.Software = 'MATLAB';
setTag(t,tagstruct)
write(t,ImgH);
close(t);

채택된 답변

DGM
DGM 2024년 5월 12일
편집: DGM 2024년 5월 12일
For half, the short answer is no. The long answer is no, but even if you could, it would probably be a huge hassle for no benefit.
As to the short answer, the underlying library does not support half-precision float. It's simply not an option.
Error using tifflib
If BitPerSample is 16, then the input image datatype must be int16 or uint16, not half.
As to the long answer, while MATLAB can write a double or single-precision TIFF with some effort, rest assured that almost no other program will be able to read it. The odds of being able to find something that can read a half-precision TIFF would be unlikely.
So if a float-class TIFF offers zero compatibility, does it make sense to use it? If the only program that ever needs to read the file is MATLAB, just use a .mat file. A .mat file will support half-precision data, and the file size is likely going to be comparable to what you could hope to have obtained using TIFF.
Could you do something silly by typecasting half-precision float data as uint16 and storing it in a TIFF? Yes. Again, nothing would be able to read it, but now you'd have the extra complication that half-floats are an opaque class. You can't use typecast() on them. You'd have to reinvent that wheel all on your own -- in both directions. If you ever forget what you did to write the malformed data to the file, good luck reading it again.
For single and double float TIFF, see this example, but bear in mind the limitations I just mentioned.
  댓글 수: 1
DGM
DGM 2024년 6월 6일
편집: DGM 2024년 6월 6일
I was thinking about half-floats again lately, and I decided to try to actually do this. Since we're going to typecast to a pedestrian uint16 class, we don't necessarily need to use Tiff() to handle the file. We could just use imwrite(). Either way, the hurdle is figuring out how to create the malformed file. Turns out that we don't really need to reinvent the wheel as much as I had thought, though we certainly can't use the conventional tools we might use for other numeric classes.
% a uint8 image
inpict = imread('peppers.png'); % uint8, RGB
% make it into a unit-scale half-float image that nothing will handle
inpict = half(im2double(inpict));
% output file name
filename = 'test.tiff';
% typecast the float16 data to uint16 and write it
fakeu16 = storedInteger(inpict);
imwrite(fakeu16,filename,'compression','deflate')
% read the image back and typecast it back to float16
newpict = imread(filename);
newpict = half.typecast(newpict);
% test that it's the same
isequal(inpict,newpict)
ans = logical
1
So it does work, but again, you're guaranteed that no other image viewer or editor application will read the file correctly, since the binary data isn't formatted as the numeric type implies. Although it's not terribly difficult to cram half-float data into a TIFF, it's still probably more sensible to just use a .mat file.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by