頻域濾波
頻域濾波是在頻率域對圖像作處理的一種方法。步驟以下:ide
濾波器大小和頻譜大小相同,相乘便可獲得新的頻譜。函數
濾波後結果顯示,低通濾波去掉了高頻信息,即細節信息,留下的低頻信息表明了概貌。經常使用的例子,好比美圖秀秀的磨皮,去掉了臉部細節信息(痘坑,痘印,暗斑等)。高通濾波則相反。ui
高通/低通濾波
1.理想的高/低通濾波
顧名思義,高通濾波器爲:讓高頻信息經過,過濾低頻信息;低通濾波相反。spa
理想的低通濾波器模板爲:3d
其中,D0表示通帶半徑,D(u,v)是到頻譜中心的距離(歐式距離),計算公式以下:code
M和N表示頻譜圖像的大小,(M/2,N/2)即爲頻譜中心blog
理想的高通濾波器與此相反,1減去低通濾波模板便可。io
部分代碼:模板


# 定義函數,顯示濾波器模板 def showTemplate(template): temp = np.uint8(template*255) cv2.imshow('Template', temp) return # 定義函數,顯示濾波函數 def showFunction(template): row, col = template.shape row = np.uint16(row/2) col = np.uint16(col/2) y = template[row, col:] x = np.arange(len(y)) plt.plot(x, y, 'b-', linewidth=2) plt.axis([0, len(x), -0.2, 1.2]) plt.show() return # 定義函數,理想的低通/高通濾波模板 def Ideal(src, d0, ftype): template = np.zeros(src.shape, dtype=np.float32) # 構建濾波器 r, c = src.shape for i in range(r): for j in range(c): distance = np.sqrt((i - r/2)**2 + (j - c/2)**2) if distance < d0: template[i, j] = 1 else: template[i, j] = 0 if ftype == 'high': template = 1 - template return template
2. Butterworth高/低通濾波
Butterworth低通濾波器函數爲:class
從函數圖上看,更圓滑,用冪係數n能夠改變濾波器的形狀。n越大,則該濾波器越接近於理想濾波器
1減去低通濾波模板便可獲得高通濾波模板
部分代碼:


# 定義函數,巴特沃斯高/低通濾波模板 def Butterworth(src, d0, n, ftype): template = np.zeros(src.shape, dtype=np.float32) # 構建濾波器 r, c = src.shape for i in np.arange(r): for j in np.arange(c): distance = np.sqrt((i - r/2)**2 + (j - c/2)**2) template[i, j] = 1/(1 + (distance/d0)**(2*n)) # Butterworth 濾波函數 template[i, j] = np.e ** (-1 * (distance**2 / (2 * d0**2))) # Gaussian濾波函數 if ftype == 'high': template = 1 - template return template
3. Gaussian高/低通濾波
Guassian低通濾波器函數爲:
1減去低通濾波模板便可獲得高通濾波模板
部分代碼:


# 定義函數,高斯高/低通濾波模板 def Gaussian(src, d0, ftype): template = np.zeros(src.shape, dtype=np.float32) # 構建濾波器 r, c = src.shape for i in np.arange(r): for j in np.arange(c): distance = np.sqrt((i - r / 2) ** 2 + (j - c / 2) ** 2) template[i, j] = np.e ** (-1 * (distance ** 2 / (2 * d0 ** 2))) # Gaussian濾波函數 if ftype == 'high': template = 1 - template return template