Average images as they are captured

1 view (last 30 days)
I'm writing an AppDesigner GUI that averages n images as they are captured from FLIR camera and saves only the averaged image to a .tif file, then restarts averaging process (ie. loops averaging/saving process for every n captures). Image capture is triggered by button press. Captured images initially saved as value arrays.
Available online examples refer to averaging images that are already saved to computer files (I cannot do this as it uses too much memory).
Relevant code:
% Set up video input object
if exist('app.vid','var')
delete(app.vid)
clear app.vid
end
app.vid=videoinput("mwspinnakerimaq");
% Button pushed function: CaptureaverageimageButton
function CaptureaverageimageButtonPushed(app, event)
% Averaging n images
n = str2double(app.AveragenimagesnEditField.Value); %value entered in Edit Field
for k=1:n %How does code know image number? Add (k) somewhere?
im1=getsnapshot(app.vid);
[row1,column1]=size(im1);
if k==1
sumIm=double(im1); %save first image & its dimensions
r=row1;
c=column1;
else % any image following the first one
if r ~= row1 || c ~= column1
% Resize to match first image's size
im1 = reshape(im1,[r,c]); % imresize, reshape, or a different function?
end
sumIm = sumIm + double(im1);
avgIm=sumIm/n;
imshow(avgIm,[],'Parent',app.UIAxes2);
drawnow;
end
end
%How to restart loop once averaged image is acquired?
end
%Save to file:
a = 1; %a=averaging loop number (ie. how many loops completed, with n averaged images per loop)
while isfile(strcat(app.parentFolder,strcat(app.fullDate,'-',num2str(n),'-',num2str(a),'.tif')))==1
a= a+1;
end
fullFileName=strcat(app.parentFolder,strcat(app.fullDate,'-',num2str(n),'-',num2str(a),'.tif'));
imwrite(avgIm,fullFileName,"tif")
At the moment, individual images are saved to files, not averages. I think my problem is giving each captured image a "k" value.
Thanks for your help.

Accepted Answer

Walter Roberson
Walter Roberson on 10 Aug 2022
read first image. This establishes size of images. set imgtot to double(img) and count to 1
loop reading images. At each point add double(img) to imgtot and increase count.
At any point that you need an average, divide imgtot by count. You might need to cast() to the class of img before writing.
Though if you im2double each image as it comes in then you will not need to cast the average, if you are happy with imwrite automatically converting to 8 bit when it writes.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!