opencv实现DeHaze
时间: 2023-12-02 07:43:14 浏览: 32
以下是使用OpenCV实现DeHaze的步骤:
1.读取图像并转换为浮点数格式:
```python
import cv2
import numpy as np
img = cv2.imread('image.jpg')
img = img.astype('float64') / 255.0
```
2.计算暗通道:
```python
patch_size = 15
b, g, r = cv2.split(img)
min_channel = cv2.min(cv2.min(r, g), b)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (patch_size, patch_size))
dark_channel = cv2.erode(min_channel, kernel)
```
3.计算大气光:
```python
top_percent = 0.001
height, width = img.shape[:2]
num_pixels = height * width
num_top_pixels = int(num_pixels * top_percent)
dark_flat = dark_channel.flatten()
indices = dark_flat.argsort()[-num_top_pixels:]
atmosphere = np.zeros(3)
for i in range(num_top_pixels):
atmosphere += img.item(indices[i] / width, indices[i] % width, :).astype('float64')
atmosphere /= num_top_pixels
```
4.计算透射率:
```python
omega = 0.95
transmission = 1 - omega * dark_channel / atmosphere.max()
```
5.恢复原始图像:
```python
tmin = 0.1
transmission = cv2.max(transmission, tmin)
img_recovered = np.zeros_like(img)
for i in range(3):
img_recovered[:, :, i] = (img[:, :, i] - atmosphere[i]) / transmission + atmosphere[i]
img_recovered = cv2.min(cv2.max(img_recovered, 0), 1)
img_recovered = (img_recovered * 255).astype('uint8')
```
相关推荐
![](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)