Comparing images in groups

1 view (last 30 days)
Manaf Noofal Taha Ahmed
Manaf Noofal Taha Ahmed on 20 Nov 2022
Commented: Image Analyst on 23 Nov 2022
Hello everyone, I am processing atomization images consisting of 120 folders. In each folder there are 3500 images. One of my tasks is to compare each 100 images individually in each file so that I obtain 35 output image per file.
My question is how to automatize selecting images from 1 to 100 and from 101 to 200 and so on? I need such a code to help me with this.
I have tried the following code, but it takes time and no result just stays in running condition. The code is:
jpgFiles= dir('*.jpg');
for n= 0:100:2900
for k=1+n:2901
for i= 100+n:3000
mydata{k:i}= imread(jpgFiles(k:i).name);
% the code
end
end
end
Thanks in advance.
  8 Comments
Image Analyst
Image Analyst on 22 Nov 2022
Have you tried the FAQ:
Did you try a nested for loop? How do you want to group the files? Just by whatever order the operating system hands them to you? Or do you have some kind of naming protocol that you need to follow?
Manaf Noofal Taha Ahmed
Manaf Noofal Taha Ahmed on 22 Nov 2022
No I have not tried nested for loop. My files are named with a sequence number ending.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 22 Nov 2022
OK, so you don't have to try nested for loops if you don't want. You can compute the mean of everything then group the means into groups of 100 afterwards.
Here is a start:
% Demo by Image Analyst
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 short g;
format compact;
fontSize = 22;
markerSize = 20;
% Specify the folder where the files live.
myFolder = pwd; %'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
allFileNames = sort(fullfile(myFolder, {theFiles.name}))';
numFiles = numel(allFileNames)
meanGL = zeros(numFiles, 1); % Should be a multiple of 100.
for k = 1 : numFiles
fullFileName = allFileNames{k};
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray = imread(fullFileName);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
% Get the mean
meanGL(k) = mean(imageArray, 'all');
end
% Get the means in groups of 100 files.
numFilesInGroup = 100;
leftOver = rem(numFiles, numFilesInGroup);
if leftOver ~= 0
% The number of files is not an integer multiple of the number of files in the group.
meanGL = meanGL(1:end-leftOver);
end
meanGL = reshape(meanGL, numFilesInGroup, [])
groupMeans = mean(meanGL, 1);
  2 Comments
Manaf Noofal Taha Ahmed
Manaf Noofal Taha Ahmed on 23 Nov 2022
Thank you dear @Image Analyst for your cooperation. I do not want to find the mean image of all, then group them in 100.
My purpose is to find the mean image of each 100 image, for example, in the file of 3000 images, I want to find the mean image of the first 100 images, then mean image of the second 100 images and so on. After that, I want to save the output mean images in a new folder.
Thanks in advance.
Image Analyst
Image Analyst on 23 Nov 2022
Then you need to create a sum image. Be sure to cast to double. Use a double for loop. In the inner loop over the next 100 images, have this:
if k == 1
sumImage = double(imageArray);
else
sumImage = sumImage + double(imageArray);
end
Then after the loop over 100 images
averageImage = sumImage / 100;
You should be able to figure it out. If not, invest the next 2 hours here:

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!