最も効率的な方法には、信号処理ツールボックスが必要です。その後、単に使用できますxcorr2()
。あなたの例に従って、以下が機能するはずです:
C = xcorr2 (A, B);
[Row, Col] = find (C == max (C(:)));
%% these are not the coordinates for the center of the best match, you will
%% have to find where those really are
%% The best way to understand this is to strip the "padding"
row_shift = (size (B, 1) - 1)/2;
col_shift = (size (B, 2) - 1)/2;
C = C(1+row_shift:end-row_shift, 1+col_shift:end-col_shift)
[Row, Col] = find (C == max (C(:)));
if (B == A(Row-row_shift:Row+row_shift, Col-col_shift:Col+col_shift))
disp ("B shows up in A");
endif
上記のコードは、任意のサイズの入力 (まだ奇数サイズのみ) をカバーしようとしているため、少し複雑に見えますが、機能するはずです。
このツールボックスをお持ちでない場合は、 Octave コードを使用できると思いますが、これにはわずかな調整しか必要ありません。基本的に、重要な 3 行のみが続きます (コードは GPL に準拠していることに注意してください)。
[ma,na] = size(a);
[mb,nb] = size(b);
c = conv2 (a, conj (b (mb:-1:1, nb:-1:1)));
Octave では、少なくともシグナル パッケージ 1.2.0 を使用している場合、xcorr2 は、正規化された相互相関を計算する追加のオプション「coeff」を使用することもできます。一致が完全な場合、値は 1 になるため、次のように簡略化できます。
C = xcorr2 (A, B, "coeff");
if (any (C(:) == 1)
display ("B shows up in A");
endif