How can I save a 32 bit image (uint32) as a tiff?
24 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
I am currently working on a post processing HDR script which requires that I use a 32 bit data type. The images start at 14 bit, once they are imported I convert them to int32, and after I do my calculations I convert them back to uint32. After I create my HDR image the max pixel value is about 181,000 counts. I am currently using,
imwrite(FinalImage, 'HDR Result.tif')
which works for 16 bit images, but does not seem to work for 32 bit images. If you could give me any help or insight I would greatly appreciate it.
Thank you, Jason
1 Kommentar
Ashish Uthama
am 28 Jan. 2014
Could you say more on what you mean by 'does not seem to work'? Do you get an error message or does it write the data incorrectly?
Antworten (1)
DGM
am 6 Jun. 2024
Here's a simple demo that should demonstrate writing odd numeric classes to a TIFF. This supports 8,16, and 32 bit signed and unsigned integers, as well as single and double-precision float. Bear in mind that many things in IPT and base MATLAB may not properly deal with int8, int32, or uint32 images, so you're on your own.
% an uint8 image
inpict = imread('peppers.png'); % uint8, RGB
% inpict = im2gray(inpict); % if you want to play with signed-int types, RGB is not supported
% make it into a 32b integer class somehow
% normally, i'd just use MIMT imcast(), but let's just do this garbage
%inpict = imcast(inpict,'uint32'); % let's pretend we can't have nice things
inpict = uint32(inpict)*16843009; % uint8 to uint32
%inpict = int32(double(inpict)*16843009 - 2147483648); % uint8 to int32
% output file name
filename = 'test.tiff';
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% get to work
sz = size(inpict,1:3);
inclass = class(inpict);
t = Tiff(filename, 'w');
tagstruct.ImageLength = sz(1);
tagstruct.ImageWidth = sz(2);
tagstruct.Compression = Tiff.Compression.AdobeDeflate;
tagstruct.SamplesPerPixel = sz(3);
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
% I/RGB?
% i'm choosing to presume that 3ch inputs are RGB
if sz(3) == 1
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
elseif sz(3) == 3
tagstruct.Photometric = Tiff.Photometric.RGB;
end
% what is the numeric type?
if isinteger(inpict) && inclass(1) == 'u'
tagstruct.SampleFormat = Tiff.SampleFormat.UInt;
elseif isinteger(inpict) && inclass(1) == 'i'
tagstruct.SampleFormat = Tiff.SampleFormat.Int;
elseif isfloat(inpict)
tagstruct.SampleFormat = Tiff.SampleFormat.IEEEFP;
else
error('this demo only supports integer and float inputs')
end
% what's the sample width?
switch inclass
case {'uint8','int8'}
tagstruct.BitsPerSample = 8;
case {'uint16','int16'}
tagstruct.BitsPerSample = 16;
case {'uint32','int32','single'}
tagstruct.BitsPerSample = 32;
case {'uint64','int64'}
error('LibTIFF does not support 64b outputs except for double-precision floats')
case 'double'
tagstruct.BitsPerSample = 64;
otherwise
error('this demo only supports integer and float inputs')
end
% write everything and close the file
t.setTag(tagstruct);
t.write(inpict);
t.close();
% read the image back
newpict = imread(filename);
isequal(inpict,newpict) % test that it's the same
Limitations:
- I have made no attempt to support logical images
- AFAIK, uint64/int64 inputs are not supported by LibTIFF. There may be broader reasons for that.
- Signed integer inputs cannot be RGB.
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!