Phase correlation of images (Alternative to cross-correlation)

26 次查看(过去 30 天)
Hello, I have developed a code for phase correlation, and I am trying to run it for some images I have, to find where they match and the similarity between them. However I am getting a very strange result, instead of matching where the highest intensity is, it is showing me that the most similar area of the image is the bottom left corner...
I don't know what is wrong with the code, what can I do to fix it?
I have attached a sample file here
clear; close all; clc;
load('my_mat_file.mat');
template = im2gray(ims(:,:,1));
background = im2gray(ims(:,:,2));
%% calculate padding
bx = size(background, 2);
by = size(background, 1);
tx = size(template, 2); % used for bbox placement
ty = size(template, 1);
%% fft
%// - Compute the cross power spectrum
Ga = fft2(background);
Gb = fft2(template, by, bx);
c = real(ifft2((Ga.*conj(Gb))./abs(Ga.*conj(Gb))));
%% find peak correlation
[max_c, imax] = max(abs(c(:)));
[ypeak, xpeak] = find(c == max(c(:)));
figure; surf(c), shading flat; % plot correlation
%% display best match
hFig = figure;
hAx = axes;
%// New - no need to offset the coordinates anymore
%// xpeak and ypeak are already the top left corner of the matched window
position = [xpeak(1), ypeak(1), tx, ty];
imshow(background, 'Parent', hAx);
imrect(hAx, position);

回答(1 个)

Mark Pratt
Mark Pratt 2023-12-22
You probably need fftshift.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by