【发布时间】:2012-09-09 19:12:31
【问题描述】:
我正在尝试通过使用 anfft module 来提高计算搜索图像和模板图像之间标准化互相关的函数的速度,该函数为 FFTW C 库提供 Python 绑定,并且似乎就我的目的而言,比scipy.fftpack 快约 2-3 倍。
当我对模板进行 FFT 时,我需要将结果填充到与搜索图像相同的大小,以便对它们进行卷积。使用scipy.fftpack.fftn 我只会使用shape 参数进行填充/截断,但anfft.fftn 更简约,并且本身不进行任何零填充。
当我尝试自己进行零填充时,我得到的结果与使用 shape 得到的结果截然不同。这个例子只使用了scipy.fftpack,但我对anfft也有同样的问题:
import numpy as np
from scipy.fftpack import fftn
from scipy.misc import lena
img = lena()
temp = img[240:281,240:281]
def procrustes(a,target,padval=0):
# Forces an array to a target size by either padding it with a constant or
# truncating it
b = np.ones(target,a.dtype)*padval
aind = [slice(None,None)]*a.ndim
bind = [slice(None,None)]*a.ndim
for dd in xrange(a.ndim):
if a.shape[dd] > target[dd]:
diff = (a.shape[dd]-b.shape[dd])/2.
aind[dd] = slice(np.floor(diff),a.shape[dd]-np.ceil(diff))
elif a.shape[dd] < target[dd]:
diff = (b.shape[dd]-a.shape[dd])/2.
bind[dd] = slice(np.floor(diff),b.shape[dd]-np.ceil(diff))
b[bind] = a[aind]
return b
# using scipy.fftpack.fftn's shape parameter
F1 = fftn(temp,shape=img.shape)
# doing my own zero-padding
temp_padded = procrustes(temp,img.shape)
F2 = fftn(temp_padded)
# these results are quite different
np.allclose(F1,F2)
我怀疑我可能犯了一个非常基本的错误,因为我对离散傅里叶变换并不太熟悉。
【问题讨论】: