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 个评论
Rik
2022-11-21
What have you tried so far?
Manaf Noofal Taha Ahmed
2022-11-22
Image Analyst
2022-11-22
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?
Manaf Noofal Taha Ahmed
2022-11-22
Image Analyst
2022-11-22
编辑:Image Analyst
2022-11-22
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?
Manaf Noofal Taha Ahmed
2022-11-22
Image Analyst
2022-11-22
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
2022-11-22
回答(1 个)
Image Analyst
2022-11-22
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 个评论
Manaf Noofal Taha Ahmed
2022-11-23
Image Analyst
2022-11-23
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:
类别
在 帮助中心 和 File Exchange 中查找有关 Image Preview and Device Configuration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!