![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/95b6efc1e8d2deb64e135f1f9a4eefa3.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bdc1832c8c732faa78159e98e3b52bea.png)
低通滤波
# -*- coding:utf-8 -*-
import numpy as np
import cv2
from matplotlib import pyplot as plt
def display(img):
cv2.imshow("img",img)
cv2.waitKey()
cv2.destroyAllWindows()
def operate(img):
img_float32 = np.float32(img)
dft = cv2.dft(img_float32,flags = cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
rows,cols = img.shape
crow ,ccol = int(rows/2),int(cols/2)
mask = np.zeros((rows,cols,2),np.uint8)
mask[crow-30:crow+30,ccol-30:ccol+30] = 1
fshift = dft_shift*mask
f_ishift = np.fft.fftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])
plt.subplot(121)
plt.imshow(img,cmap="gray")
plt.xticks([])
plt.yticks([])
plt.subplot(122)
plt.imshow(img_back,cmap="gray")
plt.xticks([])
plt.yticks([])
plt.show()
def main():
pic = "lena.jpg"
img = cv2.imread(pic,0)
operate(img)
if __name__ == "__main__":
main()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e63a52426565817a3cfdc37e4426dfac.png)
高通滤波
def operate_(img):
img_float32 = np.float32(img)
dft = cv2.dft(img_float32,flags = cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
rows,cols = img.shape
crow ,ccol = int(rows/2),int(cols/2)
mask = np.ones((rows,cols,2),np.uint8)
mask[crow-30:crow+30,ccol-30:ccol+30] = 0
fshift = dft_shift*mask
f_ishift = np.fft.fftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])
plt.subplot(121)
plt.imshow(img,cmap="gray")
plt.xticks([])
plt.yticks([])
plt.subplot(122)
plt.imshow(img_back,cmap="gray")
plt.xticks([])
plt.yticks([])
plt.show()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b7932a39d28d9b53bcfcd9269a93a652.png)