필터 지우기
필터 지우기

How to compare images from 2 folders

조회 수: 3 (최근 30일)
Fateme Jalali
Fateme Jalali 2016년 7월 30일
댓글: 90Anonymous 2018년 12월 9일
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에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by