[英]Shapes not matching in numpy.convolve
Error message: 错误信息:
operands could not be broadcast together with shapes (603) (613)
What should I do? 我该怎么办?
Do both of the list need to be the same length? 两个列表的长度都必须相同吗?
Or should I zero-pad it? 还是应该零填充?
Here's my code: 这是我的代码:
def gaussian_smooth1(img, sigma):
'''
Do gaussian smoothing with sigma.
Returns the smoothed image.
'''
result = np.zeros_like(img)
#get the filter
filter = gaussian_filter(sigma)
#get the height and width of img
width = len(img[0])
height = len(img)
#smooth every color-channel
for c in range(3):
#smooth the 2D image img[:,:,c]
#tip: make use of numpy.convolve
for x in range(height):
result[x,:,c] = np.convolve(filter,img[x,:,c])
for y in range(width):
result[:,y,c] = np.convolve(filter,img[:,y,c])
return result
The problem arises because you do not specify the right mode
. 出现问题是因为您没有指定正确的
mode
。
Read it up in the documentation: 在文档中阅读:
numpy.convolve numpy.convolve
The default for numpy.convolve is mode='full'
. numpy.convolve的默认值为
mode='full'
。
This returns the convolution at each point of overlap, with an output shape of (N+M-1,).
这将在每个重叠点返回卷积,输出形状为(N + M-1,)。
N
is the size of the input array, M
is the size of the filter. N
是输入数组的大小, M
是过滤器的大小。 So the output is larger than the input. 因此,输出大于输入。
Instead you want to use np.convolve(filter,img[...],mode='same')
. 相反,您想使用
np.convolve(filter,img[...],mode='same')
。
Also have a look at scipy.convolve which allows 2D convolution using the FFT. 还可以看看scipy.convolve ,它允许使用FFT进行2D卷积。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.