0

我正在尝试通过使用该anfft 模块来提高计算搜索图像和模板图像之间标准化互相关的函数的速度,该模块为 FFTW C 库提供 Python 绑定,并且似乎比它快 2-3 倍scipy.fftpack为了我的目的。

当我对模板进行 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)

我怀疑我可能犯了一个非常基本的错误,因为我对离散傅里叶变换并不太熟悉。

4

1 回答 1

1

只需进行逆变换,您就会看到 scipy 的填充略有不同(仅在顶部和右侧边缘):

plt.imshow(ifftn(fftn(procrustes(temp,img.shape))).real)

plt.imshow(ifftn(fftn(temp,shape=img.shape)).real)
于 2012-09-17T14:38:18.973 回答