Compare each image with another

4 次查看(过去 30 天)
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 个评论
KALYAN ACHARJYA
KALYAN ACHARJYA 2019-7-2
@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 2019-7-2
Yes, I have to find ssim() between all the images.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2019-7-2
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 个评论
Image Analyst
Image Analyst 2019-7-14
I'm attaching a kmeans demo for grayscale and color. Adapt as needed. Good luck. ?️
Abhishek Singh
Abhishek Singh 2019-7-14
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.

请先登录,再进行评论。

更多回答(1 个)

Anu
Anu 2022-1-16
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 个评论
Image Analyst
Image Analyst 2022-1-16
@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 2022-1-16
@Image Analyst Ohh I tried this before but without removing \n, and it didn't work. Now it makes sense and thanks so much!

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by