首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >numpy.convolve中的形状不匹配

numpy.convolve中的形状不匹配
EN

Stack Overflow用户
提问于 2013-02-20 08:31:31
回答 1查看 1.4K关注 0票数 2

错误消息:

代码语言:javascript
复制
operands could not be broadcast together with shapes (603) (613)

我该怎么办?

这两个列表需要相同的长度吗?

或者我应该对其进行零填充?

下面是我的代码:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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卷积。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14970260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档