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
Thanks in advance.
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);
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
% 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, {}))';
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');
% 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);
meanGL = reshape(meanGL, numFilesInGroup, [])
groupMeans = mean(meanGL, 1);
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);
sumImage = sumImage + double(imageArray);
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!