How can I compare one image to multiple images?
2 views (last 30 days)
Show older comments
KH TOHIDUL ISLAM
on 3 Nov 2021
Commented: KH TOHIDUL ISLAM
on 23 Nov 2021
For example, I have 200 images in a cell array I=1x200 cells. I want to compare image 1st with the other 199 images to the last image 200th with the other 199 images. I wrote a manual code for ten images, but is there any most straightforward way to do this? Here I used SSIM.
%% Save Images to the Directory
I={};
for kk = 1:NumberOfImages
I{kk} = imread(augimdsFolderLocation.Files{bigidxK(kk)});
end
%% Compare Images
SSIM_I_1=[];
SSIM_I_2=[];
SSIM_I_3=[];
SSIM_I_4=[];
SSIM_I_5=[];
SSIM_I_6=[];
SSIM_I_7=[];
SSIM_I_8=[];
SSIM_I_9=[];
SSIM_I_10=[];
for kk = 1:NumberOfImages
SSIM_I_1(kk,1) = ssim(I{1},I{kk});
SSIM_I_2(kk,1) = ssim(I{2},I{kk});
SSIM_I_3(kk,1) = ssim(I{3},I{kk});
SSIM_I_4(kk,1) = ssim(I{4},I{kk});
SSIM_I_5(kk,1) = ssim(I{5},I{kk});
SSIM_I_6(kk,1) = ssim(I{6},I{kk});
SSIM_I_7(kk,1) = ssim(I{7},I{kk});
SSIM_I_8(kk,1) = ssim(I{8},I{kk});
SSIM_I_9(kk,1) = ssim(I{9},I{kk});
SSIM_I_10(kk,1) = ssim(I{10},I{kk});
end
%% Remove self comparsion
SSIM_I_1(1,1)=0;
SSIM_I_2(2,1)=0;
SSIM_I_3(3,1)=0;
SSIM_I_4(4,1)=0;
SSIM_I_5(5,1)=0;
SSIM_I_6(6,1)=0;
SSIM_I_7(7,1)=0;
SSIM_I_8(8,1)=0;
SSIM_I_9(9,1)=0;
SSIM_I_10(10,1)=0;
%% Compare
[Maximum_Image_1,Index_Image_1] = max(SSIM_I_1);
[Maximum_Image_2,Index_Image_2] = max(SSIM_I_2);
[Maximum_Image_3,Index_Image_3] = max(SSIM_I_3);
[Maximum_Image_4,Index_Image_4] = max(SSIM_I_4);
[Maximum_Image_5,Index_Image_5] = max(SSIM_I_5);
[Maximum_Image_6,Index_Image_6] = max(SSIM_I_6);
[Maximum_Image_7,Index_Image_7] = max(SSIM_I_7);
[Maximum_Image_8,Index_Image_8] = max(SSIM_I_8);
[Maximum_Image_9,Index_Image_9] = max(SSIM_I_9);
[Maximum_Image_10,Index_Image_10] = max(SSIM_I_10);
0 Comments
Accepted Answer
yanqi liu
on 4 Nov 2021
clc; clear all; close all;
db = fullfile(matlabroot,'toolbox\images\imdata');
files = ls(fullfile(db, '*.jpg'));
NumberOfImages = min(5,size(files,1));
%% Save Images to the Directory
I={};
for kk = 1:NumberOfImages
I{kk} = imread(fullfile(db, strtrim(files(kk,:))));
end
%% Compare Images
SSIM_A = [];
for kk = 1:NumberOfImages
for ss = 1:NumberOfImages
if kk == ss
continue;
end
a = I{kk}; b = I{ss};
if ndims(a) == 2
a = cat(3, a, a, a);
end
if ndims(b) == 2
b = cat(3, b, b, b);
end
if ~isequal(size(a), size(b))
b = imresize(b, [size(a,1) size(a,2)], 'bilinear');
end
if ~isa(a,'uint8')
a = im2uint8(a);
end
if ~isa(b,'uint8')
b = im2uint8(b);
end
SSIM_A(kk,ss) = ssim(a,b);
end
end
%% Remove self comparsion
%% Compare
for kk = 1:NumberOfImages
[Maximum_Image_kk,Index_Image_kk] = max(SSIM_A(kk,:));
fprintf('\n%d image, the max ssim is %d and the value is %.2f', kk, Index_Image_kk, Maximum_Image_kk);
end
result
1 image, the max ssim is 5 and the value is 0.17
2 image, the max ssim is 4 and the value is 0.18
3 image, the max ssim is 2 and the value is 0.15
4 image, the max ssim is 5 and the value is 0.24
5 image, the max ssim is 4 and the value is 0.24
More Answers (2)
DGM
on 3 Nov 2021
Edited: DGM
on 3 Nov 2021
Consider the example:
% test images
A = imread('cameraman.tif');
I = {A,imnoise(A,'gaussian'),imnoise(A,'gaussian'),imnoise(A,'gaussian'),imnoise(A,'gaussian')};
NumberOfImages = numel(I);
% build ssim for all images, skipping self-comparison
allssim = nan(NumberOfImages);
for kest = 1:NumberOfImages
for kref = 1:NumberOfImages
if kref ~= kest
allssim(kref,kest) = ssim(I{kest},I{kref});
end
end
end
allssim
maxssim = max(allssim,[],1) % maximum non-self similarity for each image
Note that representing the self-ssim as nan on the diagonal isn't accurate (the self-ssim would be 1), but using a placeholder facilitates the maximization, and those values are otherwise not used.
Also, if it's desired to find the index of the image with the highest ssim for a given reference, use the two-argument output syntax for max().
Image Analyst
on 23 Nov 2021
You probably should not read all of the images into a cell array. You may run low on memory. Read in the first image, then have a loop over the rest of the images. You can put all your ssim processing into the loop and just overwrite the current image when you read it in. It's just common sense management of memory. No need for separate loops to read all the images in to a cell array and then another loop to process them.
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!