Convolve2d just by using Numpy

前端 未结 3 743
孤独总比滥情好
孤独总比滥情好 2020-12-02 13:52

I am studying image-processing using Numpy and facing a problem with filtering with convolution.

I would like to convolve a gray-scale image. (convolve a 2d A

3条回答
  •  没有蜡笔的小新
    2020-12-02 14:24

    Cleaned up using as_strided and @Crispin 's einsum trick from above. Enforces the filter size into the expanded shape. Should even allow non-square inputs if the indices are compatible.

    def conv2d(a, f):
        s = f.shape + tuple(np.subtract(a.shape, f.shape) + 1)
        strd = numpy.lib.stride_tricks.as_strided
        subM = strd(a, shape = s, strides = a.strides * 2)
        return np.einsum('ij,ijkl->kl', f, subM)
    

提交回复
热议问题