Compare each image with another

4 views (last 30 days)
Abhishek Singh
Abhishek Singh on 2 Jul 2019
Commented: Anu on 16 Jan 2022
As suggested by @Image Analyst to read images from the system to workspace I am using his code he answered on a different question.
folder = 'D:\My Pictures\whatever'
filePattern = fullfile(folder, '*.jpg');
f=dir(filePattern)
files={f.name}
for k=1:numel(files)
fullFileName = fullfile(folder, files{k})
cellArrayOfImages{k}=imread(fullFileName)
end
Now I want to compare each image in my workspace to other image using the score ssim(). I have to categorize those images into 4 groups dependent on the score (0.7,0.9) using ifelse and also then save them into different folders in my system which would be great. Please let me know how can I do this which would also not slow the system as I have lots of images. Thanks in advance.
  2 Comments
KALYAN ACHARJYA
KALYAN ACHARJYA on 2 Jul 2019
@Abhishake Suppose you have called the image1,
Now, you have to find SSIM in between image1 and ??? Please clarify?
group1={};group2={}...... %Preallocation
n=1,m=1.....
if SSIM_value==0.7
group1{n}=current_image
n=n+1;
elseif
...
So on....
Abhishek Singh
Abhishek Singh on 2 Jul 2019
Yes, I have to find ssim() between all the images.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 2 Jul 2019
I suggest you don't store all the images in a cell array because you might run out of memory if you have a lot of them. I'd just use a double for loop:
numImages = numel(files);
allSsim = ones(numImages, numImages);
for k = 1 : numImages - 1
fullFileName1 = fullfile(folder, files{k});
image1 = imread(fullFileName1);
for k2 = k + 1 : numImages
fullFileName2 = fullfile(folder, files{k2});
image2 = imread(fullFileName2);
allSsim(k, k2) = ssim(image1, image2) % Compute ssim
allSsim(k2, k) = allSsim(k, k2); % Make symmetric.
end
end
  12 Comments
Image Analyst
Image Analyst on 14 Jul 2019
I'm attaching a kmeans demo for grayscale and color. Adapt as needed. Good luck. ?️
Abhishek Singh
Abhishek Singh on 14 Jul 2019
Thanks a lot for your tremendous help and effort. So far your previous algorithm is also working with only small erroneous result I mentioned just an hour ago. Do you know why that should happen? I will look at the mat files you have attached. Thank you once again.

Sign in to comment.

More Answers (1)

Anu
Anu on 16 Jan 2022
Thanks, @Image Analyst, The code is really helpful.
folder = 'C:\Users\anusu\Downloads\figures\New folder';
filePattern = fullfile(folder, '*.tif');
f=dir(filePattern);
files={f.name};
numImages = numel(files);
allSsim = ones(numImages, numImages);
for k = 1 : numImages - 1
fullFileName1 = fullfile(folder, files{k});
image1 = imread(fullFileName1);
for k2 = k + 1 : numImages
fullFileName2 = fullfile(folder, files{k2});
image2 = imread(fullFileName2);
[ssimval,ssimmap] = ssim(image1,image2);
figure();
imshow(ssimmap,[])
title('Local SSIM Map with Global SSIM Value:'+num2str(ssimval))
subtitle(sprintf('%d',k, k2))
fileC = fullfile(folder, sprintf('SSIM%d.png', k, k2));
saveas(gcf, fileC);
allSsim(k, k2) = ssim(image1, image2); % Compute ssim
allSsim(k2, k) = allSsim(k, k2); % Make symmetric.
end
end
The above code works perfectly except the subtitle. May I know how to print the k, k2 values in a figure title so that I understand which images are compared to draw the map? Any suggestion/help would be appreciated. Thanks.
  6 Comments
Image Analyst
Image Analyst on 16 Jan 2022
@Anu so simply get rid of the backslash n
folder = 'C:\Users\anusu\Downloads\figures\New folder';
filePattern = fullfile(folder, '*.tif');
f=dir(filePattern);
files={f.name};
numImages = numel(files);
allSsim = ones(numImages, numImages);
for k = 1 : numImages - 1
fullFileName1 = fullfile(folder, files{k});
image1 = imread(fullFileName1);
for k2 = k + 1 : numImages
fullFileName2 = fullfile(folder, files{k2});
image2 = imread(fullFileName2);
[ssimval,ssimmap] = ssim(image1,image2);
figure();
imshow(im2double(ssimmap),[])
impixelinfo;
caption = sprintf('Local SSIM Map with Global SSIM Value: %.4f for k = %d, k2 = %d', ssimval, k, k2);
title(caption)
fileC = fullfile(folder, sprintf('SSIM%d.png', k, k2));
saveas(gcf, fileC);
allSsim(k, k2) = ssim(image1, image2); % Compute ssim
allSsim(k2, k) = allSsim(k, k2); % Make symmetric.
end
end
Anu
Anu on 16 Jan 2022
@Image Analyst Ohh I tried this before but without removing \n, and it didn't work. Now it makes sense and thanks so much!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!