Average of 10 images
Show older comments
Hi;
I read a series of images and I want to calculate their average I've written
this code :
I=zeros(size(Io))
for i=1:10
image{i}=im2double(imread(['0.1s_',num2str(i),'.tif']));
I=I+image{i}
end;
I=I./10;
am I right ? knowing that it gives a result
Answers (1)
Image Analyst
on 14 Jul 2013
No, it's not. For one thing, don't use image as the name of a variable since it's the name of a built-in function. And no reason to store them all and use a cell array.
I0 = imread('0.1s_1.tif')
sumImage = double(I0); % Inialize to first image.
for i=2:10 % Read in remaining images.
rgbImage = imread(['0.1s_',num2str(i),'.tif']));
sumImage = sumImage + double(rgbImage);
end;
meanImage = sumImage / 10;
6 Comments
Hi; I tried the code which was modified on the basis of your code.
the code:
amg = imread('1.jpg'); % 1.jpg is one of 10 images
[a,b,c]=size(amg);
dst_img=zeros(a,b,c);
for k=1:10
filename=[num2str(k), '.jpg'];
d=imread(filename, 'jpg');
dst_img=dst_img+double(d);
end
dst_img=dst_img/k;
dst_img=uint8(dst_img);
figure;imshow(dst_img);title('Average');
However, the result became more blured than input images.
The scene was still and the camera was still.
Can you tell me how to solve this problem?
Thanks.


Image Analyst
on 13 Sep 2017
The images must be in slightly different positions, despite you thinking they aren't. The way around it is to call imregister() to align the images before summing.
CS
on 21 Nov 2018
and how can we do that? put imregister() before the imread?
By the way i was trying to find moving object so i thought of finding the background and then subtract, do you know a better way to do that?
Walter Roberson
on 21 Nov 2018
You need a base image such aas the first in the series . Then after every imread you would register the new image against the base image and add in the aligned version of the new image .
Saransh
on 19 Jun 2023
I0 = double(imread('0.1s_1.tif'));
sumImage = I0; % Initialize to first image.
for i = 2:10 % Read in remaining images.
rgbImage = double(imread(['0.1s_', num2str(i), '.tif']));
sumImage = sumImage + rgbImage;
end
meanImage = sumImage / 10;
Image Analyst
on 19 Jun 2023
You could do it that way if that's all you're going to use rgbImage for, and it would essentially be the same as how I did it:
I0 = imread('0.1s_1.tif')
sumImage = double(I0); % Inialize to first image.
for i=2:10 % Read in remaining images.
rgbImage = imread(['0.1s_',num2str(i),'.tif']));
sumImage = sumImage + double(rgbImage);
end;
meanImage = sumImage / 10;
but I would not recommend your way. If rgbImage is in the range 0-255 and you cast it to double immediately then if you go to display it, it will show up as all white. That could be confusing. The way I did it does not change rgbImage at all so it will still display normally. Only a copy of rgbImage is cast to double and summed into sumImage so the actual rgbImage is not changed at all, like in your code, and so things like displaying and other operations that expect pixels values in the range 0-255 will still work.
Actually the code I gave is not as flexible and robust as I'd normally do it but I guess I wanted to be as similar to the original poster's code as I could so he'd understand it. I'd rather do it like this:
% Get a file specification.
folder = pwd; % Wherever you want.
filePattern = fullfile(folder, '0.1f*.tif'); % Adapt as needed.
% Get files in the folder matching that specification.
fileList = dir(filePattern);
numberOfFiles = numel(fileList)
numberOfFilesSummed = 0;
% Concatenate all filenames into one cell array for convenience.
allFileNames = fullfile(folder, {fileList.name})
% Read in first image and put it into the sum image.
sumImage = double(imread(allFileNames{1}));
firstSize = size(sumImage)
% Read in remaining images and sum them in.
for k = 2 : numberOfFiles
% Read in RGB image.
rgbImage = imread(allFileNames{k});
% Display the image.
imshow(rgbImage);
caption = sprintf('File #%d of %d : "%s"', fileList(k).name);
title(caption);
drawnow;
% Make sure size matches that of the very first image.
if ~isequal(firstSize, size(rgbImage))
fprintf('Skipping "%s"\n because size does not match that of the first image\n', allFileNames{k});
continue;
end
% Sum in the image.
sumImage = sumImage + double(rgbImage);
% Increment the number of images we actually summed.
numberOfFilesSummed = numberOfFilesSummed + 1;
end
% Divide by the number of images to get the mean.
meanImage = sumImage / numberOfFilesSummed;
% Display the floating point mean image with [] to scale it properly.
imshow(meanImage, []);
The above is more robust and flexible since it does not depend on there being exactly 10 images in the current folder, and it checks for size mismatches, plus it also displays the current image being summed in and is very well commented.
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!