Save a 3D image to TIFF

조회 수: 87 (최근 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.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by