图像去噪算法的程序
时间: 2023-10-12 12:11:31 浏览: 37
这里给出一个常见的基于小波变换和软阈值去噪的图像去噪算法的 Python 程序示例:
```python
import cv2
import numpy as np
import pywt
def wavelet_denoising(img):
# 将图像转换为浮点数类型
img = np.float32(img)
# 将图像进行小波变换
coeffs = pywt.dwt2(img, 'haar')
# 将小波系数进行分离
LL, (LH, HL, HH) = coeffs
# 对小波系数进行软阈值去噪
LH = pywt.threshold(LH, np.std(LH) / 2, mode='soft', substitute=0)
HL = pywt.threshold(HL, np.std(HL) / 2, mode='soft', substitute=0)
HH = pywt.threshold(HH, np.std(HH) / 2, mode='soft', substitute=0)
# 将小波系数合并
coeffs = LL, (LH, HL, HH)
# 将小波系数进行反变换
img_denoised = pywt.idwt2(coeffs, 'haar')
# 将图像还原为整数类型
img_denoised = np.uint8(img_denoised)
return img_denoised
# 读入原始图像
img = cv2.imread('lena.jpg',0)
# 对原始图像进行去噪
img_denoised = wavelet_denoising(img)
# 显示原始图像和去噪后的图像
cv2.imshow('Original Image', img)
cv2.imshow('Denoised Image', img_denoised)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
该程序首先读入一张图像,然后将图像进行小波变换,将小波系数进行软阈值去噪,最后将小波系数进行反变换,得到去噪后的图像。程序中使用的是 Haar 小波变换,软阈值参数为小波系数的标准差的一半。