Save a 3D image to TIFF

114 views (last 30 days)
Ace
Ace on 14 May 2024
Answered: Ace on 15 May 2024
Hi everyone,
I made a bwdist transform of some image and I am struggling to export it to the .tiff format. Actually I want to create a 2D image stack representing a 3D volume. I read about imwrite which says it cannot save an image with so many layers, and then about tiff function, but not sure how to you use it.. The original image was created using ImageJ and was in the tiff format, now I need to export the MATLAB's outcome back to tiff.
I'd appreciate your help.

Accepted Answer

Ace
Ace on 15 May 2024
In case somebody has a similar problem, it can be solved in the following way:
% Define the output filename for the multi-page TIFF stack
outputFilename = 'yourname.tif'; % Specify your desired output filename
% Create a Tiff object for writing the stack
t = Tiff(outputFilename, 'w');
% Get the size of the Mask - type your array's name
[numRows, numCols, numSlices] = size(Mask2);
% Loop through each slice and add it to the multi-page TIFF stack
for slice = 1:numSlices
% Extract the 2D slice
sliceData = Mask2(:, :, slice);
% Write the current slice to the multi-page TIFF stack
t.setTag('Photometric', Tiff.Photometric.MinIsBlack);
t.setTag('Compression', Tiff.Compression.None);
t.setTag('BitsPerSample', 32); % Set BitsPerSample to 32 for single precision data
t.setTag('SamplesPerPixel', 1);
t.setTag('SampleFormat', Tiff.SampleFormat.IEEEFP);
t.setTag('ImageLength', numRows);
t.setTag('ImageWidth', numCols);
t.setTag('PlanarConfiguration', Tiff.PlanarConfiguration.Chunky);
t.write(sliceData);
% Add a new page to the multi-page TIFF stack
if slice ~= numSlices
t.writeDirectory();
end
end
% Close the TIFF file
t.close();
You may also need to modify the min or max values when uploading your tif to other software such as ImageJ / Fiji to properly see all objects.

More Answers (1)

Matt J
Matt J on 14 May 2024
  1 Comment
Ace
Ace on 15 May 2024
Thanks but for some reason doesn't work for me - maybe something is wrongly adjusted to my case.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!