Clear Filters
Clear Filters

Average image of a folder of images

5 views (last 30 days)
Gee Cheng Mun
Gee Cheng Mun on 10 Jan 2016
Commented: Marcela pena on 26 Mar 2018
I am trying to obtain the average image for this folder of multiple images.
My code is as shown, however there is an error -> thisImage=imread(files(k).myFolder);
myFolder='E:\FYP2\Time Frames\Frame 13';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.png');
theFiles = dir(filePattern);
numberOfImages=length(theFiles);
for k=1 : numberOfImages
thisImage=imread(files(k).myFolder);
[rows, columns, numberOfColorBands]=size(thisImage);
if k == 1
sumImage = thisImage;
else
sumImage = sumImage + thisImage;
end
end
sumImage = sumImage / numberImages;

Answers (1)

Image Analyst
Image Analyst on 10 Jan 2016
Instead of this:
thisImage=imread(files(k).myFolder);
where
  1. you did not include the folder when sending the filename to imread(), and
  2. you did not use the right variable (files instead of theFiles) and
  3. you did not have the right structure member off files (should be name, not myFolder),
try this:
fullFileName = fullfile(myFolder, theFiles(k).name);
thisImage=double(imread(fullFileName)); % Be sure to cast to double to prevent clipping.
  3 Comments
Image Analyst
Image Analyst on 10 Jan 2016
See my attached demo, which averages all gray scale or RGB images in a folder.
Marcela pena
Marcela pena on 26 Mar 2018
Thanks for the script !!!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!