PROJECT 04-05
Correlation in the FrequencyDomain
Download Figs.4.41(a) and (b) and duplicate Example 4.11 to obtain Fig. 4.41(e). Give the(x,y) coordinates of the location of the maximum value in the 2D correlation function.There is no need to plot the profile in Fig. 4.41(f).
f1=imread('Fig4.41(a).jpg');
f2=imread('Fig4.41(b).jpg');
[M1,N1]=size(f1);[M2,N2]=size(f2);
%P=M1+ M2-1;Q= N1+ N2-1;
P=298;Q=298;
fp1=zeros(P,Q);fp1(1:M1,1:N1)=f1(1:M1,1:N1);
fp2=zeros(P,Q);fp2(1:M2,1:N2)=f2(1:M2,1:N2);
[Y,X]=meshgrid(1:Q,1:P);
ones=(-1).^(X+Y);
f1=ones.*fp1;FP1=fft2(f1);
f2=ones.*fp2;FP2=fft2(f2);
FP= FP2.*conj(FP1);
fp= mat2gray(real(ones.*ifft2(FP)));
imshow(fp)
max_value=max(max(fp))%可以求出最大元素的值
[row col]=find(fp== max_value)%求出最大值所在位置
结果如下
相关函数最高峰位于模板和图像发生最佳匹配的点上
或者利用函数滤波
fp=imfliter(fp1,fp2)
fp=mat2gray(fp);
imshow(fp)
取另一个函数的复共轭,或者将另一个图像作为滤波器
使用MATLAB自带傅里叶变换函数效率更高,编程时采用fft2和iffft2。
%傅里叶变换
%F(u,v)=exp((-1j)*2*pi*(u)'*x2/298)*f1(x2,y2)*exp((-1j)*2*pi*(v)'*y2%/298);
%傅里叶反变换
%fp(x2,y2)=exp(1j*2*pi*(u)'*x2/298)*iG(u,v)*exp(1j*2*pi*(v)'*y2/298%)./(298*298);