首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在scipy.fftpack中,如何对2D数组进行零填充?

在scipy.fftpack中,如何对2D数组进行零填充?
EN

Stack Overflow用户
提问于 2012-09-17 22:30:15
回答 1查看 2K关注 0票数 0

我正在尝试通过使用anfft module来提高计算搜索图像和模板图像之间的归一化互相关的函数的速度,它为FFTW库提供了Python,就我的目的而言,它似乎比scipy.fftpack快2-3倍。

当我使用我的模板的FFT时,我需要将结果填充到与我的搜索图像相同的大小,以便我可以对它们进行卷积。使用scipy.fftpack.fftn时,我只会使用shape参数进行填充/截断,但anfft.fftn更简约,本身不会进行任何零填充。

当我尝试自己填充零时,我得到的结果与我使用shape得到的结果非常不同。这个例子只使用了scipy.fftpack,但是我使用anfft也遇到了同样的问题

代码语言:javascript
复制
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)

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-17 22:38:19

只需做反变换,你就会看到scipy做的填充略有不同(仅对顶部和右侧边缘):

代码语言:javascript
复制
plt.imshow(ifftn(fftn(procrustes(temp,img.shape))).real)

plt.imshow(ifftn(fftn(temp,shape=img.shape)).real)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12461479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档