错误消息:
operands could not be broadcast together with shapes (603) (613)我该怎么办?
这两个列表需要相同的长度吗?
或者我应该对其进行零填充?
下面是我的代码:
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发布于 2013-02-20 09:12:11
出现这个问题是因为您没有指定正确的mode。
请在文档中详细阅读:
numpy.convolve
numpy.convolve的默认值为mode='full'。
这将返回每个重叠点处的卷积,输出形状为(N+M-1,)。
N是输入数组的大小,M是过滤器的大小。所以输出大于输入。
相反,您希望使用np.convolve(filter,img[...],mode='same')。
还可以看看scipy.convolve,它允许使用快速傅立叶变换进行2D卷积。
https://stackoverflow.com/questions/14970260
复制相似问题