In [1]:
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
from scipy import misc
% matplotlib inline

In [4]:
face_img = misc.face()
misc.imsave('face.png', face_img)
print (face_img.shape)
plt.imshow(face_img[:,:,:])


(768, 1024, 3)
Out[4]:
<matplotlib.image.AxesImage at 0x11bc56f60>

In [5]:
kernel1 = np.array([[ -1, -2, -1],
                    [  0,  0,  0],
                    [  1,  2,  1]])

kernel2 = np.array([[ -1, 0, 1],
                    [ -2, 0, 2],
                    [ -1, 0, 1]])

In [6]:
grad1 = signal.convolve2d(face_img[:,:,1], kernel1, boundary='symm', mode='same')
grad2 = signal.convolve2d(face_img[:,:,1], kernel2, boundary='symm', mode='same')
print (grad1.shape)
print (grad2.shape)


(768, 1024)
(768, 1024)

In [7]:
fig, (ax_orig, ax_mag1, ax_mag2) = plt.subplots(3, 1, figsize=(21, 21))
ax_orig.imshow(face_img[:,:,0], cmap='gray')
ax_orig.set_title('Original')
ax_orig.set_axis_off()
ax_mag1.imshow(np.absolute(grad1), cmap='gray')
ax_mag1.set_title('Convolution')
ax_mag1.set_axis_off()
ax_mag2.imshow(np.absolute(grad2), cmap='gray')
ax_mag2.set_title('Convolution')
ax_mag2.set_axis_off()
fig.show()


/Users/paulrad/anaconda/lib/python3.5/site-packages/matplotlib/figure.py:397: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
  "matplotlib is currently using a non-GUI backend, "

In [ ]: