MATLABスタディノートピアソンの相関係数とテンプレートマッチング

ピアソン相関係数

        統計では、ピアソンの積-モーメント相関係数(PPMCCまたはPCC)とも呼ばれるピアソン相関係数を使用して、2つの変数XとYの間の関係を測定します。-1から1までの値の相関(線形相関) 。

        

         計算手順については、次の記事のセクションVII相関を参照してください。

機械学習ノート-データと統計の一般的な用語 1.データの種類定性的および定量的、離散的または連続的2.中心傾向の平均、中央値、最頻値、外れ値、幾何平均、調和平均、加重平均の測定。3.データサンプリング:ランダムサンプリング、系統抽出、層化サンプリング、クラスタリングサンプルなど。4.測定された分位数、平均偏差、標準偏差、およびデータ分布の分散

第二に、MATLAB計算の相関

1.例1

        式によると、AとBは高い相関関係があります。相関係数は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.例2

        corr2関数を使用すると、CとDの相関係数は-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.テンプレートマッチングの例

元の画像

 

テンプレート

1.normxcorr2関数を使用します

       参照コード

%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

        実行結果は次のとおりです。

 2.corr2関数を使用します

        参照コード

%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);

        実行結果は次のとおりです。

左:相関プロット(x、y)。右:一致する位置

 3.周波数領域テンプレートマッチング

        フーリエ領域での正規化された相互相関に基づいています。位相相関とも呼ばれます。ここで使用されている2つの写真は、同じシーンの異なるスナップショットです。Image1.jpgがテンプレート画像として使用され、Image2.jpgのサブ画像がターゲット画像として使用されます。宛先画像には、テンプレート画像のサイズに一致するようにゼロが埋め込まれます。フーリエ変換後、テンプレート信号はターゲット信号の共役で乗算され、正規化されます。次に、逆フーリエを適用して、最大値に対応するピクセル位置を抽出します。

%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
同じシーンの異なるスナップショット

         画像マトリックスBからパーツを切り抜きます

B = imcrop(B,[58.5 49.5 226 102]);
figure,imagesc(B);title('sub Image - Image 2');axis image

         フーリエ変換と逆変換を適用する

%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);
コレログラムを計算する

         最大値のピクセル位置を見つけて、image1と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

        結果は次のとおりです。

おすすめ

転載: blog.csdn.net/bashendixie5/article/details/123580576