Compare images in one folder with a single image?

There is a folder with a set of sub folders. Each one of them contains several images.
Is there a way to compare first image in the first sub folder, with the first image of another folder. And so on...
Any idea? Thank you!

 Réponse acceptée

Try this code, parts of which I pulled from the FAQ:
% Specify the folder where the reference image file lives.
refFolder = pwd; % or wherever, such as 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(refFolder)
errorMessage = sprintf('Error: The following reference folder does not exist:\n%s', refFolder);
uiwait(warndlg(errorMessage));
return;
end
% Read in the reference image.
fullRefImageFileName = fullfile(refFolder, 'whatever.png');
if ~exist(fullRefImageFileName, 'file')
errorMessage = sprintf('Error: The following reference image does not exist:\n%s', fullRefImageFileName);
uiwait(warndlg(errorMessage));
return;
else
refImage = imread(fullRefImageFileName);
end
% Specify the folder where the test image files live.
testFolder = pwd; % or wherever, such as 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(testFolder)
errorMessage = sprintf('Error: The following test image folder does not exist:\n%s', testFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(testFolder, '*.PNG'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(testFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Read in test image.
testImage = imread(fullFileName);
imshow(testImage); % Display image.
drawnow; % Force display to update immediately.
% Now do whatever you want with this file name,
% such as comparing the image array with refImage in some function you write.
results = CompareImages(testImage, refImage);
end
Adapt as needed. Write back if there are any problems with it.
Remember, the CompareImages() function in there is something you have to write because we don't know how you want to compare the test images to the reference image.

9 commentaires

You should use isequal() instead of corr2().
Also, what are (the very badly-named) "A" and "D"? They don't seem to be defined anywhere? Why not? Why is an error not thrown for that?
Again, instead of this:
c = corr2(image1,image2);
use this
c = isequal(image1, image2);
@Image Analyst : Thank you for your response.
Hi @Image Analyst
I have "Test" folder which contains resized images (64x64) with which, I need to plot the test image with the most similar images in the “Alphabets” folder
Below is the hint given
(hint: you concatenate all image pixels to form 4096-dimension feature for each image. Then you compute the distance between each test image and all Alphabets images. Next, you plot the Alphabets image with the smallest distance to the test image)
can you please help ?
Sneha P
Sneha P le 14 Mai 2022
Can you please help in compare images() part , I have to compare a image with the 10 different images inside a single folder
Now reading C:\Users\Sneha\Desktop\MATLAB\video_extract\Images1.jpg
Unrecognized function or variable 'CompareImages'.
@Sneha P your CompareImages() function is not in the same folder as your script or anywhere on the path. Did you even write it yet? There is no generic built in function to compare images since there could be a million ways to do it and everyone might have a different method. What was your method? Can you attach your CompareImages() m-file or function?
Sneha P
Sneha P le 15 Mai 2022
Hi @Image Analyst Iam a begginer and assigned with project
% Specify the folder where the reference image file lives.
refFolder = 'C:\Users\Sneha\Desktop\MATLAB\video_extract'; % or wherever, such as 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(refFolder)
errorMessage = sprintf('Error: The following reference folder does not exist:\n%s', refFolder);
uiwait(warndlg(errorMessage));
return;
end
% Read in the reference image.
fullRefImageFileName = fullfile(refFolder, 'find_person2.jpg');
if ~exist(fullRefImageFileName, 'file')
errorMessage = sprintf('Error: The following reference image does not exist:\n%s', fullRefImageFileName);
uiwait(warndlg(errorMessage));
return;
else
refImage = imread(fullRefImageFileName);
end
% Specify the folder where the test image files live.
testFolder = 'C:\Users\Sneha\Desktop\MATLAB\video_extract\'; % or wherever, such as 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(testFolder)
errorMessage = sprintf('Error: The following test image folder does not exist:\n%s', testFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(testFolder, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(testFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Read in test image.
testImage = imread(fullFileName);
imshow(testImage); % Display image.
drawnow; % Force display to update immediately.
% Now do whatever you want with this file name,
% such as comparing the image array with refImage in some function you write.
results = CompareImages(testImage, refImage);
end
Sneha P
Sneha P le 15 Mai 2022
results = CompareImages(testImage, refImage);
In place of this if i use
results = isequal(testImage, refImage); --> It's reading all images in folder instead comparing testimage folder and refimage
I'm not seeing a question there, so I'll just expand on that statement.
For one call it just uses isequal on a pair of images. Of course that function gets called in a loop so eventually it will compare all images.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by