主要内容

xcorr2

R2026b

二维互相关性

描述

c = xcorr2(a,b) 返回矩阵 a 和 b 的互相关性,不进行缩放。xcorr2 是 xcorr 的二维版本。

示例

c = xcorr2(a) 是输入矩阵 a 的自相关矩阵。此语法等效于 xcorr2(a,a)。

示例

全部折叠

创建两个矩阵 M1 和 M2。

M1 = [17 24  1  8 15;
      23  5  7 14 16;
       4  6 13 20 22;
      10 12 19 21  3;
      11 18 25  2  9];

M2 = [8 1 6;
      3 5 7;
      4 9 2];

M1 大小为 5×5,M2 大小为 3×3,因此其互相关性大小为 (5+3-1)×(5+3-1),即 7×7。用滞后表示,所得矩阵为

C=(c-2,-2c-2,-1c-2,0c-2,1c-2,2c-2,3c-2,4c-1,-2c-1,-1c-1,0c-1,1c-1,2c-1,3c-1,4c0,-2c0,-1c0,0c0,1c0,2c0,3c0,4c1,-2c1,-1c1,0c1,1c1,2c1,3c1,4c2,-2c2,-1c2,0c2,1c2,2c2,3c2,4c3,-2c3,-1c3,0c3,1c3,2c3,3c3,4c4,-2c4,-1c4,0c4,1c4,2c4,3c4,4).

例如,计算元素 c0,2(或 MATLAB® 中的 C(3,5),因为 M2 大小为 3×3)。将两个矩阵对齐,使其 (1,1) 元素重合。此放置对应于 c0,0。要找到 c0,2,请将 M2 向右滑动两行。

现在 M2 位于矩阵 M1(1:3,3:5) 之上。计算逐元素乘积并对它们求和。答案应为

1×8+7×3+13×4+8×1+14×5+20×9+15×6+16×7+22×2=585.

[r2,c2] = size(M2);

CC = sum(sum(M1(0+(1:r2),2+(1:c2)).*M2))
CC = 
585

使用 xcorr2 验证结果。

D = xcorr2(M1,M2);

DD = D(0+r2,2+c2)
DD = 
585

给定大小为 M×N 的矩阵 X 和大小为 P×Q 的矩阵 H,其二维互相关性 C=X⋆H 是大小为 (M+P-1)×(N+Q-1) 的矩阵,其元素为

C(k,l)=Tr{X∼H∼kl†}1≤k≤M+P-1,1≤l≤N+Q-1.

Tr 是迹,剑号表示埃尔米特共轭。矩阵 X∼ 和 H∼kl 的大小为 (M+2(P-1))×(N+2(Q-1)),非零元素由下式给出

X~(m,n)=X(m-P+1,n-Q+1),P≤m≤M+P-1,Q≤n≤N+Q-1

和

H~kl(p,q)=H(p-k+1,q-l+1),k≤p≤P+k-1,l≤q≤Q+l-1.

调用 xcorr2 等效于此过程,适用于任意大小的一般复矩阵。

创建两个复矩阵,即大小为 7×22 的 X 和大小为 6×17 的 H。

X = randn([7 22])+1j*randn([7 22]);
H = randn([6 17])+1j*randn([6 17]);

[M,N] = size(X);
m = 1:M;
n = 1:N;

[P,Q] = size(H);
p = 1:P;
q = 1:Q;

初始化 X∼ 和 C。

Xt = zeros([M+2*(P-1) N+2*(Q-1)]);
Xt(m+P-1,n+Q-1) = X;
C = zeros([M+P-1 N+Q-1]);

通过遍历 k 和 l 来计算 C 的元素。在每步将 H∼kl 重置为零。通过对元素乘积求和(而不是相乘并取迹)来节省时间和内存。

for k = 1:M+P-1
    for l = 1:N+Q-1
        Hkl = zeros([M+2*(P-1) N+2*(Q-1)]);
        Hkl(p+k-1,q+l-1) = H;
        C(k,l) = sum(sum(Xt.*conj(Hkl)));
    end
end

max(max(abs(C-xcorr2(X,H))))
ans = 
1.5139e-14

答案与 xcorr2 的输出在计算机精度上一致。

使用互相关性来查找图像的一部分适合整个图像的位置。互相关性使您能够找到两个信号最相似的区域。对于二维信号(如图像),使用 xcorr2。

将黑白测试图像加载到工作区中。使用 imagesc 显示它。

load durer
img = X;
White = max(max(img));

imagesc(img)
axis image off
colormap gray
title("Original")

Figure contains an axes object. The hidden axes object with title Original contains an object of type image.

选择图像的一个矩形部分。显示缺失该部分的较大图像。

x = 435;
X = 535;
szx = x:X;

y = 62;
Y = 182;
szy = y:Y;

Sect = img(szx,szy);

kimg = img;
kimg(szx,szy) = White;

kumg = White*ones(size(img));
kumg(szx,szy) = Sect;

tiledlayout("horizontal")
nexttile
imagesc(kimg)
axis image off
colormap gray
title("Image")

nexttile
imagesc(kumg)
axis image off
colormap gray
title("Section")

Figure contains 2 axes objects. Hidden axes object 1 with title Image contains an object of type image. Hidden axes object 2 with title Section contains an object of type image.

使用 xcorr2 查找小图像适合较大图像的位置。减去均值,使负值和正值的数量大致相等。

nimg = img-mean(mean(img));
nSec = nimg(szx,szy);

crr = xcorr2(nimg,nSec);

互相关性的最大值对应于该部分右下角的估计位置。使用 ind2sub 将最大值的一维位置转换为二维坐标。

[ssr,snd] = max(crr(:));
[ij,ji] = ind2sub(size(crr),snd);

figure
plot(crr(:))
title("Cross-Correlation")
hold on
plot(snd,ssr,"or")
hold off
text(snd*1.05,ssr,"Maximum")

Figure contains an axes object. The axes object with title Cross-Correlation contains 3 objects of type line, text. One or more of the lines displays its values using only markers

将较小图像放置在较大图像内。旋转较小图像,以符合 MATLAB® 用于显示图像的约定。在其周围绘制一个矩形。

img(ij:-1:ij-size(Sect,1)+1,ji:-1:ji-size(Sect,2)+1) = rot90(Sect,2);

imagesc(img)
axis image off
colormap gray
title("Reconstructed")
hold on
plot([y y Y Y y],[x X X x x],"r")
hold off

Figure contains an axes object. The hidden axes object with title Reconstructed contains 2 objects of type image, line.

将模板偏移已知量,并使用互相关性还原该偏移。

在 11×11 矩阵中创建一个模板。创建一个 22×22 矩阵,并将原始模板沿行维度偏移 8,沿列维度偏移 6。

template = 0.2*ones(11);
template(6,3:9) = 0.6;
template(3:9,6) = 0.6;
offsetTemplate = 0.2*ones(22);
offset = [8 6];
offsetTemplate((1:size(template,1))+offset(1), ...
    (1:size(template,2))+offset(2)) = template;

绘制原始模板和偏移后的模板。

imagesc(offsetTemplate)
colormap gray
hold on
imagesc(template)
axis equal

Figure contains an axes object. The axes object contains 2 objects of type image.

对两个矩阵进行互相关,并找到互相关性的最大绝对值。使用最大绝对值的位置确定模板中的偏移。将结果与已知偏移进行核对。

cc = xcorr2(offsetTemplate,template);
[max_cc, imax] = max(abs(cc(:)));
[ypeak, xpeak] = ind2sub(size(cc),imax(1));
corr_offset = [(ypeak-size(template,1)) (xpeak-size(template,2))];

isequal(corr_offset,offset)
ans = logical
   1

从互相关性获得的偏移在行和列维度上等于已知模板偏移。

此示例需要 Parallel Computing Toolbox™ 软件。请参阅 GPU 计算要求 (Parallel Computing Toolbox)以了解支持哪些 GPU。

将模板偏移已知量,并使用互相关性还原该偏移。

在 11×11 矩阵中创建一个模板。创建一个 22×22 矩阵,并将原始模板沿行维度偏移 8,沿列维度偏移 6。

template = 0.2*ones(11); 
template(6,3:9) = 0.6;   
template(3:9,6) = 0.6;
offsetTemplate = 0.2*ones(22); 
offset = [8 6];
offsetTemplate((1:size(template,1))+offset(1), ...
    (1:size(template,2))+offset(2)) = template;

使用 gpuArray 对象将原始模板矩阵和偏移后的模板矩阵放到您的 GPU 上。

template = gpuArray(template);
offsetTemplate = gpuArray(offsetTemplate);

在 GPU 上计算互相关性。

cc = xcorr2(offsetTemplate,template);

使用 gather 将结果返回到 MATLAB® 工作区。使用互相关性的最大绝对值确定偏移,并将结果与已知偏移进行比较。

cc = gather(cc);
[max_cc,imax] = max(abs(cc(:)));
[ypeak,xpeak] = ind2sub(size(cc),imax(1));
corr_offset = [(ypeak-size(template,1)) (xpeak-size(template,2))];
isequal(corr_offset,offset)
ans = logical
   1

输入参数

全部折叠

输入数组,指定为矩阵。

示例: sin(2*pi*(0:9)'/10)*sin(2*pi*(0:13)/20) 指定二维正弦曲面。

数据类型: single | double
复数支持: 是

输出参量

全部折叠

二维互相关性或自相关矩阵,以矩阵形式返回。

详细信息

全部折叠

扩展功能

全部展开

C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。

GPU 代码生成
使用 GPU Coder™ 为 NVIDIA® GPU 生成 CUDA® 代码。

版本历史记录

在 R2006a 之前推出

全部展开

另请参阅

| |