我正在尝试使用维基百科上的一份配方(http://en.wikipedia.org/wiki/Phase_correlation)在R中实现2D相位相关算法,以便跟踪两个图像之间的移动。这些图像(帧)是由于摄像机在风中晃动而捕获的,最终目标是消除这些和随后的帧中的抖动。下面是两个示例图像和R代码:
## we will need the tiff library
library(tiff)
## read in the tiff files
f1=as.matrix(readTIFF('f1.tiff',native=TRUE))
f2=as.matrix(readTIFF('f2.tiff',native=TRUE))
## take the fft of the first frame
F1 <- fft(f1)
## take the Conjugate fft of the second frame
F2.c <- Conj(fft(f2))
## calculate the cross power spectrum according to the wiki article
R <- (F1*F2.c)/abs(F1*F2.c)
## take the inverse fft of R
r <- fft(R,inv=TRUE)/length(R)
## because the zero valued imaginary numbers are not needed
r <- Re(r)
## show the normalized cross-correlation
image(r)
## find the max in the cross correlation matrix, or the phase shift -
## between the two images
shift <- which(r==max(r),arr.ind=TRUE)
< p >据我理解,向量shift应包含有关最佳矫正这两幅图像的平移位移信息(dx和dy)。但是,变量shift给出dx=1和dy=1,我认为这表示x或y方向都没有移动。这在后续帧中发生,在这些帧中存在可见的x和y方向上的多个像素移动。
您是否看到我的代码/公式中的任何错误?还是我需要尝试一些更高级的东西,比如在进行相位相关之前先过滤图像?
祝大家好运!