繁体   English   中英

numpy.convolve中的形状不匹配

[英]Shapes not matching in numpy.convolve

错误信息:

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

出现问题是因为您没有指定正确的mode
在文档中阅读:
numpy.convolve

numpy.convolve的默认值为mode='full'

这将在每个重叠点返回卷积,输出形状为(N + M-1,)。

N是输入数组的大小, M是过滤器的大小。 因此,输出大于输入。

相反,您想使用np.convolve(filter,img[...],mode='same')

还可以看看scipy.convolve ,它允许使用FFT进行2D卷积。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM