【问题标题】:Cross-Correlation in Frequency and Spatial domain - Template Matching频域和空间域的互相关 - 模板匹配
【发布时间】:2014-10-09 21:01:42
【问题描述】:

所以当我试图在大图像 A 中找到模板 B 时,我可以通过找到互相关的最大值来做到这一点,就像在空间域中这样:

%       Finding maximum of correlation:
        phi = normxcorr2(B,A); 
        [ymax, xmax] = find(phi == max(phi(:)));

%       Find position in original image:
        ypeak = ymax - size(B,1);
        xpeak = xmax - size(B,2);

但是当我想在频域中做时,我得到了错误的结果:

%       Calculate correlation in frequency domain:
        Af = fft2(A);
        Bf = fft2(B, size(A,1), size(A,2));
        phi2f = conj(Af)'*Bf;

%       Inverse fft to get back to spatial domain:       
        phi2 = real(ifft(fftshift(phi2f)));

%       Now we have correlation matrix, excatly the same as calculated in
%       the spatial domain.
        [ymax2, xmax2] = find(phi2 == max(phi2(:)));

我不明白我在频域中做错了什么。我在没有 fftshift 的情况下尝试过,它给出了不同的结果,尽管仍然是错误的。我怎样才能正确地做到这一点?

【问题讨论】:

  • normcorr2的定义,我猜我们可以假设B是你的模板,A是你的图像?我认为如果您添加它会很好,只是为了清楚起见。
  • 如果您有图像,请将它们与预期和实际输出一起添加,以便它成为可重复的示例。
  • 我认为conj(Af)' 采用了Af 的非共轭转置,这真的是你想要的吗?
  • @McMa 它需要共轭转置,只需使用 (conj(Af))' 进行检查
  • 恐怕它没有:转置运算符' 已经执行了共轭转置,通过使用conj(),您只是取消了初始共轭,留下了非共轭转置。这是让我印象深刻的第一件事。

标签: matlab image-processing 2d template-matching frequency-domain


【解决方案1】:

这应该可以解决问题:

t   = imread('cameraman.tif');
a   = imtranslate(t, [15, 25]);

% Determine padding size in x and y dimension
size_t      = size(t);
size_a      = size(a);
outsize     = size_t + size_a - 1;

% Determine 2D cross correlation in Fourier domain
Ft = fft2(t, outsize(1), outsize(2));
Fa = fft2(a, outsize(1), outsize(2));
c = abs( fftshift( ifft2(Fa .* conj(Ft))) );

% Find peak
[max_c, imax]   = max(abs(c(:)));
[ypeak, xpeak]  = ind2sub(size(c), imax(1));

% Correct found peak location for image size
corr_offset = round([(ypeak-(size(c, 1)+1)/2) (xpeak-(size(c, 2)+1)/2)]);

% Write out offsets
y_offset = corr_offset(1)
x_offset = corr_offset(2)

【讨论】:

  • 有趣!您将如何更改代码以计算归一化互相关版本?
猜你喜欢
  • 2019-11-08
  • 2011-02-24
  • 2015-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-04
相关资源
最近更新 更多