Last active
May 14, 2020 19:03
-
-
Save eduardo4jesus/0dc157d505152f41c3651fe5e639ca8a to your computer and use it in GitHub Desktop.
Difference in Spatial Convolution: Numpy vs Matlab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = [ 0 3 1 -4] | |
b = [-1 -1 -1 3] | |
c = cconv(a, b, 4) % [12 4 -16 0] | |
l = conv(a, b, 'same') % [ -4 0 12 7] | |
f = conv(a, b) % [0 -3 -4 0 12 7 -12] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import scipy as sp | |
import scipy.signal | |
def spatial_full(img, kernel): | |
return sp.signal.convolve(img, kernel, mode='full', method="direct") | |
def spatial_linear(img, kernel): | |
return sp.signal.convolve(img, kernel, mode='same', method="direct") | |
def spatial_circ(img, kernel): | |
return sp.ndimage.convolve1d(img, kernel, mode='wrap') | |
a = np.array([ 0, 3, 1, -4]) | |
b = np.array([-1, -1, -1, 3]) | |
c = spatial_circ(a, b) # array([-16, 0, 12, 4]) | |
l = spatial_linear(a, b) # array([-3, -4, 0, 12]) | |
f = spatial_full(a, b) # array([ 0, -3, -4, 0, 12, 7, -12]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment