Python OpenCV图像高通滤波器和低通滤波器介绍
作为本际云服务器推荐网的小编小本本,今天给大家详细介绍一下Python OpenCV图像高通滤波器和低通滤波器的使用方法。

详细编码案例
低通滤波
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('./moon.jpg',flags=cv2.IMREAD_GRAYSCALE)
img1 = img / 255
#进行傅里叶变换,时域——>频域
dtf = cv2.dft(img1, flags=cv2.DFT_COMPLEX_OUTPUT)
#移动低频波到中心位置
dft_shift = np.fft.fftshift(dtf)
#低通滤波
h,w=img.shape
#图像中心点即低频波所在位置
h2,w2=h//2,w//2
mask=np.zeros((h,w,2),dtype=np.uint8)
#选取长宽为100的区域的低频部分为1,其余部分为0
mask[h2-50:h2+50, w2-50:w2+50] = 1
#低频部分保留,其余部分*0被滤掉
dft_shift *= mask
#傅里叶逆变换,频域——>时域
ifft_shift2 = np.fft.ifftshift(dft_shift)
result = cv2.idft(ifft_shift2)
#创建显示窗口,显示原图
plt.figure(figsize=(12,9))
plt.subplot(121)
plt.imshow(img, cmap='gray')
#创建显示窗口,显示低通滤波后的图像
plt.subplot(122)
plt.imshow(result[:,:,0], cmap='gray')
plt.show()
高通滤波
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('./moon.jpg',flags=cv2.IMREAD_GRAYSCALE)
img1 = img / 255
#进行傅里叶变换,时域——>频域
dtf = cv2.dft(img1,flags=cv2.DFT_COMPLEX_OUTPUT)
#移动低频波到中心位置
dft_shift = np.fft.fftshift(dtf)
#高通滤波
h, w = img.shape
#图像中心点即低频波所在位置
h2, w2 = h//2, w//2
#选取长宽为100的区域的低频部分为0,其余高频部分为1
dft_shift[h2-5:h2+5, w2-5:w2+5] = 0
#傅里叶逆变换,频域——>时域
ifft_shift2 = np.fft.ifftshift(dft_shift)
result = cv2.idft(ifft_shift2)
#创建显示窗口,显示原图
plt.figure(figsize=(12,9))
plt.subplot(121)
plt.imshow(img, cmap='gray')
#创建显示窗口,显示低通滤波后的图像
plt.subplot(122)
plt.imshow(result[:,:,0], cmap='gray')
plt.show()
结果展示
改变滤波区域的大小可以改变滤波的程度,可以修改代码来实现。
低通滤波

高通滤波

结语
通过本文的介绍,大家已经掌握了Python OpenCV图像高通滤波器和低通滤波器的使用方法,希望对大家的学习和工作具有帮助。
原创文章,作者:小编小本本,如若转载,请注明出处:https://www.benjiyun.com/yunzhujiyunwei/vps-yunwei/5769.html
