维纳滤波 python
时间: 2024-12-26 19:13:00 浏览: 19
### 实现维纳滤波
在 Python 中实现维纳滤波通常依赖于 OpenCV 和 NumPy 库。这些库提供了必要的工具来进行图像处理和矩阵运算,这对于执行复杂的滤波操作至关重要[^2]。
#### 导入所需库
首先,确保安装并导入所需的 Python 库:
```python
import cv2
import numpy as np
from matplotlib import pyplot as plt
```
#### 添加噪声到测试图片
为了验证维纳滤波的效果,在实际应用之前可以在一张清晰的图片上人工加入一些常见的噪音类型,比如高斯白噪或椒盐噪声。这里给出一个简单的例子来创建带噪版本的输入图像:
```python
def add_gaussian_noise(image, mean=0, sigma=25):
gauss = np.random.normal(mean, sigma, image.shape).astype('uint8')
noisy_image = cv2.add(image, gauss)
return noisy_image
```
#### 定义维纳滤波函数
接下来定义用于去除上述人为添加的随机干扰项的核心部分——即维纳滤波器本身。该过程涉及到频域变换、计算传递函数H(u,v),以及最终求解最优估计X'(u,v):
```python
def wiener_filter(img, kernel, K):
dummy = np.copy(img)
dummy = fftshift(fftn(dummy))
kernel = psf2otf(kernel, img.shape)
# Wiener filter formula implementation
H = kernel / (abs(kernel)**2 + K)
dummy = abs(ifftn(ifftshift(dummy * H)))
return dummy.clip(0, 255).astype(np.uint8)
# Helper function to convert PSF into OTF
def psf2otf(psf, shape=None):
"""
Convert point-spread function to optical transfer function.
Input:
- psf: N-D array representing the PSF of a linear shift-invariant system.
- shape: Output size desired for the resulting OTF; defaults to same dimensions as `psf`.
If specified must be >= corresponding dimension sizes in `psf`.
Returns an N-dimensional complex-valued ndarray containing the Fourier transform of `psf`,
padded with zeros and shifted appropriately so that its center is at position zero.
Note this routine assumes periodic boundary conditions on both input & output arrays,
which may not always yield physically meaningful results near boundaries when used directly.
"""
if shape is None:
shape = psf.shape
fshape = [(s + k) // 2 for s,k in zip(shape[::-1], psf.shape[::-1])]
pad_width = []
for i in range(len(fshape)):
before_padding = max((fshape[i]-psf.shape[-i-1])//2, 0)
after_padding = max(fshape[i]-before_padding-psf.shape[-i-1], 0)
pad_width.append([before_padding,after_padding])
otf = fftshift(fftn(ifftshift(pad(psf[::-1,...][np.newaxis].transpose(), tuple(range(-len(psf.shape),0))), mode='constant'), axes=tuple([-d-1 for d,_ in enumerate(psf.shape)])),axes=tuple([-d-1 for d,_ in enumerate(psf.shape)]))
return otf[:shape[0], :shape[1]]
```
这段代码实现了完整的维纳滤波流程,并包含了辅助功能`psf2otf()`用来转换点扩展函数至光学传输函数形式以便后续处理[^3]。
#### 显示结果
最后一步是显示原始图像、加噪后的图像及其经过维纳滤波恢复的结果之间的比较:
```python
original_img = cv2.imread('path_to_your_clean_image.jpg', 0) # Load grayscale image
noisy_img = add_gaussian_noise(original_img)
plt.figure(figsize=(12,6))
plt.subplot(131); plt.title("Original Image"); plt.imshow(original_img, cmap="gray")
plt.subplot(132); plt.title("Noisy Image"); plt.imshow(noisy_img, cmap="gray")
restored_img = wiener_filter(noisy_img.astype(float)/255., np.ones((3,3))/9., 0.01)*255.
plt.subplot(133); plt.title("Restored by Wiener Filter"); plt.imshow(restored_img, cmap="gray")
plt.show()
```
此段脚本会生成三个子图窗口,分别呈现原图、含噪版及修复后的情况供观察者直观感受维纳滤波带来的改善效果[^4]。
阅读全文