Working with Image Stacks (or Arrays)

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

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

Hi Geoff, thanks for your comments.
The problem is the pre allocation. I don't know how many images there will be. Is it possible to not preallocate.
Just to add.
I save the imgArray to appdata so I can call it in a slidercall back (with an aim to scroll through the image array).
To save the image array i use
setappdata(0,'IS',imgArray); %Save to variable IS = Image Stack
Then in the slider call back:
stack=getappdata(0,'IS');
I then obtain the slider value, p and have made sure if relates to the image stack.
axes(handles.axes1)
imshow(stack{p},[]);
But, Im not seeing any images. I have checked the index p returned from the sldier and itrs fine.
Jason - you need not pre-size the array. Just do
imgArray = {};
and then use the usual concatenation to insert new elements into the array. However, depending upon the number of images you can always pre-size the array with an upper bound (there is no harm in this). Perhaps 500 will suffice.
As for the other problem, have you verified that imgArray is populated when you save it with setappdata? Why not save it to the handles structure instead (I'm assuming that you are using GUIDE to build your GUI).
How do i verify imgArray is populated, by using size?
Yes, you can use size or length or just type
imgArray
in the Command Window (assuming you have put a breakpoint in your code where
setappdata(0,'IS',imgArray);
is called.
And consider using the handles structure to store the data (using guidata to do so).
Thanks. I have put the breakpoint in on the line
setappdata(0,'IS',imgArray);
and checking what imgArray is here gives: (It shoudl be 5 images)
imgArray=
[] [] [] [] [] [1040x1393 uint16]
I have tried
figure
imshow(imgArray{1},[]);
But don't see anything on the figure.
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?
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)

Asked:

on 3 Mar 2015

Commented:

on 4 Mar 2015

Community Treasure Hunt

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

Start Hunting!