Save a 3D image to TIFF

108 次查看(过去 30 天)
Ace
Ace 2024-5-14
回答: Ace 2024-5-15
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.

采纳的回答

Ace
Ace 2024-5-15
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.

更多回答(1 个)

Matt J
Matt J 2024-5-14
  1 个评论
Ace
Ace 2024-5-15
Thanks but for some reason doesn't work for me - maybe something is wrongly adjusted to my case.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by