我正在尝试通过使用anfft module来提高计算搜索图像和模板图像之间的归一化互相关的函数的速度,它为FFTW库提供了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)我怀疑我可能犯了一个非常基本的错误,因为我不太熟悉离散傅立叶变换。
发布于 2012-09-17 22:38:19
只需做反变换,你就会看到scipy做的填充略有不同(仅对顶部和右侧边缘):
plt.imshow(ifftn(fftn(procrustes(temp,img.shape))).real)
plt.imshow(ifftn(fftn(temp,shape=img.shape)).real)https://stackoverflow.com/questions/12461479
复制相似问题