Find the average image of a set of images

조회 수: 48 (최근 30일)
K.
K. 2011년 12월 11일
댓글: Orhan Albayrak 2019년 3월 11일
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 !

답변 (5개)

Image Analyst
Image Analyst 2011년 12월 11일
편집: Image Analyst 2016년 8월 16일
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
Image Analyst
Image Analyst 2011년 12월 11일
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
Kuno Meyer 2016년 8월 16일
@Image Analys, you should also care about clipping. Use im2double() before accumulation and im2uint8() afterwards.
  댓글 수: 1
Image Analyst
Image Analyst 2016년 8월 16일
편집: Image Analyst 2016년 8월 16일
Yes, that's true. Good catch. I'll fix it.

댓글을 달려면 로그인하십시오.


K.
K. 2011년 12월 11일
Thanks for the answer!
I've already read and displayed the images using this code :
myFolder = 'C:\Users\***\Desktop\barcelona';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not
exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
end
and tried to use the loop you mentioned but I didn't understand for what "files" refer to:
for k = 1 : 48
thisImage = imread(files(k).fullFileName); % 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 / 48;
An error appears telling me that "files" is an ndefined variable or class "files".
Thanks again.

kavya k
kavya k 2017년 5월 15일
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
kavya k 2017년 5월 15일
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
Image Analyst
Image Analyst 2019년 3월 10일
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
Orhan Albayrak 2019년 3월 11일
Thank you for helping me :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by