Skip to content

Instantly share code, notes, and snippets.

@yangyushi
Created March 5, 2019 11:07
Show Gist options
  • Save yangyushi/52654ee7dbcd12ff89189ace0dadf414 to your computer and use it in GitHub Desktop.
Save yangyushi/52654ee7dbcd12ff89189ace0dadf414 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage
from scipy import signal
a = np.ones((20, 20)) * 0
a[5, 5] = 1
a[10, 10] = 1
a[15, 15] = 1
a = ndimage.gaussian_filter(a, 1)
s = np.zeros((3, 3))
s[1, 1] = 1
s = ndimage.gaussian_filter(s, 1)
s[0, :] = -0.1
print(a.shape, s.shape)
c = signal.correlate(a, s, mode='valid')
d = signal.convolve(a, s, mode='valid')
plt.subplot(221).imshow(a)
plt.title('$f$')
plt.subplot(222).imshow(s)
plt.title('$h$')
plt.subplot(223).imshow(d)
plt.title('convolve:\nblend $h$ into $f$')
plt.subplot(224).imshow(c)
plt.title('correlate:\nwhich pixel in $f$ looks like $h$')
plt.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment