Saving an array of images as an 'Image Stack'

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

回答 (1 件)

Jan
Jan 2016 年 11 月 1 日

4 投票

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 件のコメント

Jason
Jason 2016 年 11 月 1 日
Why does the following not work
for k=1:5
imgArray3(:,:,k)=imgArray{k};
end
imwrite(imgArray3,sprintf('%s\\%s.stk',folder,prefix));
Jan
Jan 2016 年 11 月 1 日
What error are you getting? On my Matlab version, the stk format is not supported.
Jason
Jason 2016 年 11 月 1 日
I assumed you could use what ever extension name you wanted to. The code is testing on a compiled version using the MRC so i cannot get to the error message
Adam
Adam 2016 年 11 月 1 日
imwrite uses the extension to work out the format in which to save the file.
Jason
Jason 2016 年 11 月 1 日
編集済み: Jason 2016 年 11 月 1 日
I see there is a fmt optionwith imwrite to set the type as either tif or tiff. It still doesn't seem to work - so its seems I must have the .tif or .tiff extension. I really wanted a different extension to distinguish this file as a lot of tiffs rather than just a single tiff
Jason
Jason 2016 年 11 月 1 日
Correction. It should be possible to save the tiffs using any extension using fmt option
imwrite(___,fmt) writes the image in the format specified by fmt, regardless of the file extension in filename. You can specify fmt after the input arguments in any of the previous syntaxes.
Joe Sheppard
Joe Sheppard 2018 年 4 月 3 日
I have seen 'WriteMode' 'append' suggested everywhere, but for some reason MatLab still saves the files as seperate tiffs rather than a single file, why would this be?
Sai Sirish Gokavarapu
Sai Sirish Gokavarapu 2019 年 9 月 5 日
編集済み: Sai Sirish Gokavarapu 2019 年 9 月 5 日
Hey there! Even I'm trying to use to convert a .avi file to a .tif STACk but unfortunately the code just converts it to a set of .tif images despite using 'WriteMode' and "append".
Here's the code, it's similar to that of Joe.
%% To convert avi files to tif images
obj = VideoReader('video.avi');
vid = read(obj);
frames = obj.NumberOfFrames;
% This just converts them to a set of images
% for x = 1 : frames
% imwrite(vid(:,:,:,x),strcat('frame-',num2str(x),'.tif'));
% end
%This is meant to amend that and to make a .tif stack
for x = 1 : frames
imwrite(vid(:,:,:,x),strcat('frame-',num2str(x),'.tif'), 'Compression', 'none','WriteMode', "append");
end
Lili Xu
Lili Xu 2020 年 6 月 7 日
I've been dealing with the same issue the brutal way by just saving the separate tifs in the same folder and open the folder using ImageJ afterwards and let ImageJ do the job of combining the separate tifs into a stack...
Yael Grossman
Yael Grossman 2021 年 3 月 8 日
@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 2021 年 9 月 15 日
Thank you Yael! That worked perfectly for me.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeImages についてさらに検索

質問済み:

2016 年 11 月 1 日

コメント済み:

2021 年 9 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by