MATLAB Answers

How can I create a 3D Tiff image (2D stack) from a 3D matrix?

24 views (last 30 days)
Nicola Calliari
Nicola Calliari on 29 Oct 2020
Answered: Tim on 30 Oct 2020
I have to save a 400x400x400 matrix (bw) in a stack of 2D tiff images.
I tried to us "imwrite" but it gives me this error:
Error using writetif (line 40)
Writing TIFFs with 400 components is not supported with IMWRITE. Use Tiff instead. Type "help Tiff" for more information.
Error in imwrite (line 546)
feval(fmt_s.write, data, map, filename, paramPairs{:});
But typing "help Tiff" I didn't find any solution.

  0 Comments

Sign in to comment.

Answers (1)

Tim
Tim on 30 Oct 2020
Loop over your volume using imwrite(image_page_1, filename) for n == 1 and imwrite(image_page_nLargerThan1, filename, 'WriteMode', 'append') for n > 1. Here's some code I use to do this on a regular basis:
% Your example volume
tst = randn(400, 400, 400);
% Normalize for RGB colormap mapping:
scaled_tst = tst./max(tst, [], 'all');
% Colormap
cmp = parula;
% Write flag: run once with iwrite = false if you want to see what you are writing first
iwrite = true;
% File name:
fname = 'Test.tif';
for n = 1:size(tst, 3)
% Make an RGB image:
i_img = ind2rgb(round(scaled_tst(:, :, n)*256), cmp);
% Check what you are writing
image(i_img);
drawnow;
% Generate your tiff stack:
if iwrite
if n == 1
% First slice:
imwrite(i_img,fname)
else
% Subsequent slices:
imwrite(i_img,fname,'WriteMode','append');
end
end
disp(n)
end

  0 Comments

Sign in to comment.

Categories

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!