跟我学Python图像处理丨傅里叶变换之高通滤波和低通滤波( 二 )


跟我学Python图像处理丨傅里叶变换之高通滤波和低通滤波

文章插图
那么 , 如何构造该滤波图像呢?如下图所示 , 滤波图像是通过低通滤波器和频谱图像形成 。其中低通滤波器中心区域为白色255 , 其他区域为黑色0 。
跟我学Python图像处理丨傅里叶变换之高通滤波和低通滤波

文章插图
低通滤波器主要通过矩阵设置构造 , 其核心代码如下:
rows, cols = img.shapecrow,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通过低通滤波器将模糊图像的完整代码如下所示:
# -*- coding: utf-8 -*-import cv2import numpy as npfrom matplotlib import pyplot as plt#读取图像img = cv2.imread('lena.bmp', 0)#傅里叶变换dft = cv2.dft(np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT)fshift = np.fft.fftshift(dft)#设置低通滤波器rows, cols = img.shapecrow,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#掩膜图像和频谱图像乘积f = fshift * maskprint f.shape, fshift.shape, mask.shape#傅里叶逆变换ishift = np.fft.ifftshift(f)iimg = cv2.idft(ishift)res = cv2.magnitude(iimg[:,:,0], iimg[:,:,1])#显示原始图像和低通滤波处理图像plt.subplot(121), plt.imshow(img, 'gray'), plt.title('Original Image')plt.axis('off')plt.subplot(122), plt.imshow(res, 'gray'), plt.title('Result Image')plt.axis('off')plt.show()输出结果如图所示 , 第一幅图为原始“Lena”图 , 第二幅图为低通滤波器模糊处理后的图像 。
跟我学Python图像处理丨傅里叶变换之高通滤波和低通滤波

文章插图
点击关注 , 第一时间了解华为云新鲜技术~

经验总结扩展阅读