How to calculate the correlation matrix of two jpg images of the same size?
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, I'm trying to calculate the correlation matrix of two jpg images of the same size (125x60 pixels) using the following code.
But it gives me 2x2 matrix as the result. shouldn't it be a 125x 60 matrix?
% Load the two images
img1 = imread('(iii).jpg');
img2 = imread('(vii).jpg');
size(img1)
size(img2)
% Convert the images to grayscale if necessary
if size(img1, 3) == 3
img1 = rgb2gray(img1);
end
if size(img2, 3) == 3
img2 = rgb2gray(img2);
end
% Check the dimensions of the images
if ~isequal(size(img1), size(img2))
error('Images must have the same dimensions.');
end
% Compute the correlation matrix
corr_mat = corrcoef(double(img1(:)), double(img2(:)))
% Display the images and the correlation matrix
figure;
subplot(1,3,1);
imshow(img1);
title('Image 1');
subplot(1,3,2);
imshow(img2);
title('Image 2');
subplot(1,3,3);
imagesc(corr_mat);
colorbar;
title('Correlation Matrix');
0 commentaires
Réponses (1)
the cyclist
le 16 Mar 2023
Modifié(e) : the cyclist
le 16 Mar 2023
The result is what I would expect. Imagine this smaller example, with two images of 9 random pixels:
rng default
N = 3;
img1 = rand(N);
img2 = rand(N);
figure
tiledlayout(1,2)
nexttile
imshow(img1)
nexttile
imshow(img2)
What is the correlation? There are not 9 separate correlations. It does not make sense to ask if the upper left corner of each image is correlated. There is just one correlation coefficient, which is the correlation of the two 9x1 vectors that represent the images:
corrcoef(img1(:),img2(:))
The upper-right (or symmetric lower-left) value is the correlation coefficient. (The diagonal is the self-correlation, which is 1 by construction.)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!