【课程作业】实现高斯低通滤波器并与理想低通滤波器进行效果比较
生活随笔
收集整理的這篇文章主要介紹了
【课程作业】实现高斯低通滤波器并与理想低通滤波器进行效果比较
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
要求
嘗試設計巴特沃斯或高斯低通濾波器對下面的圖像進行處理,比較不同參數下的結果,并與理想濾波器的結果進行比較。
高斯低通濾波器實現原理
高斯低通濾波器公式如下:
根據不同的sigma值,生成模糊后圖像
高斯低通濾波器實現代碼
import cv2 from matplotlib import pyplot as plt import numpy as npimgGray = cv2.imread(r"D:\Project\PythonProject\GraphicAnalysis\class4\zuoye4.png", flags=0) # flags=0 讀取為灰度圖像imgFloat32 = np.float32(imgGray) dft = cv2.dft(imgFloat32, flags=cv2.DFT_COMPLEX_OUTPUT) dftShift = np.fft.fftshift(dft) plt.figure(figsize=(10, 10)) rows, cols = imgGray.shape[:2] sigma2 = np.arange(0.05, 0.25, 0.05) for i in range(4):x, y = np.mgrid[-1:1:2 / rows, -1:1:2 / cols]z = np.exp(-(x ** 2 + y ** 2) / (2 * sigma2[i]))# 歸一化為 [0,255]z = np.uint8(cv2.normalize(z, None, 0, 255, cv2.NORM_MINMAX)) maskGauss = np.zeros((rows, cols, 2), np.uint8)maskGauss[:,:,0] = zmaskGauss[:,:,1] = zdftTrans = dftShift * maskGauss ishift = np.fft.ifftshift(dftTrans) idft = cv2.idft(ishift) imgRebuild = cv2.magnitude(idft[:,:,0], idft[:,:,1]) plt.subplot(2,4,i+1), plt.title("sigma^2={:.2f}".format(sigma2[i])), plt.axis('off')plt.imshow(maskGauss[:,:,0], cmap='gray')plt.subplot(2,4,i+5), plt.title(" "), plt.axis('off')plt.imshow(imgRebuild, cmap='gray')plt.tight_layout() plt.show()高斯低通濾波器實現效果
與理想低通濾波對比
理想低通濾波實現代碼
import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread("D:/Project/PythonProject/GraphicAnalysis/class4/zuoye4.png")[:,:,0] #傅里葉變換 dft = cv2.dft(np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT) fshift = 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-60:crow+60, ccol-60:ccol+60] = 1 #掩膜圖像和頻譜圖像乘積 f = fshift * mask print (f.shape, fshift.shape, mask.shape) #傅里葉逆變換 ishift = np.fft.ifftshift(f) iimg = cv2.idft(ishift) res = cv2.magnitude(iimg[:,:,0], iimg[:,:,1]) #顯示原始圖像和低通濾波處理圖像 plt.subplot(131), plt.imshow(img, 'gray'), plt.title('Original Image') plt.axis('off') plt.subplot(132), plt.imshow(res, 'gray'), plt.title('Result Image') plt.axis('off') plt.subplot(133), plt.imshow(mask[:,:,1], 'gray'), plt.title('Mask') plt.axis('off') plt.show()理想低通濾波實現效果
分析
分析可知,理想低通濾波器有明顯的振鈴現象,而高斯低通濾波器沒有振鈴現象,高斯低通濾波器實現效果較好。
總結
以上是生活随笔為你收集整理的【课程作业】实现高斯低通滤波器并与理想低通滤波器进行效果比较的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果手机扬声器没声音怎么办_手机扬声器盲
- 下一篇: FFplay文档解读-46-视频源,视频