实现二维图像的卷积运算
import numpy as np
from scipy import signal
from scipy import misc
import matplotlib.pyplot as plt
face = misc.face(gray=True) #face.dat
scharr = np.array([[ -3-3j, 0-10j, +3 -3j],
[-10+0j, 0+ 0j, +10 +0j],
[ -3+3j, 0+10j, +3 +3j]]) # Gx + j*Gy
grad = signal.convolve2d(face, scharr, boundary='symm', mode='same')
fig, (ax_orig, ax_mag) = plt.subplots(1, 2, figsize=(10, 6))
plt.rcParams['font.sans-serif']='SimHei'
ax_orig.imshow(face, cmap='gray')
ax_orig.set_title('first')
ax_orig.set_axis_off()
ax_mag.imshow(np.absolute(grad), cmap='gray')
ax_mag.set_title('second')
ax_mag.set_axis_off()
fig.show()
当把face = misc.face(gray=True) 改成face = misc.ascent()
以照片形式 face=plt.imread(‘D:/caomei.jpg’) # np.ndarray BGR uint8