写一个去雾化代码
时间: 2024-02-12 20:04:16 浏览: 19
以下是一个简单的去雾化代码,使用了暗通道先验算法:
```
import cv2
import numpy as np
def dehaze(image, w=0.95, t0=0.1, A=220):
I = image.astype('float64') / 255
# 计算暗通道图像
dark_channel = np.min(I, axis=2)
kernel_size = int(np.max(image.shape) / 50)
dark_channel = cv2.erode(dark_channel, np.ones((kernel_size, kernel_size)))
# 估计大气光
top_num = int(len(dark_channel) * len(dark_channel[0]) * w)
indices = np.argpartition(-dark_channel.flatten(), top_num)[:top_num]
top_pixels = np.vstack(np.unravel_index(indices, dark_channel.shape)).T
A = np.max(image[top_pixels[:, 0], top_pixels[:, 1]], axis=0)
# 估计透射率
t = 1 - t0 * dark_channel / A
# 修复透射率
t[t < 0.1] = 0.1
t = cv2.blur(t, (15, 15))
# 去雾
J = (I - A) / np.expand_dims(t, 2) + A
J = np.clip(J, 0, 1) * 255
return J.astype('uint8')
# 加载图像
image = cv2.imread('image.png')
# 去雾化
dehazed_image = dehaze(image)
# 显示图像
cv2.imshow('Original Image', image)
cv2.imshow('Dehazed Image', dehazed_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这个代码使用了暗通道先验算法,通过计算暗通道图像来估计大气光和透射率,然后修复透射率并去雾。你可以根据具体的需求进行修改和优化。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)