Saving an array of images as an 'Image Stack'

144 views (last 30 days)
Jason
Jason on 1 Nov 2016
Commented: Cody Crosby on 15 Sep 2021
Hi.
I obtain grayscale image' 'img' that I add to a 'stack' i.e. image array by:
imgArray{index}=img
So If I have say 5 images in the imgArray, I have been saving them to disk by writing them out individually as tifs via:
prefix=get(handles.editSaveName,'String');
for k=1:5
imwrite(imgArray{k},sprintf('%s\\%s%04d.tif',folder,prefix,k));
end
BUT, I want the option to say the array of images as a single file i.e. an image stack. I thought the following would do it
try
imwrite(imgArray,sprintf('%s\\%s.stk',folder,prefix));
catch
h = errordlg('Error Saving Image STK')
end
But it always fails. Is there an obvious error?
Thanks
Jason

Answers (1)

Jan
Jan on 1 Nov 2016
According to the matlab documentation you can store multiple images to a single file as follows:
imwrite(im1,'myMultipageFile.tif')
imwrite(im2,'myMultipageFile.tif','WriteMode','append')
See https://www.mathworks.com/help/matlab/ref/imwrite.html --> Write Multiple Images to TIFF File.
  11 Comments
Yael Grossman
Yael Grossman on 8 Mar 2021
@Lili Xu@Sai Sirish Gokavarapu The bug in the code is that you're chaning the file name for each iteration
for x = 1 : frames
imwrite(vid(:,:,:,x),'All_frames.tif', 'Compression', 'none','WriteMode', "append");
end
Cody Crosby
Cody Crosby on 15 Sep 2021
Thank you Yael! That worked perfectly for me.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!