我正在尝试使用anfft
module来提高计算搜索图像和模板图像之间的规范化互相关的函数的速度,scipy.fftpack
{{3}}为FFTW提供Python绑定为了我的目的,C库似乎比scipy.fftpack.fftn
快〜2-3倍。
当我采用模板的FFT时,我需要将结果填充到与搜索图像相同的大小,以便我可以对它们进行卷积。使用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)
相同的问题:
{{1}}
我怀疑我可能犯了一个非常基本的错误,因为我对离散傅立叶变换并不太熟悉。
答案 0 :(得分:1)
只需进行逆变换即可看到scipy的填充略有不同(仅限于上边缘和右边缘):
plt.imshow(ifftn(fftn(procrustes(temp,img.shape))).real)
plt.imshow(ifftn(fftn(temp,shape=img.shape)).real)