22. Scipy Tutorial-图像的卷积fftconvolve
scipy的signal模块里提供fftconvolve可以对图像进行快速卷积运算,一般在图像的尺寸大于500时会很快,但尺寸较小的时候会慢一些。
#coding:utf-8
import numpy as np
from scipy import misc, ndimage,signal
import matplotlib.pyplot as plt
import scipy
print “scipy version is:”, scipy.__version__
src = misc.face()
print src.shape
misc.imsave(‘faceRGB.png’, src)
img = ndimage.imread(“faceRGB.png”, mode = “L”)
misc.imsave(‘faceGray.png’, img)
k = np.zeros((5,5))
k[1][0] = 1
k[4][2] = 1
k[3][4] = 1
img_con = signal.convolve(img, k, mode = “valid”)
img_fft = signal.fftconvolve(img, k, mode = “valid”)
if (img_con == img_fft).all():
print “same”
else:
print “diff”
plt.gray()
plt.subplot(221, title = “orginal”)
plt.imshow(img)
plt.subplot(222, title = “fftconvolve”)
plt.imshow(img_fft)
plt.subplot(224, title = “convolve”)
plt.imshow(img_con)
plt.show()
执行结果: