How to open multiple folders and compare the images?
9 views (last 30 days)
Show older comments
This is how my code is right now. It can process images from one folder into a binary image and then show it. However, I need to process images from two seperate folders into binary images and then compare images from each folder to create one final binary image. For example, (images are all the same size) a 0 in one image multipled by a 0 in a different image will create a 0, a 1 multipled by a 1 will create a 1, a 1 multipled by a 0 will create a 0. And thus similarites within images are found.
So far this is the code.
clear;clc;
path = uigetdir(); %open directory
if isequal(path,0)
disp('User selected Cancel');
return
else
disp(['User selected ', (path)]);
end
% pathPattern = fullfile(path);
theFiles = dir(path); % Get list of all files with desired pattern
for k = 3 : 27 % Change length when needed
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName)
fprintf(1,'Now reading %s\n', fullFileName)
a{k-2} = imread(fullFileName); % Read the image
a_gray = im2gray(a{k-2});
BW = imbinarize(a_gray,0.02); % The threshold can be set depending on file
figure
imshow(BW);
end
Is it possible to do this? Any type of help, such as leading me in the right direction or links to forums are much appreciated. If there is any confusion with what I am asking please ask me to clarify.
0 Comments
Answers (1)
Abhinav Aravindan
on 13 Dec 2024
It seems like you want to find the similarities between sets of images located in different folders. You can achieve this by obtaining the folder paths using “uigetdir” for each of the folders, as implemented in your code and perform logical "AND" operation to obtain the similarity between two images. Here is a sample code snippet for your reference:
% Select the folders
path1 = uigetdir();
if isequal(path1, 0)
disp('User selected Cancel');
return;
else
disp(['User selected ', path1]);
end
path2 = uigetdir();
if isequal(path2, 0)
disp('User selected Cancel');
return;
else
disp(['User selected ', path2]);
end
% Get list of files in each folder assuming the images are PNG format
files1 = dir(fullfile(path1, '*.png'));
files2 = dir(fullfile(path2, '*.png'));
% Assuming both folders have same number of images
numImages = length(files1);
for k = 1:numImages
img1 = imread(fullfile(files1(k).folder, files1(k).name));
img2 = imread(fullfile(files2(k).folder, files2(k).name));
BW1 = imbinarize(im2gray(img1), 0.02);
BW2 = imbinarize(im2gray(img2), 0.02);
% Logical AND to find similarities
similarityImage = BW1 & BW2;
% Display the result
figure;
imshow(similarityImage);
title(sprintf('Similarity Image %d', k));
end
You may refer to the below documentation for further reference:
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!