Does Python or any of its modules have an equivalent of MATLAB's conv2 function? More specifically, I'm interested in something that does the same computation as conv2(A, B, 'same')
in MATLAB.
Python或它的任何模块是否具有与MATLAB的conv2函数等价的功能?更具体地说,我对在MATLAB中做与conv2(A, B, 'same')相同的计算感兴趣。
4 个解决方案
#1
4
Looks like scipy.signal.convolve2d is what you're looking for.
看起来像scipy.signal。convolve2d是你要找的东西。
#2
2
While the other answers already mention scipy.signal.convolve2d
as an equivalent, i found that the results do differ when using mode='same'
.
而其他的答案已经提到了scipy.signal。convolve2d作为等效的,我发现当使用mode='same'时,结果会有所不同。
While Matlab's conv2
results in artifacts on the bottom and right of an image, scipy.signal.convolve2d
has the same artifacts on the top and left of an image.
而Matlab的conv2结果是在图像的底部和右侧的工件,scipy.signal。convolve2d在图像的顶部和左侧有相同的工件。
See these links for plots showing the behaviour (not enough reputation to post the images directly):
查看这些图的链接,显示这些行为(没有足够的声望直接发布图像):
Upper left corner of convoluted Barbara
盘旋的芭芭拉的左上角。
Lower right corner of convoluted Barbara
芭芭拉的右下角。
The following wrapper might not be very efficient, but solved the problem in my case by rotating both input arrays and the output array, each by 180 degrees:
下面的包装可能不是很有效,但是在我的情况下,通过旋转输入数组和输出数组来解决这个问题,每一个都是180度:
import numpy as np
from scipy.signal import convolve2d
def conv2(x, y, mode='same')
return np.rot90(convolve2d(np.rot90(x, 2), np.rot90(y, 2), mode=mode), 2)
#3
1
scipy.ndimage.convolve
does it in n dimensions.
它在n维中。
#4
1
You must provide an offset for each non-singleton dimension to reproduce the results of Matlab's conv2. A simple implementation supporting the 'same' option, only, could be made like this
您必须为每个非单例维度提供一个偏移量来复制Matlab的conv2结果。一个支持“相同”选项的简单实现,只能这样做。
import numpy as np
from scipy.ndimage.filters import convolve
def conv2(x,y,mode='same'):
"""
Emulate the function conv2 from Mathworks.
Usage:
z = conv2(x,y,mode='same')
TODO:
- Support other modes than 'same' (see conv2.m)
"""
if not(mode == 'same'):
raise Exception("Mode not supported")
# Add singleton dimensions
if (len(x.shape) < len(y.shape)):
dim = x.shape
for i in range(len(x.shape),len(y.shape)):
dim = (1,) + dim
x = x.reshape(dim)
elif (len(y.shape) < len(x.shape)):
dim = y.shape
for i in range(len(y.shape),len(x.shape)):
dim = (1,) + dim
y = y.reshape(dim)
origin = ()
# Apparently, the origin must be set in a special way to reproduce
# the results of scipy.signal.convolve and Matlab
for i in range(len(x.shape)):
if ( (x.shape[i] - y.shape[i]) % 2 == 0 and
x.shape[i] > 1 and
y.shape[i] > 1):
origin = origin + (-1,)
else:
origin = origin + (0,)
z = convolve(x,y, mode='constant', origin=origin)
return z
#1
4
Looks like scipy.signal.convolve2d is what you're looking for.
看起来像scipy.signal。convolve2d是你要找的东西。
#2
2
While the other answers already mention scipy.signal.convolve2d
as an equivalent, i found that the results do differ when using mode='same'
.
而其他的答案已经提到了scipy.signal。convolve2d作为等效的,我发现当使用mode='same'时,结果会有所不同。
While Matlab's conv2
results in artifacts on the bottom and right of an image, scipy.signal.convolve2d
has the same artifacts on the top and left of an image.
而Matlab的conv2结果是在图像的底部和右侧的工件,scipy.signal。convolve2d在图像的顶部和左侧有相同的工件。
See these links for plots showing the behaviour (not enough reputation to post the images directly):
查看这些图的链接,显示这些行为(没有足够的声望直接发布图像):
Upper left corner of convoluted Barbara
盘旋的芭芭拉的左上角。
Lower right corner of convoluted Barbara
芭芭拉的右下角。
The following wrapper might not be very efficient, but solved the problem in my case by rotating both input arrays and the output array, each by 180 degrees:
下面的包装可能不是很有效,但是在我的情况下,通过旋转输入数组和输出数组来解决这个问题,每一个都是180度:
import numpy as np
from scipy.signal import convolve2d
def conv2(x, y, mode='same')
return np.rot90(convolve2d(np.rot90(x, 2), np.rot90(y, 2), mode=mode), 2)
#3
1
scipy.ndimage.convolve
does it in n dimensions.
它在n维中。
#4
1
You must provide an offset for each non-singleton dimension to reproduce the results of Matlab's conv2. A simple implementation supporting the 'same' option, only, could be made like this
您必须为每个非单例维度提供一个偏移量来复制Matlab的conv2结果。一个支持“相同”选项的简单实现,只能这样做。
import numpy as np
from scipy.ndimage.filters import convolve
def conv2(x,y,mode='same'):
"""
Emulate the function conv2 from Mathworks.
Usage:
z = conv2(x,y,mode='same')
TODO:
- Support other modes than 'same' (see conv2.m)
"""
if not(mode == 'same'):
raise Exception("Mode not supported")
# Add singleton dimensions
if (len(x.shape) < len(y.shape)):
dim = x.shape
for i in range(len(x.shape),len(y.shape)):
dim = (1,) + dim
x = x.reshape(dim)
elif (len(y.shape) < len(x.shape)):
dim = y.shape
for i in range(len(y.shape),len(x.shape)):
dim = (1,) + dim
y = y.reshape(dim)
origin = ()
# Apparently, the origin must be set in a special way to reproduce
# the results of scipy.signal.convolve and Matlab
for i in range(len(x.shape)):
if ( (x.shape[i] - y.shape[i]) % 2 == 0 and
x.shape[i] > 1 and
y.shape[i] > 1):
origin = origin + (-1,)
else:
origin = origin + (0,)
z = convolve(x,y, mode='constant', origin=origin)
return z