1.引言
在实际应用中,图像经常会因为运动模糊或噪声干扰而质量下降。图像恢复技术旨在从退化的图像中重建出清晰的原始图像。本文将使用Python实现两种经典的图像恢复算法——逆滤波和维纳滤波,并通过实验对比它们的恢复效果。
2.实验环境
依赖库:
- OpenCV(cv2):图像读取与处理
- Numpy:矩阵运算
- Matplotlib:结果可视化
- SciPy:傅里叶变换与卷积运算
3.实验原理
3.1 图像退化模型
图像退化过程可以表示为:
g(x,y)=h(x,y)∗f(x,y)+η(x,y)
- g(x,y):退化后的图像
- h(x,y):点扩散函数(PSF,模拟运动模糊)
- f(x,y):原始图像
- η(x,y):加性噪声
3.2 恢复方法
- 逆滤波
直接在频域进行逆运算:
缺点:对噪声敏感,易放大高频噪声。
- 维纳滤波
引入信噪比(SNR)约束,抑制噪声:
优点:鲁棒性强,适合噪声环境。
4. 实验步骤与代码实现
4.1 运动模糊仿真(motion_process函数)
def motion_process(image_size, motion_angle):
PSF = np.zeros(image_size)
center_position = (image_size[0] - 1) / 2
slope_tan = math.tan(motion_angle * math.pi / 180)
slope_cot = 1 / slope_tan
if slope_tan <= 1:
for i in range(15):
offset = round(i * slope_tan)
PSF[int(center_position + offset), int(center_position - offset)] = 1
else:
for i in range(15):
offset = round(i * slope_cot)
PSF[int(center_position - offset), int(center_position + offset)] = 1
return PSF / PSF.sum()
- 功能:模拟运动模糊的效果
- PSF是点扩散函数(Point Spread Function),表示运动模糊的方向和程度
4.2运动模糊处理(make_blurred函数)
def make_blurred(input_image, PSF, eps):
input_fft = np.fft.fft2(input_image)
PSF_fft = np.fft.fft2(PSF) + eps
blurred = np.fft.ifft2(input_fft * PSF_fft)
return np.abs(np.fft.fftshift(blurred))
- 功能:对输入图像应用运动模糊
4.3 逆滤波 (inverse 函数)
def inverse(input_image, PSF, eps):
input_fft = np.fft.fft2(input_image)
PSF_fft = np.fft.fft2(PSF) + eps # 噪声功率
result = np.fft.ifft2(input_fft / PSF_fft)
result = np.abs(np.fft.fftshift(result))
return result
- 功能:使用逆滤波恢复原始图像
4.4 维纳滤波 (wiener 函数)
def wiener(input_image, PSF, eps, K=0.01):
input_fft = np.fft.fft2(input_image)
PSF_fft = np.fft.fft2(PSF) + eps
PSF_fft_conj = np.conj(PSF_fft)
PSF_fft_abs_sq = np.abs(PSF_fft) ** 2
PSF_fft_1 = PSF_fft_conj / (PSF_fft_abs_sq + K)
result = np.fft.ifft2(input_fft * PSF_fft_1)
result = np.abs(np.fft.fftshift(result))
return resultCopy
- 功能:使用维纳滤波恢复原始图像
4.5主程序 (__main__ 部分)
if __name__ == "__main__":
# 读取图像并转换为灰度图
image = cv2.imread('lena.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 获取图像尺寸
img_h, img_w = image.shape[:2]
# 进行运动模糊处理
PSF = motion_process((img_h, img_w), 60)
blurred = np.abs(make_blurred(image, PSF, 1e-3))
# 显示运动模糊图像
plt.figure(1)
plt.subplot(131), plt.axis('off')
plt.title("Motion blurred")
plt.imshow(blurred, cmap='gray')
# 对运动模糊图像进行逆滤波
result_inverse = inverse(blurred, PSF, 1e-3)
plt.subplot(132), plt.axis('off')
plt.title("Inverse filter")
plt.imshow(result_inverse, cmap='gray')
# 对运动模糊图像进行维纳滤波
result_wiener = wiener(blurred, PSF, 1e-3)
plt.subplot(133), plt.axis('off')
plt.title("Wiener filter (k=0.01)")
plt.imshow(result_wiener, cmap='gray')
plt.show()
# 对运动模糊图像添加噪声
blurred_noisy = blurred + 0.1 * blurred.std() * \\
np.random.standard_normal(blurred.shape)
# 显示添加噪声的运动模糊图像
plt.figure(2)
plt.subplot(131), plt.axis('off')
plt.title("Motion & noisy blurred")
plt.imshow(blurred_noisy, cmap='gray')
# 对添加噪声的图像进行逆滤波
result_inverse_noisy = inverse(blurred_noisy, PSF, 0.1 + 1e-3)
plt.subplot(132), plt.axis('off')
plt.title("Inverse filter")
plt.imshow(result_inverse_noisy, cmap='gray')
# 对添加噪声的图像进行维纳滤波
result_wiener_noisy = wiener(blurred_noisy, PSF, 0.1 + 1e-3)
plt.subplot(133), plt.axis('off')
plt.title("Wiener filter (k=0.01)")
plt.imshow(result_wiener_noisy, cmap='gray')
plt.show()
- 功能1:读取图像并转换为灰度图
- 功能2:对图像进行运动模糊处理
- 功能3:分别使用逆滤波和维纳滤波恢复图像
- 功能4:对运动模糊图像添加噪声,并再次使用逆滤波和维纳滤波恢复图像
5.实验结果分析
5.1无噪声坏境下的恢复效果
5.2有噪声环境下的恢复效果
6.结论
- 逆滤波适合无噪声或低噪声环境,计算简单但对噪声敏感
- 维纳滤波在噪声环境下表现优异,通过调节K值可平衡去噪与细节保留
- 实际应用中需根据退化模型选择合适方法,或结合深度学习进一步优化