实现目标:
实现频率域图像增强方法中的带通带阻滤波器。
代码如下:
"""
Created on 2023/3/20 16:32
@author: liuwenq
实验一_频率域图像增强方法_带通带阻滤波器
"""
import math
import numpy as np
import cv2
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")
plt.rcParams['font.sans-serif'] = ['SimHei'] # 图像展示的字体设置
plt.rcParams['axes.unicode_minus'] = False
# 读入图片并转化为灰度图
origin = cv2.imread('E:/11.PNG')
image = cv2.cvtColor(origin, cv2.COLOR_BGR2GRAY) # 转化为灰度图
def bandpass_filter(image, radius, w, n=1):
"""
带通滤波函数
:param image: 原始图像
:param radius: 频带中心半径
:param w: 带宽
:param n: 阶数
:return: 滤波结果
"""
# 傅里叶变换,得到频谱图,fft是一个三维数组,fft[:, :, 0]为实数部分,fft[:, :, 1]为虚数部分
fft = cv2.dft(np.float32(image), flags=cv2.DFT_COMPLEX_OUTPUT)
dshift = np.fft.fftshift(fft) # 将低频部分转移到中心
rows, cols = image.shape[:2]
mid_row, mid_col = int(rows / 2), int(cols / 2) # 得到中心像素
mask = np.zeros((rows, cols, 2), np.float32) # 构建滤波器,256位,两个通道
for i in range(0, rows):
for j in range(0, cols):
d = math.sqrt(pow(i - mid_row, 2) + pow(j - mid_col, 2)) # 计算(i, j)到中心点的距离
if radius - w / 2 < d < radius + w / 2: #带内范围判定
mask[i, j, 0] = mask[i, j, 1] = 1 # 在范围内则赋1
else:
mask[i, j, 0] = mask[i, j, 1] = 0 # 不在范围内则赋0
fft_filtering = dshift * np.float32(mask) # 将滤波器和频谱图结合,1的保留,0的过滤
ishift = np.fft.ifftshift(fft_filtering) # 将低频部分转移回左上角
image_filtering = cv2.idft(ishift) # 进行傅里叶逆变换
image_filtering = cv2.magnitude(image_filtering[:, :, 0], image_filtering[:, :, 1]) # 转化到空间域内
cv2.normalize(image_filtering, image_filtering, 0, 1, cv2.NORM_MINMAX) # 对逆变换结果进行归一化
return image_filtering
def bandstop_filter(image, radius, w, n=1):
"""
带阻滤波函数
:param image: 原始图像
:param radius: 频带中心半径
:param w: 带宽
:param n: 阶数
:return: 滤波结果
"""
fft = cv2.dft(np.float32(image), flags=cv2.DFT_COMPLEX_OUTPUT)
dshift = np.fft.fftshift(fft) # 将低频部分转移到中心
rows, cols = image.shape[:2]
mid_row, mid_col = int(rows / 2), int(cols / 2) # 得到中心像素
mask = np.zeros((rows, cols, 2), np.float32)
for i in range(0, rows):
for j in range(0, cols):
d = math.sqrt(pow(i - mid_row, 2) + pow(j - mid_col, 2)) # 计算(i, j)到中心点的距离
if radius - w / 2 < d < radius + w / 2:
mask[i, j, 0] = mask[i, j, 1] = 0
else:
mask[i, j, 0] = mask[i, j, 1] = 1
fft_filtering = dshift * np.float32(mask)
ishift = np.fft.ifftshift(fft_filtering)
image_filtering = cv2.idft(ishift) # 傅里叶逆变换
image_filtering = cv2.magnitude(image_filtering[:, :, 0], image_filtering[:, :, 1])
cv2.normalize(image_filtering, image_filtering, 0, 1, cv2.NORM_MINMAX) # 对逆变换结果进行归一化
return image_filtering
image_bandpass_filter = bandpass_filter(image, 30, 56, 1)
image_bandstop_filter = bandstop_filter(image, 30, 30, 1)
# 显示图像
plt.figure(figsize=(10, 8), dpi=200)
plt.suptitle("带通/带阻滤波器", fontsize=40, x=0.5, y=0.2)
plt.subplot(131)
plt.imshow(image, cmap=plt.cm.gray)
plt.title("原始图像", fontsize=18)
plt.subplot(132)
plt.imshow(image_bandpass_filter, cmap=plt.cm.gray)
plt.title("带通图像", fontsize=18)
plt.subplot(133)
plt.imshow(image_bandstop_filter, cmap=plt.cm.gray)
plt.title("带阻图像", fontsize=18)
plt.show()
实现效果: