MATLAB: Working with Image Stacks (or Arrays)

arrayimagestacks

I am reading images from a camera and displaying them on on an axes.
Every time I press a "Snap" button, the axes component updates with the new image. I am wanting to keep all the images I collect in a stack or array.
My initial thought is to use something along the lines of
imgArray=cat(3,image1,image2,image3,...)
But after I have collected image1, then assign it to the array imgArray, Im not sure how to then add the second image to this array, and then the third to this array.
2: How would I save each image individually from the imgArray once i have finished (the are tiffs)

Best Answer

Jason - are all images of the same dimension? If not (and even if they are) it may be easier to just use a cell array.
If you have an idea of how many images you are going to collect, then initialize your cell array as
numImages = 42;
imgArray = cell(numImages,1);
Keep an index of the next position to store the image at
nextImgIdx = 1;
Then whenever you need to add an image to the array, just do the following
imgArray{nextImgIdx} = myImage;
nextImgIdx = nextImgIdx + 1;
Once finished, you can then iterate over each image in the array and save to file.
for k=1:length(imgArray)
imwrite(imgArray{k},sprintf('image%04d.tiff',k);
end
Related Question