Python中基于FFT的2D卷积和相关

发布于 2024-07-27 13:47:31 字数 522 浏览 5 评论 0原文

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

有这样的函数:

  • scipy.signal.correlate2d -“由convolveND实现的直接方法将是 对于大数据来说很慢”
  • scipy.ndimage.correlate - “该数组与给定的内核相关,使用 精确计算(即不是 FFT)。”
  • scipy.fftpack.convolve.convolve,我不太明白,但似乎错误

numarray 有一个 correlate2d() 函数带有 fft=True 开关,但我猜 numarray 被折叠了 进入numpy,我找不到是否包含此函数。

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

There are functions like these:

  • 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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

可爱暴击 2024-08-03 13:47:31

我找到了 scipy.signal.fftconvolve,正如 magnus 所指出的,但没有当时意识到它是n维的。 由于它是内置的并产生正确的值,因此它似乎是理想的解决方案。

来自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 版本需要一些额外的工作才能使边界正确?

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

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

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

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 )。

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.

From Example of 2D Convolution:

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.]])

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.]])

(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]])

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 ).

陌若浮生 2024-08-03 13:47:31

看看 scipy.signal.fftconvolve,signal.convolve 和 signal.correlate (有一个 signal.correlate2d 但它似乎返回一个移位数组,不居中)。

look at scipy.signal.fftconvolve, signal.convolve and signal.correlate (there is a signal.correlate2d but it seems to return an shifted array, not centered).

鹊巢 2024-08-03 13:47:31

我认为你想要 scipy.stsci 包:

http://docs.scipy.org /doc/scipy/reference/stsci.html

In [30]: scipy.__version__
Out[30]: '0.7.0'

In [31]: from scipy.stsci.convolve import convolve2d, correlate2d

I think you want the scipy.stsci package:

http://docs.scipy.org/doc/scipy/reference/stsci.html

In [30]: scipy.__version__
Out[30]: '0.7.0'

In [31]: from scipy.stsci.convolve import convolve2d, correlate2d
心病无药医 2024-08-03 13:47:31

我编写了一个互相关/卷积包装器,负责填充和填充。 nans 并在此处包含一个简单的平滑包装器。 它不是一个流行的包,但除了 numpy(或 fftw 以获得更快的 ffts)之外,它也没有任何依赖项。

我还在此处 如果有人感兴趣的话。 令人惊讶的是,它表明 numpy 的 fft 比 scipy 更快,至少在我的机器上是这样。

编辑:将代码移至 N 维版本此处

I wrote a cross-correlation/convolution wrapper that takes care of padding & nans and includes a simple smooth wrapper here. It's not a popular package, but it also has no dependencies besides numpy (or fftw for faster ffts).

I've also implemented an FFT speed testing code here in case anyone's interested. It shows - surprisingly - that numpy's fft is faster than scipy's, at least on my machine.

EDIT: moved code to N-dimensional version here

阳光①夏 2024-08-03 13:47:31

我已经忘记了这个包在 scipy 中的状态,但我知道我们将 ndimage 作为 stsci_python 发行包的一部分包含在内,以方便我们的用户:

http://www.stsci.edu/resources/software_hardware/pyraf/stsci_python/current/download

或者你应该能够拉它如果您愿意,可以从存储库中获取:

https://www.stsci。 edu/svn/ssb/stsci_python/stsci_python/trunk/ndimage/

I've lost track of the status of this package in scipy, but I know we include ndimage as part of the stsci_python release package as a convenience for our users:

http://www.stsci.edu/resources/software_hardware/pyraf/stsci_python/current/download

or you should be able pull it from the repository if you prefer:

https://www.stsci.edu/svn/ssb/stsci_python/stsci_python/trunk/ndimage/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文