compare one image with an aray of images

1 view (last 30 days)
i have a set of 11 images in a folder, i have to choose an image in random and check whether the choosen image is equal to any one of the 11 images and get the output as "which image is equal to the choosen image".
myFolder = 'C:\Users\hp\Desktop\final image';
filePattern = fullfile(myFolder, '*.tif');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
Z = imread(fullFileName);
end
[a,b]=imgetfile;
I=imread(a);
for i=1:length(Z)
if (I==imread(Z{i}))
disp('YES');
else
disp('NO')
break
end
end
disp(x)
this code shows an error like this
kindly help me in resolving the issue

Accepted Answer

Image Analyst
Image Analyst on 25 Apr 2020
No need for a cell array int he first place. No need to read all those images into memory, which might cause you to run out of memory if you had a lot of images. Just get the random number, read in that image, then loop over all other images using isequal() to look for a match. Fixed code is below:
fprintf('Beginning to run %s.m ...\n', mfilename);
fontSize = 14;
myFolder = pwd; %'C:\Users\hp\Desktop\final image';
filePattern = fullfile(myFolder, '*.tif');
imageFiles = dir(filePattern);
numberOfImages = length(imageFiles)
randomNumber = randi(numberOfImages)
% Get that random image.
baseRefFileName = imageFiles(randomNumber).name;
refFullFileName = fullfile(myFolder, baseRefFileName);
referenceImage = imread(refFullFileName);
% Display reference image.
subplot(1, 2, 1);
imshow(referenceImage);
caption = sprintf('%s', baseRefFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'none');
% Loop over all images.
for k = 1:length(imageFiles)
baseFileName = imageFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s to see if it matches image %d\n', fullFileName, randomNumber);
thisImage = imread(fullFileName);
% Display test image.
subplot(1, 2, 2);
imshow(thisImage);
drawnow;
if isequal(referenceImage, thisImage)
fprintf('\t"%s" matches "%s".\n', baseRefFileName, baseFileName);
caption = sprintf('Match! %d of %d : %s', k, length(imageFiles), baseRefFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'none');
if k < length(imageFiles)
pause(2); % Delay a little bit so we can see it, unless it's the very last image.
end
else
fprintf('\t"%s" does NOT match "%s".\n', baseRefFileName, baseFileName);
caption = sprintf('No Match! %d of %d : %s', k, length(imageFiles), baseRefFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'none');
pause(0.4); % Delay a little bit so we can see it.
end
end
fprintf('Done running %s.m .\n', mfilename);
  2 Comments
Jishnoop JAYAPRAKASH
Jishnoop JAYAPRAKASH on 25 Apr 2020
thanks for the answer, actually i have to randomly choose the input image, and get which image is perfectly matched. but this is an elaborate answer which suits for handling large amount of images.
Image Analyst
Image Analyst on 25 Apr 2020
The input image was chosen. You said to choose one at random -- " i have to choose an image in random" -- and that's what exactly what I did when I called randi(). And using isequal() does the comparison pixel by pixel. So this perfectly meets your needs. If you want to simplify it (and make it less user friendly) you could get rid of the lines of code where I display the images and titles, get rid of the calls to pause() (not needed if you're not displaying anything), and get rid of fprintf() which displays stuff in the command window. So the code will run but you basically won't see anything and won't know if it worked, unless you leave in at least one of the fprintf()'s.

Sign in to comment.

More Answers (1)

David Hill
David Hill on 25 Apr 2020
myFolder = 'C:\Users\hp\Desktop\final image';
filePattern = fullfile(myFolder, '*.tif');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
Z{k} = imread(fullFileName);%need to store in cell array
end
[a,b]=imgetfile;
I=imread(a);
for i=1:length(Z)
if isequal(I,Z{i})%should use isequal
disp('YES');
else
disp('NO')
break
end
end
disp(x)
  1 Comment
Jishnoop JAYAPRAKASH
Jishnoop JAYAPRAKASH on 25 Apr 2020
Edited: Jishnoop JAYAPRAKASH on 25 Apr 2020
thanks for the answer, the code works , but it stops when the answer is no, if the first image mismatches it stops there, can you help it out, i want the value of " i " or which image is matched as output.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!