频率和空间域中的互相关-模板匹配



所以当我试图在大图像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(:)));

我不明白我在频域里做错了什么。我试过没有偏移的情况,它给出了不同的结果,尽管仍然是错误的。我该如何正确地执行此操作?

这应该可以做到:

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)

相关内容

  • 没有找到相关文章

最新更新