Python中的2D卷积类似于Matlab的conv2 [英] 2D Convolution in Python similar to Matlab's conv2

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

问题描述

我一直在尝试使用SciPy和Numpy进行2D矩阵的卷积但是失败了。对于SciPy我试过,sepfir2d和scipy.signal.convolve和Convolve2D for Numpy。在Matlab for Python中是否有像conv2这样的简单函数?

I have been trying to do Convolution of a 2D Matrix using SciPy, and Numpy but have failed. For SciPy I tried, sepfir2d and scipy.signal.convolve and Convolve2D for Numpy. Is there a simple function like conv2 in Matlab for Python?

以下是一个例子:

 A= [ 5     4     5     4;
      3     2     3     2;
      5     4     5     4;
      3     2     3     2 ]

我想用卷积它[0.707 0.707]

来自Matlab的conv2的结果是

And the result as by conv2 from Matlab is

3.5350    6.3630    6.3630    6.3630    2.8280
2.1210    3.5350    3.5350    3.5350    1.4140
3.5350    6.3630    6.3630    6.3630    2.8280
2.1210    3.5350    3.5350    3.5350    1.4140

在Python中计算此输出的一些函数?我将非常感谢您的回复。

Some function to compute this output in Python? I will be grateful for a response.

推荐答案

使用进行此操作有多种方法scipy ,但2D卷积不直接包含在 numpy 中。 (如果你需要避免scipy依赖,那么使用fump也很容易实现fft。)

There are a number of different ways to do it with scipy, but 2D convolution isn't directly included in numpy. (It's also easy to implement with an fft using only numpy, if you need to avoid a scipy dependency.)

scipy.signal.convolve2d scipy.signal.convolve scipy.signal.fftconvolve scipy.ndimage.convolve 将以不同的方式处理2D卷积(最后三个是Nd)。

scipy.signal.convolve2d, scipy.signal.convolve, scipy.signal.fftconvolve, and scipy.ndimage.convolve will all handle a 2D convolution (the last three are N-d) in different ways.

scipy.signal.fftconvolve 在fft域中进行卷积(这是一个简单的乘法)。在许多情况下,这种情况要快得多,但与边缘情况相比,边缘效应的差异可能非常小,而且您的数据将通过此特定实现强制转换为浮点数。此外,在使用更大的阵列卷积小数组时,会有不必要的内存使用。总而言之,基于fft的方法可以大大加快,但是有一些常见的用例,其中 scipy.signal.fftconvolve 不是理想的解决方案。

scipy.signal.fftconvolve does the convolution in the fft domain (where it's a simple multiplication). This is much faster in many cases, but can lead to very small differences in edge effects than the discrete case, and your data will be coerced into floating point with this particular implementation. Additionally, there's unnecessary memory usage when convolving a small array with a much larger array. All in all, fft-based methods can be dramatically faster, but there are some common use cases where scipy.signal.fftconvolve is not an ideal solution.

scipy.signal.convolve2d scipy.signal.convolve ,和 scipy.ndimage.convolve 都使用在C中实现的离散卷积,但是,它们以不同的方式实现它。

scipy.signal.convolve2d, scipy.signal.convolve, and scipy.ndimage.convolve all use a discrete convolution implemented in C, however, they implement it in different ways.

scipy.ndimage.convolve 保持相同的数据类型,并让您控制输出的位置以最小化内存使用。如果您要解析 uint8 (例如图像数据),它通常是最佳选择。输出将始终与第一个输入数组的形状相同,这对图像有意义,但可能不适用于更一般的卷积。 ndimage.convolve 通过模式 kwarg(其功能完全不同)为您提供了对边缘效果处理方式的大量控制比 scipy.signal 模式 kwarg)。

scipy.ndimage.convolve keeps the same data type, and gives you control over the location of the output to minimize memory usage. If you're convolving uint8's (e.g. image data), it's often the best option. The output will always be the same shape as the first input array, which makes sense for images, but perhaps not for more general convolution. ndimage.convolve gives you a lot of control over how edge effects are handled through the mode kwarg (which functions completely differently than scipy.signal's mode kwarg).

如果您正在使用2d数组,请避免 scipy.signal.convolve 。它适用于N-d情况,但它对于2d数组来说不是最理想的,并且 scipy.signal.convolve2d 存在以更高效地执行完全相同的操作。 scipy.signal 中的卷积函数可让您使用模式 kwarg控制输出形状。 (默认情况下,它们的行为就像matlab的 conv2 。)这对于一般的数学卷积非常有用,但对图像处理不太有用。但是, scipy.signal.convolve2d 通常比 scipy.ndimage.convolve 慢。

Avoid scipy.signal.convolve if you're working with 2d arrays. It works for the N-d case, but it's suboptimal for 2d arrays, and scipy.signal.convolve2d exists to do the exact same thing a bit more efficiently. The convolution functions in scipy.signal give you control over the output shape using the mode kwarg. (By default, they'll behave just like matlab's conv2.) This is useful for general mathematical convolution, but less useful for image processing. However, scipy.signal.convolve2d is generally slower than scipy.ndimage.convolve.

有很多不同的选择,部分是由于 scipy 的不同子模块中的重复,部分是因为有不同的方法来实现卷积,有不同的性能权衡。

There are a lot of different options partly due to duplication in the different submodules of scipy and partly because there are different ways to implement a convolution that have different performance tradeoffs.

如果你能提供一些关于你的用例的更多细节,我们可以推荐一个更好的解决方案。如果您正在卷积两个大小相同的数组,并且它们已经浮动, fftconvolve 是一个很好的选择。否则, scipy.ndimage.convolve 可能会击败它。

If you can give a bit more detail about your use case, we can recommend a better solution. If you're convolving two arrays of roughly the same size, and they're already floats, fftconvolve is an excellent choice. Otherwise, scipy.ndimage.convolve may beat it.

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

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