How to compare images from 2 folders

1 次查看(过去 30 天)
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

采纳的回答

Image Analyst
Image Analyst 2016-7-30
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.

更多回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2016-7-30
编辑:Azzi Abdelmalek 2016-7-30
X_1 = imread(fullfile(doc1,Inputs1{I1}))
%or
X_1 = imread([doc1 '\' Inputs1{I1}])
  1 个评论
Fateme Jalali
Fateme Jalali 2016-7-30
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}));

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2016-7-31
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 个评论
Image Analyst
Image Analyst 2018-12-7
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 2018-12-9
@Image Analyst: Thank you so much for your support. It works fine now; for both scenarios.
Thanks again!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by