Using Tiff function to save tiff files

70 views (last 30 days)
Raphaël
Raphaël on 7 Feb 2024
Edited: DGM on 7 Feb 2024
Hi everyone,
I am currently trying to save a 3D array into a SINGLE tiff file using the Tiff function as suggested by Matlab. I have found no example showing how to use the Tiff function. Does anyone know? Here is an example of code that needs to be completed:
A = rand(512,512,512);
file = Tiff('myfile.tif','w');
file.write(A)
file.close()
Thank you for you help.

Answers (1)

DGM
DGM on 7 Feb 2024
Edited: DGM on 7 Feb 2024
There are at least two ways of doing this.
You can try to just cram everything into a single image. As far as I can tell, I don't see where this is contrary to the TIFF spec, but MATLAB does complain about it. It still works though.
fname = 'fname.tif';
% a MxNx5 3D image
inpict = imread('cameraman.tif');
inpict = repmat(inpict,[1 1 5]);
%warning off
% create the file (you'll get a warning)
write3Dtiff(inpict,fname)
Warning: Sum of Photometric color channels and ExtraSamples does not match the value specified in SamplesPerPixel.
Writing with this configuration will error in a future release. To correct the configuration, define the non-color channels as ExtraSamples.
% read the imate
C(:,:,:) = imread(fname,1);
% test it
size(C) % the size is as expected
ans = 1×3
256 256 5
isequal(C,inpict) % it's the same as the original
ans = logical
1
Alternatively, you can split the pages of the volumetric image, representing each slice as a frame (a distinct image). This is the more general approach, but reading the file is more verbose.
clearvars
fname = 'fname.tif';
% a MxNx5 3D image
inpict = imread('cameraman.tif');
inpict = repmat(inpict,[1 1 5]);
% reshape the image to be a 4D array
% this presumes that inpict is MxNxFx1
inpict4D = permute(inpict,[1 2 4 3]);
% create the file
write3Dtiff(inpict4D,fname)
% get the information in the multitiff image
info = imfinfo(fname);
nframes = numel(info);
% read images to check that it's the expected size
% this assumes all the frames are the same class and size
C(:,:,:,1) = imread(fname,1);
C = repmat(C,[1 1 1 nframes]);
for f = 2:nframes
C(:,:,:,f) = imread(fname,f);
end
% permute it back into a 3D array
% this presumes that all frames of C are single-channel
C = permute(C,[1 2 4 3]);
% test it
size(C) % the size is as expected
ans = 1×3
256 256 5
isequal(C,inpict) % it's the same as the original
ans = logical
1
Choose whichever you feel is suitable. This is an example helper function as is used in both examples above.
function write3Dtiff(array, filename)
% make sure the vector returned by size() is of length 4
dims = size(array,1:4);
% Set data type specific fields
if isa(array, 'single')
bitsPerSample = 32;
sampleFormat = Tiff.SampleFormat.IEEEFP;
elseif isa(array, 'uint16')
bitsPerSample = 16;
sampleFormat = Tiff.SampleFormat.UInt;
elseif isa(array, 'uint8')
bitsPerSample = 8;
sampleFormat = Tiff.SampleFormat.UInt;
else
% if you want to handle other numeric classes, add them yourself
disp('Unsupported data type');
return;
end
% Open TIFF file in write mode
outtiff = Tiff(filename,'w');
% Loop through frames
for f = 1:dims(4)
% Set tag structure for each frame
tagstruct.ImageLength = dims(1);
tagstruct.ImageWidth = dims(2);
tagstruct.SamplesPerPixel = dims(3);
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
tagstruct.BitsPerSample = bitsPerSample;
tagstruct.SampleFormat = sampleFormat;
if any(dims(3) == [3 4]) % assume these are RGB/RGBA
tagstruct.Photometric = Tiff.Photometric.RGB;
else % otherwise assume it's I/IA or volumetric
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
end
if any(dims(3) == [2 4]) % assume these are IA/RGBA
tagstruct.ExtraSamples = Tiff.ExtraSamples.AssociatedAlpha;
end
% Set the tag for the current frame
outtiff.setTag(tagstruct);
% Write the frame
outtiff.write(array(:,:,:,f));
% Create a new directory for the next frame
if f ~= dims(4)
outtiff.writeDirectory();
end
end
% Close the file
outtiff.close();
end

Tags

Community Treasure Hunt

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

Start Hunting!