Find the average image of a set of images
Show older comments
Hello,
I need to calculate the average image of a training set images but I don't have any idea how to do it.
Thank you in advance !
Answers (5)
Image Analyst
on 11 Dec 2011
Edited: Image Analyst
on 16 Aug 2016
How about just doing a for loop:
for k = 1 : numberOfImages
thisImage = double(imread(files(k).filename)); % Or whatever...
[rows columns numberOfColorBands] = size(thisImage);
% First do a check for matching rows, columns, and number of color channels. Then:
if k == 1
sumImage = thisImage;
else
sumImage = sumImage + thisImage;
end
end
sumImage = sumImage / numberOfImages;
If you want, you can then either leave sumImage as double
imshow(sumImage, []); % Use []
or cast back to uint8 or uint16
sumImage = uint8(sumImage);
imshow(sumImage); % [] is optional now.
I do it all the time and it's fast
1 Comment
Image Analyst
on 11 Dec 2011
Referring to your answer, I hadn't seen your code yet. Obviously my files(k).filename should be your jpegFiles(k).name. Plus make sure you do the size check for robustness. It's not robust unless you check for that because it's dangerous to assume that all your images will be the same size. Even if they are, it can't hurt and makes it more robust should someone decide to borrow that snippet of code for another averaging app.
Kuno Meyer
on 16 Aug 2016
1 vote
@Image Analys, you should also care about clipping. Use im2double() before accumulation and im2uint8() afterwards.
1 Comment
Image Analyst
on 16 Aug 2016
Edited: Image Analyst
on 16 Aug 2016
Yes, that's true. Good catch. I'll fix it.
K.
on 11 Dec 2011
kavya k
on 15 May 2017
0 votes
sir, i am trying to perform the averaging of 30 images . i am unable to clear that error can any one help me please......
kavya k
on 15 May 2017
0 votes
sir, i am trying to perform the averaging of 30 images . i am unable to clear that error can any one help me please...... i have attached the .m file please once check and help me out
4 Comments
Image Analyst
on 15 May 2017
Lots of stuff wrong with that. See improved version below.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 30;
filenames{1} = '20170508115706551.bmp';
filenames{2} = '20170508115706871.bmp';
filenames{3} = '20170508115707191.bmp';
filenames{4} = '20170508115707511.bmp';
filenames{5} = '20170508115708150.bmp';
filenames{6} = '20170508115708470.bmp';
filenames{7} = '20170508115708790.bmp';
filenames{8} = '20170508115709110.bmp';
filenames{9} = '20170508115709433.bmp';
filenames{10} = '20170508115709750.bmp';
filenames{11} = '20170508115710070.bmp';
filenames{12} = '20170508115710711.bmp';
filenames{13} = '20170508115711031.bmp';
filenames{14} = '20170508115711670.bmp';
filenames{15} = '20170508115712310.bmp';
filenames{16} = '20170508115712630.bmp';
filenames{17} = '20170508115712950.bmp';
filenames{18} = '20170508115713590.bmp';
filenames{19} = '20170508115713912.bmp';
filenames{20} = '20170508115714230.bmp';
filenames{21} = '20170508115714550.bmp';
filenames{22} = '20170508115715192.bmp';
filenames{23} = '20170508115715510.bmp';
filenames{24} = '20170508115715830.bmp';
filenames{25} = '20170508115716470.bmp';
filenames{26} = '20170508115716790.bmp';
filenames{27} = '20170508115717431.bmp';
filenames{28} = '20170508115717751.bmp';
filenames{29} = '20170508115718070.bmp';
filenames{30} = '20170508115718390.bmp';
count = 0;
for k = 1 : length(filenames)
thisFileName = filenames{k};
if exist(thisFileName, 'file')
thisImage = double(imread(thisFileName));
if k == 1
sumImage = thisImage;
else
sumImage = sumImage + thisImage;
end
count = count + 1;
end
end
meanImage = sumImage / count;
figure
imshow(meanImage, []);
axis on;
title('Mean Image', 'FontSize', 30);
Orhan Albayrak
on 10 Mar 2019
I have tried to run this code but i have encountered with an errror. It said to me that-->
Array dimensions must match for binary array op.
Error in assign2 (line 64)
sumImage = sumImage + thisImage;
so what can i do now?
Image Analyst
on 10 Mar 2019
Well obviously thisImage is a different size than sumImage. What sizes are they? Look in the workspace, or use the size() function
[rows, columns, numberOfColorChannels] = size(sumImage)
[rows2, columns2, numberOfColorChannels2] = size(thisImage)
if isequal(size(sumImage), size(thisImage))
sumImage = sumImage + thisImage;
else
warningMessage = sprintf('Image sizes do not match.\nCheck sizes in the command window\n')
uiwait(errordlg(warningMessage));
return;
end
Orhan Albayrak
on 11 Mar 2019
Thank you for helping me :)
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!