python cv2 彩色图像去雾
时间: 2023-10-22 11:09:13 浏览: 147
彩色图像去雾可以通过以下步骤实现:
1. 将彩色图像转换为灰度图像。
2. 对灰度图像进行暗通道先验估计,得到全局大气光值。
3. 估计图像中的透射率。
4. 根据透射率和大气光值,对原始彩色图像进行去雾处理。
下面是一个简单的 Python 代码示例:
```python
import cv2
import numpy as np
def dark_channel(img, size):
# 计算暗通道图像
b, g, r = cv2.split(img)
min_img = cv2.min(cv2.min(r, g), b)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (size, size))
dc_img = cv2.erode(min_img, kernel)
return dc_img
def estimate_atmosphere(img, dc_img):
# 估计全局大气光值
h, w = img.shape[:2]
flat_img = img.reshape(h * w, 3)
flat_dc = dc_img.reshape(h * w)
indices = np.argsort(flat_dc)[::-1][:h * w // 100]
atmosphere = np.max(flat_img[indices], axis=0)
return atmosphere
def estimate_transmission(img, atmosphere, size, omega):
# 估计透射率
img_norm = img.astype(np.float32) / atmosphere.astype(np.float32)
dc_img_norm = dark_channel(img_norm, size)
transmission = 1 - omega * dc_img_norm
return transmission
def dehaze(img, size=15, omega=0.95):
# 去雾处理
dc_img = dark_channel(img, size)
atmosphere = estimate_atmosphere(img, dc_img)
transmission = estimate_transmission(img, atmosphere, size, omega)
transmission = cv2.max(transmission, 0.1)
result = np.empty_like(img)
for i in range(3):
result[:, :, i] = (img[:, :, i].astype(np.float32) - atmosphere[i]) / transmission + atmosphere[i]
result = np.clip(result, 0, 255).astype(np.uint8)
return result
```
阅读全文