How to compare images from 2 folders

1 view (last 30 days)
Hello.I have 2 folders of images and want to compare each image in folder 1 with each image in folder 2 in order to find almost similar images. I wrote the code but it can not open images in for loop.can any one help me plz? Here is my code:
doc1 = dir('E:\fake1\*.jpg');
doc2 = dir('E:\fake2\*.jpg');
d_1 = size(doc1);
d_2 = size(doc2);
Inputs1 = {doc1.name}';
Inputs2 = {doc2.name}';
for I1 = 1:d_1;
for I2 = 1:d_2;
X_1 = imread([doc1 Inputs1{I1}]);
X_2 = imread([doc2 Inputs2{I2}]);
compare_images = imabsdiff(X_1,X_2);
figure;
imshow(compare_images);
title('compare_images');
%comp1 = imshowpair(X_1,X_2,'diff');
end
end

Accepted Answer

Image Analyst
Image Analyst on 30 Jul 2016
Fateme, this is in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F Look at the second code block.

More Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 30 Jul 2016
Edited: Azzi Abdelmalek on 30 Jul 2016
X_1 = imread(fullfile(doc1,Inputs1{I1}))
%or
X_1 = imread([doc1 '\' Inputs1{I1}])
  1 Comment
Fateme Jalali
Fateme Jalali on 30 Jul 2016
It does not work and error exists.this is the error: Undefined function or method 'eq' for input arguments of type 'struct'.
Error in ==> fullfile at 37 if (f(end)==fs) && (part(1)==fs),
Error in ==> COMPAREimages9mordad at 19 X_1 = imread(fullfile(doc1,Inputs1{I1}));

Sign in to comment.


Walter Roberson
Walter Roberson on 31 Jul 2016
first_dir = 'E:\fake1';
second_dir = 'E:\fake2';
doc1 = dir(fullfile(first_dir, '*.jpg'));
doc2 = dir(fullfile(second_dir, '*.jpg'));
d_1 = length(doc1);
d_2 = length(doc2);
Inputs1 = {doc1.name}';
Inputs2 = {doc2.name}';
for I1 = 1:d_1
first_base = Inputs1{I1};
first_file = fullfile(first_dir, first_base);
X_1 = imread(first_file);
for I2 = 1:d_2
second_base = Inputs2{I1};
second_file = fullfile(second_dir, second_base);
X_2 = imread(second_file);
compare_images = imabsdiff(X_1,X_2);
figure;
imshow(compare_images);
title( sprintf('"%s" vs "%s"', second_base, first_base) );
%comp1 = imshowpair(X_1,X_2,'diff');
end
end
  2 Comments
Image Analyst
Image Analyst on 7 Dec 2018
Try this:
first_dir = 'D:\My Pictures\Misc';
second_dir = 'D:\My Pictures\Wallpapers';
fileList1 = dir(fullfile(first_dir, '*.jpg'));
fileList2 = dir(fullfile(second_dir, '*.jpg'));
numFiles1 = length(fileList1);
numFiles2 = length(fileList2);
baseFileNames1 = {fileList1.name}';
baseFileNames2 = {fileList2.name}';
for I1 = 1:numFiles1
firstBaseName = baseFileNames1{I1};
firstFullFileName = fullfile(first_dir, firstBaseName);
image1 = imread(firstFullFileName);
for I2 = 1:numFiles2
fprintf('\nComparing image #%d of %d in folder 1 to #%d of %d in folder 2.\n', ...
I1, numFiles1, I2, numFiles2);
secondBaseName = baseFileNames2{I2};
secondFullFileName = fullfile(second_dir, secondBaseName);
image2 = imread(secondFullFileName);
if isequal(size(image1), size(image2))
compare_images = imabsdiff(image1,image2);
% Sizes match. Subtract them and display the differences.
figure;
imshow(compare_images);
title( sprintf('"%s" vs "%s"', secondBaseName, firstBaseName) );
%comp1 = imshowpair(X_1,X_2,'diff');
else
% Sizes do NOT match. Print out that they don't match, then continue.
fprintf(' Not comparable because sizes do not match.\n');
[rows, columns, numColorChannels] = size(image1);
fprintf(' %s = %d rows by %d columns by %d color channels.\n', firstFullFileName, rows, columns, numColorChannels);
[rows, columns, numColorChannels] = size(image2);
fprintf(' %s = %d rows by %d columns by %d color channels.\n', secondFullFileName, rows, columns, numColorChannels);
end
end
end
90Anonymous
90Anonymous on 9 Dec 2018
@Image Analyst: Thank you so much for your support. It works fine now; for both scenarios.
Thanks again!

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!