低频、高频
频域滤波
import cv2
import numpy as np
import matplotlib.pyplot as plt
#1.导入图片并进行傅里叶变换
img = cv2.imread('./hanser.jpg',0)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
#2.剔除低频,通高频
rows,cols = img.shape
crow,ccol = int(rows/2),int(cols/2)
fshift[crow-30:crow+30,ccol-30:ccol+30] = 0
#3.反傅里叶变换
ishift = np.fft.ifftshift(fshift)
iimg = np.abs(np.fft.ifft2(ishift))
#4.图片展示
plt.subplot(121),plt.imshow(img,cmap='gray')
plt.title('original'),plt.axis('off')
plt.subplot(122),plt.imshow(iimg,cmap='gray')
plt.title('iimg'),plt.axis('off')
plt.show()
低通滤波器(掩膜)
import cv2
import numpy as np
import matplotlib.pyplot as plt
#1.导入图片并进行傅里叶变换
img = cv2.imread('./hanser.jpg',0)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
#2.制作掩膜图像,通低频
rows,cols = img.shape
crow,ccol = int(rows/2),int(cols/2)
mask = np.zeros((rows,cols),np.uint8)
mask[crow-30:crow+30,ccol-30:ccol+30] = 1
fshift = mask*fshift
#3.反傅里叶变换
ishift = np.fft.ifftshift(fshift)
iimg = np.abs(np.fft.ifft2(ishift))
#4.图片展示
plt.subplot(121),plt.imshow(img,cmap='gray')
plt.title('original'),plt.axis('off')
plt.subplot(122),plt.imshow(iimg,cmap='gray')
plt.title('iimg'),plt.axis('off')
plt.show()