Working with Image Stacks (or Arrays)

3 views (last 30 days)
Jason
Jason on 3 Mar 2015
Commented: Jason on 4 Mar 2015
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)

Accepted Answer

Geoff Hayes
Geoff Hayes on 3 Mar 2015
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
  8 Comments
Geoff Hayes
Geoff Hayes on 4 Mar 2015
Jason - if imgArray is only showing the fifth entry populated, then you will have to look at the code that builds this array. How are you populating imgArray?
Jason
Jason on 4 Mar 2015
It was a mistake, i initialised the cell array outside of my function but didnt give it global scope.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!