using a for loop to create a structure of images

I'm using matlab to control a microscope camera. I set up a script to grab 8 frames and average them, shown partially below. (The mmc lines are java commands that let MATLAB communicate via Java to the camera and other microscope parts via the micromanager API):
xpix = mmc.getImageWidth;
ypix = mmc.getImageHeight;
mmc.snapImage();
im.im1 = mmc.getImage();
im.im1 = reshape(uint16(im.im1), xpix, ypix);
mmc.snapImage();
im.im2 = mmc.getImage();
im.im2 = reshape(uint16(im.im2), xpix, ypix);
...
mmc.snapImage();
im.im8 = mmc.getImage();
im.im8 = reshape(uint16(im.im8), xpix, ypix);
avim = (im.im1 + im.im2 ... + im.im8)/8;
So, is there an easier way I could set this up using a for loop? I don't know how to tell it to name a variable or parts of a structure sequentially. It would be nice if there were a variable n, that the use could input to tell it how many frames to average, either in the first line of the script (n=4) or as a variable when i make it into a function. I am not profient in MATLAB so my efforts to write a for loop so far have not worked.

 Accepted Answer

Why does it need to be reshaped? Anyway, what's wrong with this:
sumImage = zeros(ypix, xpix);
for frameNumber = 1 : 8
mmc.snapImage();
sumImage = sumImage + mmc.getImage();
pause(0.5); % If you want a time between grabbed frames.
end
meanImage = sumImage / 8;

1 Comment

Thanks! I implemented a version of the loop that is now working in a function:
sumIm = zeros(xpix, ypix, 'uint16');
for frnum = 1:n
mmc.snapImage();
im = mmc.getImage();
im = reshape(uint16(im), xpix, ypix);
sumIm = sumIm + im;
end
im = sumIm / n;
I think the camera reads out the pixels in columns instead of rows, or something like that so that without reshaping the image is scrambled.

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!