Matlab xcorr2对“1”;手册;互相关
我编写了一个简短的代码,它既可以“手动”对两个图像进行互相关,也可以使用Matlab xcorr2对“1”;手册;互相关,matlab,fft,offset,cross-correlation,Matlab,Fft,Offset,Cross Correlation,我编写了一个简短的代码,它既可以“手动”对两个图像进行互相关,也可以使用xcorr2进行互相关。目标是找到两幅图像之间的偏移(以像素为单位) 我不明白为什么,但这两种方法给了我截然不同的结果 xcorr2方法的实现方式如下: clc close all %Cropping the bitmaps into the 2 sections I Want to cross-correlate, placing them in the center of new bitmaps with 2^n dim
xcorr2
进行互相关。目标是找到两幅图像之间的偏移(以像素为单位)
我不明白为什么,但这两种方法给了我截然不同的结果
xcorr2
方法的实现方式如下:
clc
close all
%Cropping the bitmaps into the 2 sections I Want to cross-correlate, placing them in the center of new bitmaps with 2^n dimensions
ExpInitCrop = imcrop(BMInit,[1, 157, 1024, 131]); %Size of cropped images : 132*1024
ExpFinalCrop = imcrop(BMFinal,[1, 157, 1024, 131]);
ExpInitLarge = padarray(ExpInitCrop, [190, 512]); %Total size of both images : 512*2048
ExpFinalLarge = padarray(ExpFinalCrop, [190, 512]);
%Grayscalling the 2 sections
ExpInitLargeGS = ind2gray(ExpInitLarge, mapInit);
ExpFinalLargeGS = ind2gray(ExpFinalLarge, mapFinal);
%xcorr
tic
XCorrExp = xcorr2(ExpInitLargeGS, ExpFinalLargeGS);
toc
whos XCorrExp
mesh(XCorrExp)
%Finding shift alongside X and Y axis
[max_XCorrExp, imax] = max(abs(XCorrExp(:)));
[ypeak, xpeak] = ind2sub(size(XCorrExp),imax(1));
corr_offset = [(ypeak-size(ExpInitLarge,1)) (xpeak-size(ExpInitLarge,2))]
xcorr2函数的计算时间约为85-90秒。输出矩阵XCorrExp的大小为1023*4096。计算出的偏移量为(-2,-37),这似乎完全符合输入数据
现在,使用以下公式实现了“手动”互相关:corr(a,b)=ifft(fft(a_和u零)*conj(fft(b_和u零))
,这是两个信号互相关的公式。
代码如下:
clc
close all
%Cropping the bitmaps into the 2 sections I Want to cross-correlate, placing them in the center of new bitmaps with 2^n dimensions
ExpInitCrop = imcrop(BMInit,[1, 157, 1024, 131]); %Size of cropped images : 132*1024
ExpFinalCrop = imcrop(BMFinal,[1, 157, 1024, 131]);
ExpInitLarge = padarray(ExpInitCrop, [190, 512]); %Total size of both images : 512*2048
ExpFinalLarge = padarray(ExpFinalCrop, [190, 512]);
tic
%Grayscalling the 2 sections
ExpInitLargeGS = ind2gray(ExpInitLarge, mapInit);
ExpFinalLargeGS = ind2gray(ExpFinalLarge, mapFinal);
%FFT
ExpInitFFT = fft2(ExpInitLargeGS);
ExpFinalFFT = fft2(ExpFinalLargeGS);
%Conjugating complex values
for i = 1:size(ExpFinalFFT,1)
for j = 1:size(ExpFinalFFT,2)
ExpFinalFFT(i,j) = conj(ExpFinalFFT(i,j));
end
end
%Element-wise multiplication
ExpMultipliedMatrix = times(ExpInitFFT, ExpFinalFFT);
%IFFT
XCorrExp = ifft2(ExpMultipliedMatrix);
XCorrExpShift = fftshift(XCorrExp);
toc
whos XCorrExpShift
mesh(XCorrExpShift)
%Finding shift alongside X and Y axis
[max_XCorrExpShift, imax] = max(abs(XCorrExpShift(:)));
[ypeak, xpeak] = ind2sub(size(XCorrExpShift),imax(1));
corr_offset = [(ypeak-size(ExpInitLarge,1)) (xpeak-size(ExpInitLarge,2))]
“手动”方法的计算时间约为0.7-0.8秒。输出矩阵XCorrExpShift的大小为512*2048。计算出的偏移量是(-257,-1060),根据输入数据,这看起来很遥远
我很难理解为什么我的“手动”方法不能给出与xcorr2
方法相同的结果。有人能给我解释一下吗?什么是xcorr2
做的而我的手动方法做不到的
谢谢
编辑:预期输出将与xcorr2
方法给出的偏移量相同。但我也想理解为什么在计算时间上有如此巨大的差异。
请给我们一个(特别是包括输入和预期输出)@Wolfie完成了!