Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab xcorr2对“1”;手册;互相关_Matlab_Fft_Offset_Cross Correlation - Fatal编程技术网

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完成了!