The Pearson correlation coefficient
In statistics, the Pearson correlation coefficient, also known as the Pearson product-moment correlation coefficient (PPMCC or PCCs), is used to measure the relationship between two variables X and Y. Correlation (linear correlation) with a value between -1 and 1.
For calculation steps, refer to Section VII Correlation of the following article
Second, matlab calculation correlation
1. Example 1
According to the formula, A and B are highly correlated. The correlation coefficient is 1.
A= [1 4 7; 2 5 8; 3 6 9]
B = A*2
%Find the average of the matrix A
meanA = mean2(A);
%Find the average of the matrix B
meanB = mean2(B);
%Subtract the average value from matrix A
Asub = A-meanA;
%Subtract the average value from matrix B
Bsub = B-meanB;
%Covariance of matrix A and matrix B
covAB = mean2(Asub.*Bsub);
%Find the standard deviation of the matrix A
stdA = std(A(:),1);
%Find the standard deviation of the matrix B
stdB = std(B(:),1);
%Find the correlation Cofficient
Rho = covAB./(stdA*stdB)
2. Example 2
Using the corr2 function, the C and D correlation coefficients are -1.
C= [1 4 7; 2 5 8; 3 6 9]
D = [9 6 3;8 5 2; 7 4 1];
Rho = corr2(C,D)
3. Template matching example
1. Use normxcorr2 function
Reference Code
%Read an Image A(Template)
A1 = imread('benten.jpg');
%Read the Target Image
B1 = imread('watch.jpg');
A = A1(:,:,1);
B = B1(:,:,1);
normx_corrmap=normxcorr2(B(:,:,1),A(:,:,1));
maxptx = max(normx_corrmap(:));
[x1,y1]=find(normx_corrmap==maxptx);
figure,
imagesc(A1(x1-size(B,1):x1,y1-size(B,2):y1,:));axis image
The running result is as follows
2. Use the corr2 function
Reference Code
%Read an Image A(Template)
A1 = imread('图片/benten.jpg');
%Read the Target Image
B1 = imread('图片/watch.jpg');
A = A1(:,:,1);
B = B1(:,:,1);
corr_map = zeros([size(A,1),size(A,2)]);
for i = 1:size(A,1)-size(B,1)
for j = 1:size(A,2)-size(B,2)
%Construct the correlation map
corr_map(i,j) = corr2(A(i:i+size(B,1)-1,j:j+size(B,2)-1),B);
end
end
figure,imagesc(corr_map);colorbar;
%Find the maximum value
maxpt = max(corr_map(:));
[x,y]=find(corr_map==maxpt);
%Display the image from the template
figure,imagesc(B1);title('Target Image');colormap(gray);axis image
grayA = rgb2gray(A1);
Res = A;
Res(:,:,1)=grayA;
Res(:,:,2)=grayA;
Res(:,:,3)=grayA;
Res(x:x+size(B,1)-1,y:y+size(B,2)-1,:)=A1(x:x+size(B,1)-1,y:y+size(B,2)-1,:);
figure,imagesc(Res);
The running result is as follows
3. Frequency domain template matching
Based on normalized cross-correlation in the Fourier domain. Also known as phase correlation. The two pictures used here are different snapshots of the same scene. Image1.jpg is used as the template image and a subimage of Image2.jpg is used as the target image. The destination image is padded with zeros to match the size of the template image. After Fourier transform, the template signal is multiplied by the conjugate of the target signal and normalized. The inverse Fourier is then applied to extract the pixel location corresponding to the maximum value.
%Read two images of same scene
A = imread('Image1.jpg');
B = imread('Image2.jpg');
figure,subplot(2,1,1);imagesc(A);title('Image 1');axis image
subplot(2,1,2);imagesc(B);title('Image 2');axis image
crop a part from image matrix B
B = imcrop(B,[58.5 49.5 226 102]);
figure,imagesc(B);title('sub Image - Image 2');axis image
Apply Fourier and Inverse Transforms
%Pad the image matrix B with zeros
B1 = zeros([size(A,1),size(A,2)]);
B1(1:size(B,1),1:size(B,2))=B(:,:,1);
%Apply Fourier Transform
Signal1 = fftshift(fft2(A(:,:,1)));
Signal2 = fftshift(fft2(B1));
%Mulitply Signal1 with the conjugate of Signal2
R = Signal1 .*conj(Signal2);
%Normalize the result
Ph = R./abs(R);
%Apply inverse fourier transform
IFT = ifft2(fftshift(Ph));
figure,imagesc((abs((IFT))));colormap(gray);
Find the pixel position of the maximum value and take a cut from image1 and image2
%Find the maximum value
maxpt = max(real(IFT(:)));
%Find the pixel position of the maximum value
[x,y]= find(real(IFT)==maxpt);
figure,subplot(1,2,1);imagesc(A(x:x+size(B,1),y:y+size(B,2),:));axis image
subplot(1,2,2);imagesc(B);axis image
The result is as follows