0
我試圖通過使用爲FFTW C庫提供Python綁定並且似乎是〜2的函數來提高計算搜索圖像與模板圖像之間的歸一化互相關的函數的速度,爲我的目的比scipy.fftpack
快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)
我懷疑我可能做一個很基本的錯誤,因爲我不是太熟悉的離散傅里葉變換。
是啊,preeeetty基本的錯誤在那裏。謝謝你清理那個。 –