我决定通过FFT来实现它。在def fft_xcorr2D(x):
# Over axes (-2,-1) (default in the fft2 function)
## Pad because of cyclic (circular?) behavior of the FFT
x = np.fft2(np.pad(x,([0,0],[0,0],[0,34],[0,34]),mode='constant'))
# Conjugate for correlation, not convolution (Conv. Theorem)
x[:,1,:,:] = np.conj(x[:,1,:,:])
# Over axes (-2,-1) (default in the ifft2 function)
## Multiply elementwise over 2:nd axis (2 image bands for me)
### fftshift over rows and column over images
corr = np.fft.fftshift(np.ifft2(np.prod(x,axis=1)),axes=(-2,-1))
# Return after removing padding
return np.abs(corr)[:,3:-2,3:-2]
呼叫方式:
^{pr2}$
如果有人想用它:
我的输入是一个4D数组:(N, 2, #Rows, #Cols)
例如(500, 2, 30, 30):500张图像,2个波段(例如偏振),像素为30x30
如果您的输入不同,请根据您的喜好调整填充
检查输入顺序是否与我的相同,否则更改fft2和ifft2函数中的轴参数,则np.生产和fftshift。我使用fftshift在中间(否则在角落)获得最大值,因此如果这不是您想要的,请小心。在
为什么是最大值?从技术上讲,这不一定,但就我的目的而言是这样。fftshift用于获得一个看起来你习惯了的关联。否则,象限将“由内而外”。如果您想知道我的意思,请删除fftshift(只是fftshift部分,而不是它的参数),像以前一样调用函数,并绘制它。在