필터 지우기
필터 지우기

Finding average of images using for loop.

조회 수: 5 (최근 30일)
Raushan
Raushan 2023년 3월 15일
댓글: Raushan 2023년 3월 17일
Hi,
I have hundreds of images, I Want to read the first 3 image and then get the average and save them in a file. I want to do this for all the images. I tried to do this but I am able to find the average of only first 3 image. Can anyone please help me to implement the following code in a loop? I'm also attaching 6 image for example to try them in a loop.
Folder='G:\trail\'
outdir = 'G:\Avg_Image';
I0 = imread('pic1_1_1.tiff');
sumImage = double(I0); % Inialize to first image.
for i=2:3 % Read first 3 image
rgbImage = imread(['pic1_1_',num2str(i),'.tiff']);
sumImage = sumImage + double(rgbImage);
end
meanImage = sumImage / 3;
imshow(uint8(meanImage));
outname1 = fullfile(outdir, "pic1_");
imwrite(meanImage, outname1);
I will appreciate any kind of help.

채택된 답변

Image Analyst
Image Analyst 2023년 3월 17일
@Raushan your code is not very robust at all. I've made many improvements and it's below. I also have a demo to sum images which is even more robust and can handle resizing the images. It's an attachment.
% Optional initialization steps
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
inputFolder= pwd; %'D:\';
outputFolder = 'D:\';
% Count the max number of files we expect to find.
files = dir(fullfile(inputFolder, '*.tif*'));
numFiles = numel(files)
sumImage = 0;
imageCounter = 0;
for k = 1 : numFiles
inputBaseFileName = files(k).name;
thisFileName = fullfile(inputFolder, sprintf('pic_%d.tiff', k));
% thisFileName = fullfile(files(k).folder, files(k).name);
fprintf('Processing #%d of %d : "%s".\n', k, numFiles, thisFileName);
% See if the file exists.
if ~isfile(thisFileName)
% It doesn't exist. Alert user and continue.
fprintf(' "%s" does not exist. Skipping it.\n', inputBaseFileName);
continue; % Skip this image.
end
% Read in image.
rgbImage = imread(thisFileName);
% If the size is not 3 skip it.
if size(rgbImage, 3) ~= 3
fprintf(' "%s" is not RGB full color. Skipping it.\n', inputBaseFileName);
continue;
end
% Display this input image.
subplot(1, 2, 1);
imshow(rgbImage);
impixelinfo;
title(files(k).name, 'Interpreter','none');
axis('on', 'image');
drawnow;
% Sum the image in.
try
sumImage = sumImage + double(rgbImage);
catch ME
% Couldn't sum, probably because the size didn't match the size of the prior image(s).
fprintf(' "%s" could not be summed. Skipping it.\n', inputBaseFileName);
continue;
end
imageCounter = imageCounter + 1;
% Every 10th image, write out the average picture.
if imageCounter == 10
baseFileName = sprintf('Average Pic starting at %d images.tiff', imageCounter);
fullOutputFileName = fullfile(outputFolder, baseFileName);
averageImage = uint8(sumImage / imageCounter);
% Display average image.
subplot(1, 2, 2);
imshow(averageImage);
impixelinfo;
title(baseFileName, 'Interpreter','none');
axis('on', 'image');
drawnow;
% Write out to disk.
imwrite(averageImage, fullOutputFileName);
% Reset everything to zero for the next set of 10 images.
sumImage = 0;
imageCounter = 0;
end
end
% Print out how many average images we have
outputFiles = dir(fullfile(outputFolder, 'Average Pic*.tif*'));
numOutputFiles = numel(outputFiles);
fprintf('There are %d average pictures now.\n', numOutputFiles);
  댓글 수: 1
Raushan
Raushan 2023년 3월 17일
Thank you very much for the suggestion. I just want to take average of every 10 image out of over 1000 images and save them. I did not understand what is wroing in my script. If we can do this in few lines of code then is it necessary to write long line codes. Is it possible to highlight mistakes in my script.

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

추가 답변 (1개)

Raushan
Raushan 2023년 3월 16일
편집: Walter Roberson 2023년 3월 17일
Folder='G:\
outdir = 'G:\
files = dir(fullfile(Folder, '*.tiff'));
sumImage = 0;
for k=1:length(files)
%thisFileName = files(k).name;
rgbImage = imread(['pic1_',num2str(k),'.tiff']);
sumImage = sumImage + double(rgbImage);
if mod(k,10) == 0
outname1 = fullfile(outdir, "pic1_" + floor(k/10) + ".tiff");
imwrite(uint8(sumImage/10), outname1);
sumImage = 0;
end
end
can you please check once whether I am doing correct?
Thank you.
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 3월 17일
imwrite(uint8(sumImage/10), outname1);
That is wrong. You would want
imwrite(uint8(floor(sumImage/k)), outname1);
Raushan
Raushan 2023년 3월 17일
I want average of every 10 image so I have divided by 10. If we divide by k it means you are not taking any average, right.

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

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by