维纳滤波图像复原python
时间: 2023-10-12 18:21:45 浏览: 147
可以使用scipy库中的signal模块实现维纳滤波图像复原,具体实现步骤可以参考以下代码:
```python
import numpy as np
import cv2
from scipy.signal import convolve2d
from scipy.signal import fftconvolve
def wiener_filter(image, kernel, K):
kernel /= np.sum(kernel)
dummy = np.copy(image)
dummy = np.pad(dummy, [(klen // 2, klen // 2), (klen // 2, klen // 2)], mode='constant')
dummy = fftconvolve(dummy, kernel, mode='same')
dummy = dummy[klen // 2:-klen // 2, klen // 2:-klen // 2]
noise = image - dummy
noise = np.abs(np.fft.fft2(noise)) ** 2
noise /= np.prod(image.shape)
noise *= K / (K + noise)
kernel = np.fft.fft2(kernel, s=image.shape)
dummy = kernel * np.fft.fft2(dummy)
dummy += noise * np.fft.fft2(image)
dummy = np.fft.ifft2(dummy)
dummy = np.real(dummy)
return np.clip(dummy, 0, 255)
# 读入图像并转为灰度图
image = cv2.imread('lena.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 添加高斯噪声
sigma = 25
noise = np.random.normal(0, sigma, gray.shape)
noisy = np.clip(gray + noise, 0, 255).astype(np.uint8)
# 维纳滤波复原
klen = 21
kernel = np.ones((klen, klen))
restored = wiener_filter(noisy, kernel, sigma ** 2)
# 显示结果
cv2.imshow('Noisy', noisy)
cv2.imshow('Restored', restored)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
当然,这只是一种实现方式,具体的维纳滤波图像复原方法还有很多,可以根据实际情况选择适合自己的方法。
阅读全文