Python中基于FFT的2D卷积和相关
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 byconvolveND
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我找到了 scipy.signal.fftconvolve,正如 magnus 所指出的,但没有当时意识到它是n维的。 由于它是内置的并产生正确的值,因此它似乎是理想的解决方案。
来自2D 卷积示例:
正确! 另一方面,STSCI 版本需要一些额外的工作才能使边界正确?
(STSCI方法也需要编译,我没有成功(我只是注释掉了非python部分),有一些错误,例如 this 并修改输入([1, 2] 变为 [[1, 2]])等。所以我将我接受的答案更改为内置
fftconvolve()
函数。)当然,相关性与卷积相同,但一个输入相反:
和 最新版本 已通过内部使用二次幂大小来加速(然后我通过 使用真实的 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:
Correct! The STSCI version, on the other hand, requires some extra work to make the boundaries correct?
(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:
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 ).
看看 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).
我认为你想要 scipy.stsci 包:
http://docs.scipy.org /doc/scipy/reference/stsci.html
I think you want the scipy.stsci package:
http://docs.scipy.org/doc/scipy/reference/stsci.html
我编写了一个互相关/卷积包装器,负责填充和填充。 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
我已经忘记了这个包在 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/
请注意,有 scipy.signal.oaconvolve< /a> 自 Scipy 1.4 起,使用 重叠添加方法 。
Note that there is scipy.signal.oaconvolve since Scipy 1.4, which uses the Overlap-add method.