写一段利用引导滤波器进行图像去躁的代码
时间: 2024-05-07 15:16:32 浏览: 94
2017(5)_滚动引导滤波器进行红外与可见光图像融合_
5星 · 资源好评率100%
好的,以下是使用引导滤波器进行图像去躁的 Python 代码:
```python
import cv2
import numpy as np
def guided_filter(I, p, r=16, eps=1e-5):
N = cv2.boxFilter(np.ones_like(I), -1, (r, r))
mean_I = cv2.boxFilter(I, -1, (r, r)) / N
mean_p = cv2.boxFilter(p, -1, (r, r)) / N
mean_Ip = cv2.boxFilter(I * p, -1, (r, r)) / N
cov_Ip = mean_Ip - mean_I * mean_p
mean_II = cv2.boxFilter(I * I, -1, (r, r)) / N
var_I = mean_II - mean_I * mean_I
a = cov_Ip / (var_I + eps)
b = mean_p - a * mean_I
mean_a = cv2.boxFilter(a, -1, (r, r)) / N
mean_b = cv2.boxFilter(b, -1, (r, r)) / N
q = mean_a * I + mean_b
return q
if __name__ == '__main__':
img = cv2.imread('lena.jpg')
noise = np.random.normal(0, 50, img.shape)
noisy_img = np.clip(img + noise, 0, 255).astype(np.uint8)
filtered_img = guided_filter(noisy_img.astype(np.float32), noisy_img.astype(np.float32))
cv2.imshow('Original Image', img)
cv2.imshow('Noisy Image', noisy_img)
cv2.imshow('Filtered Image', filtered_img.astype(np.uint8))
cv2.waitKey(0)
```
希望这段代码对您有所帮助!接下来,您有什么问题需要我回答吗?
阅读全文