Average of 10 images

41 ビュー (過去 30 日間)
Soum
Soum 2013 年 7 月 14 日
コメント済み: Image Analyst 2023 年 6 月 19 日
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

回答 (1 件)

Image Analyst
Image Analyst 2013 年 7 月 14 日
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 件のコメント
Saransh
Saransh 2023 年 6 月 19 日
hey @Image Analyst u should add a double funtion before line number 4 for precise calculation
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
Image Analyst 2023 年 6 月 19 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by