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 comentarios

@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 el 2 de Jul. de 2019
Yes, I have to find ssim() between all the images.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 2 de Jul. de 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 comentarios

Abhishek Singh
Abhishek Singh el 2 de Jul. de 2019
Editada: Abhishek Singh el 2 de Jul. de 2019
Hi, thank you for the reply. Ignore my previous comment I am editing it. So the files are currently 25 images in number and I am creating a 25*25 matrix of ones and then filling it with ssim(). I ran the code by you basically nothing just appended this what I had which was also from you and it nearly took 15 minutes and returns the matrix filled by ssim values. Do you also have any idea how can I make it faster and separate them or save them into folders by segragating them into 4 classes?
One more question, what does adding symmetry does? I mean I am getting two allSsim outputs with the same result.
Image Analyst
Image Analyst el 2 de Jul. de 2019
If you have 25*24/2 = 300 comparisons, yes it might take a while.
I don't know what criteria you want to use to segrate the images into 4 class folders.
Abhishek Singh
Abhishek Singh el 2 de Jul. de 2019
Yes, I agree with you but I think my primary concern would be the code to run rather than the time. I ran the code and now I have 25*25 matrix with me where the upper diagonal or lower diagonal has the ssim values. I want to check all these values with conditions if they are greater than 0.7 or greater than 0.8 or greater than 0.9 so it basically turns out to be 4 groups which are: <0.7, 0.7-0.8, 0.8-0.9, >0.9.
Image Analyst
Image Analyst el 3 de Jul. de 2019
Well, you have all the ssim values. Just check them. But what happens the sim between image15 and image22 is 0.5, but between image15 and image24 is 0.75. Which class would image15 go into? Or do the classes apply to PAIRS rather than individual images, and if so, you just need to quantize your allSsim array which you can do with imquantize().
Abhishek Singh
Abhishek Singh el 3 de Jul. de 2019
Yes. I meant how can I check each matrix position (x,y). And also what you said makes sense I totally missed it. What I have to do is if the images have good relation that is good ssim() values I have to somehow categorize them in one group and that is why I was trying to do something like that and your point makes sense that it could not be done so sorry for that. Could you tell me something which would be more impactful and would make more sense and can be applied from the current position in code.
Abhishek Singh
Abhishek Singh el 5 de Jul. de 2019
Hi. I thought about it and I have a new strategy which could hopefully work. I will start with the first row which is Image 1 and give a condition like >0.9 only images to be grouped with Image 1 and will find the least value in the row which should also be <0.9 and then directly go to that image's row number. Will perform the same thing for that image and again seek the lowest related image. Will keep reiterating this and in the end I will end the loop on the condition where all images are at leaast gruoped. Do you think it will work?
Image Analyst
Image Analyst el 5 de Jul. de 2019
No. Let's say image1 was grouped with images 2, 3, and 4 but not with 5. So then you go to row 5 and find image5 is grouped with images 3 and 4. So now images 3 and 4 are both grouped in two groups. So I think the only way to group these is to group by pairs, not by individual images. Because I think that image 1 might be like image 2, and image 2 might be like image 3 but image 1 might not be like image 3.
Abhishek Singh
Abhishek Singh el 5 de Jul. de 2019
I think you misunderstood me. If lets say in the first iteration out of all the 25 images 2 3 4 are grouped with 1 and 5 has the least value. Now when I do the iteration for image 5 I will not iterate for the images 1 2 3 4 since they are already grouped. I know this is not exactly correct way but there are high chances from what I have seen till now that in image 5 iteration its association would not be very good with 2,3,4 since it had not with 1 (and 1 had a good ssim with 2 3 4). Makes sense?
Abhishek Singh
Abhishek Singh el 8 de Jul. de 2019
I was also wondering that if I have a certain feature (like RBG value for example) of each images then since I have independent values now could I use kmeans() to cluster them? If yes, do you know any feature that would be best suited in this case?
Abhishek Singh
Abhishek Singh el 14 de Jul. de 2019
Editada: Abhishek Singh el 14 de Jul. de 2019
1.0000 0.9258 0.9175 0.9080 0.8322
0.9258 1.0000 0.9321 0.9106 0.8511
0.9175 0.9321 1.0000 0.8798 0.8911
0.9080 0.9106 0.8798 1.0000 0.8025
0.8322 0.8511 0.8911 1.0000 1.0000
Hi,
I am not sure if there is any thing in the code which is making the 5th row, 4th column entry as always 1 whichever image I select. Rest everything seems correct. It is always happening whatever may the matrix size be with with last row and second last column entry.
Image Analyst
Image Analyst el 14 de Jul. de 2019
I'm attaching a kmeans demo for grayscale and color. Adapt as needed. Good luck. ?️
Abhishek Singh
Abhishek Singh el 14 de Jul. de 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.

Iniciar sesión para comentar.

Más respuestas (1)

Anu
Anu el 16 de En. de 2022

0 votos

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 comentarios

It seems like subtitle does not work when there is an image in the axes. It works for plot() but not with imshow(). So you'll just have to have two lines.
figure();
imshow(im2double(ssimmap),[])
caption = sprintf('Local SSIM Map with Global SSIM Value: %.4f', ssimval);
title(caption)
subcaption = sprintf('%s\nk = %d, k2 = %d', caption, k, k2);
% subtitle(subcaption)
title(subcaption)
Anu
Anu el 16 de En. de 2022
Thanks, @Image Analyst, for the insights. However, this above code gives me respective k and k2 values in each figure, not anything for the 'Local SSIM Map with Global SSIM Value: %.4f', ssimval.
I was wondering if it is possible to get the output all together in a title. I actually don't need the subtitle, but I am afraid if the title is too lengthy that it goes beyond the size of the figure.
Thanks
Image Analyst
Image Analyst el 16 de En. de 2022
Exactly what do you consider the "output" and how can you put that into a string that is the title? Please show an example of what you'd like the title to look like.
Anu
Anu el 16 de En. de 2022
Sorry for the confusion. If k=1, and k2 = 2, and ssimval = 0.876, all I want to show in the title of the figure is:
Local SSIM Map with Global SSIM Value: 0.876 for k = 1 and k2 = 2
Since the images are shown in a for loop, the SSIM value, k, k2 will change for the respective cases.
Hope this clears what I want to print in the title of the figure.
@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 el 16 de En. de 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!

Iniciar sesión para comentar.

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Preguntada:

el 2 de Jul. de 2019

Comentada:

Anu
el 16 de En. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by