2D Convolution in Python similar to Matlab's conv2

后端 未结 3 1477
醉话见心
醉话见心 2020-12-03 14:10

I have been trying to do Convolution of a 2D Matrix using SciPy, and Numpy but have failed. For SciPy I tried, sepfir2d and scipy.signal.convolve and Convolve2D for Numpy. I

3条回答
  •  没有蜡笔的小新
    2020-12-03 15:08

    scipy's convolved1d() does what you want, just treats the edges a bit differently:

    sp.ndimage.filters.convolve1d(A,[0.707,0.707],axis=1,mode='constant')
    

    will give you:

    array([[ 6.363,  6.363,  6.363,  2.828],
           [ 3.535,  3.535,  3.535,  1.414],
           [ 6.363,  6.363,  6.363,  2.828],
           [ 3.535,  3.535,  3.535,  1.414]])
    

    If you want the exact same result, just add a column of zeros to A like this:

    sp.ndimage.filters.convolve1d(np.c_[np.zeros((4,1)),A],[0.707,0.707],axis=1,mode='constant')
    

    and you will get:

    array([[ 3.535,  6.363,  6.363,  6.363,  2.828],
           [ 2.121,  3.535,  3.535,  3.535,  1.414],
           [ 3.535,  6.363,  6.363,  6.363,  2.828],
           [ 2.121,  3.535,  3.535,  3.535,  1.414]])
    

    From my experience you can do in scipy/numpy most of what you do in Matlab very easily (and more).

提交回复
热议问题