Comparing images in groups

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 个评论

What have you tried so far?
The question is updated with the code.
What does "compare" mean to you? You mean perfect equality like isequal(image1, image2) or do you want to compare some other attribute(s) of the images?
I want to collect each 100 image and take the mean image of them for example. Comparison here is to compare between each 100 image regarding the mean image of each batch images so that I have 30 mean images out of the 3000 input images.
It's still not clear. And my Mind Reading Toolbox is still on order. So you have a folder of 3500 images and you want to somehow process "100 images individually in each file". How does each file have 100 images in it? Do you want to split up each of the 3500 images into a set of 10 by 10 subimages? What is a "mean image"? Is it a uniform image of just one gray level -- the mean of the batch? And do you have 3000 images in each folder, or 3500 images?
Yeah, I may misexplained the point. Let me put it clearly:
  • I have 6 folders, in each folder there are about 3000 images.
  • For each folder I should take the greyscale mean image for each 100 images, i.e, from image 1--> 100 I need a greyscale mean image and from 101-->200 I need a greyscale mean image and so on. Therefore, for this file consisting of 3000 images I will obtain 30 mean greyscale images. The same will be applied for the remaining 5 folders.
  • The code ffor obtaining the mean image is already with me. My problem is how to let the matlab select from 1 to 100 and from 101 to 200 and so on automatically.
I hope it is clearer, and thank you for your interaction.
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?
No I have not tried nested for loop. My files are named with a sequence number ending.

请先登录,再进行评论。

回答(1 个)

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 个评论

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.
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:

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by