Python中基于FFT的2D卷积和相关 [英] FFT-based 2D convolution and correlation in Python

查看:526
本文介绍了Python中基于FFT的2D卷积和相关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

scipy(或其他流行的库)中是否内置了基于FFT的2D互相关或卷积函数?

Is there a FFT-based 2D cross-correlation or convolution function built into scipy (or another popular library)?

有以下函数:


  • scipy.signal.correlate2d - 实施的直接方法> convolveND 对于大数据来说将是

  • scipy.ndimage.correlate - 使用
    精确计算(即不是FFT)将数组与给定内核相关联。

  • scipy.fftpack.convolve.convolve ,我不太懂,但似乎错了

  • scipy.signal.correlate2d - "the direct method implemented by convolveND will be slow for large data"
  • scipy.ndimage.correlate - "The array is correlated with the given kernel using exact calculation (i.e. not FFT)."
  • scipy.fftpack.convolve.convolve, which I don't really understand, but seems wrong

numarray有 correlate2d()函数带有 fft = True 切换,但我猜numarray将
折叠成numpy,我无法找到是否包含此功能。

numarray had a correlate2d() function with an fft=True switch, but I guess numarray was folded into numpy, and I can't find if this function was included.

推荐答案

我找到 scipy.signal.fftconvolve 还有马格努斯指出,但当时没有意识到它是 n - 维度。由于它内置并产生正确的值,它似乎是理想的解决方案。

I found scipy.signal.fftconvolve, as also pointed out by magnus, but didn't realize at the time that it's n-dimensional. Since it's built-in and produces the right values, it seems like the ideal solution.

来自 2D卷积示例

In [1]: a = asarray([[ 1, 2, 3],
   ...:              [ 4, 5, 6],
   ...:              [ 7, 8, 9]])

In [2]: b = asarray([[-1,-2,-1],
   ...:              [ 0, 0, 0],
   ...:              [ 1, 2, 1]])

In [3]: scipy.signal.fftconvolve(a, b, mode = 'same')
Out[3]: 
array([[-13., -20., -17.],
       [-18., -24., -18.],
       [ 13.,  20.,  17.]])

正确!另一方面,STSCI版本需要一些额外的工作才能使边界正确?

Correct! The STSCI version, on the other hand, requires some extra work to make the boundaries correct?

In [4]: stsci.convolve2d(a, b, fft = True)
Out[4]: 
array([[-12., -12., -12.],
       [-24., -24., -24.],
       [-12., -12., -12.]])

(STSCI方法也需要编译,我没有成功(我只是注释掉了非python部分),有一些错误,如这个并修改输入([1,2]变为[[1,2]]等等。所以我改变了我接受的内置答案在 fftconvolve()函数中。)

(The STSCI method also requires compiling, which I was unsuccessful with (I just commented out the non-python parts), has some bugs like this and modifying the inputs ([1, 2] becomes [[1, 2]]), etc. So I changed my accepted answer to the built-in fftconvolve() function.)

相关性当然与卷积相同,但有一个输入反转:

Correlation, of course, is the same thing as convolution, but with one input reversed:

In [5]: a
Out[5]: 
array([[3, 0, 0],
       [2, 0, 0],
       [1, 0, 0]])

In [6]: b
Out[6]: 
array([[3, 2, 1],
       [0, 0, 0],
       [0, 0, 0]])

In [7]: scipy.signal.fftconvolve(a, b[::-1, ::-1])
Out[7]: 
array([[ 0., -0.,  0.,  0.,  0.],
       [ 0., -0.,  0.,  0.,  0.],
       [ 3.,  6.,  9.,  0.,  0.],
       [ 2.,  4.,  6.,  0.,  0.],
       [ 1.,  2.,  3.,  0.,  0.]])

In [8]: scipy.signal.correlate2d(a, b)
Out[8]: 
array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [3, 6, 9, 0, 0],
       [2, 4, 6, 0, 0],
       [1, 2, 3, 0, 0]])

最新修订版通过内部使用两种幂的大小加速(然后我通过使用真实FFT进行实际输入使用5光滑长度代替权力2 :D)。

and the latest revision has been sped up by using power-of-two sizes internally (and then I sped it up more by using real FFT for real input and using 5-smooth lengths instead of powers of 2 :D ).

这篇关于Python中基于FFT的2D卷积和相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆