opencv 暗通道去雾
时间: 2023-11-20 19:57:43 浏览: 42
OpenCV暗通道去雾的步骤如下:
1. 读取待去雾的图像I(x)和全球大气光成分A。
2. 计算暗通道图像Jdark(x),其中Jdark(x) = min{min{I(x)}|x∈Ω},Ω为图像块的集合,一般取3*3或者15*15的块。
3. 估计全球大气光成分A,其中A = max{J(x)|x∈Ω},Ω为图像块的集合,一般取3*3或者15*15的块。
4. 估计折射率t,其中t = 1 - w * min{Jdark(x)/A|x∈Ω},w为权重系数,一般取0.95。
5. 对每个颜色通道进行去雾处理,其中J(x) = (I(x) - A) / t + A。
6. 输出去雾后的图像J(x)。
下面是OpenCV暗通道去雾的Python代码示例:
```python
import cv2
import numpy as np
def dark_channel(image, block_size):
# 计算暗通道图像
b, g, r = cv2.split(image)
min_channel = cv2.min(cv2.min(r, g), b)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (block_size, block_size))
dark_channel = cv2.erode(min_channel, kernel)
return dark_channel
def estimate_atmosphere(image, dark_channel):
# 估计全球大气光成分
h, w = image.shape[:2]
flat_image = image.reshape(h * w, 3)
flat_dark = dark_channel.reshape(h * w)
indices = np.argsort(flat_dark)[::-1][:h * w // 100]
atmosphere = np.max(flat_image[indices], axis=0)
return atmosphere
def estimate_transmission(dark_channel, atmosphere, w):
# 估计折射率
transmission = 1 - w * (dark_channel / atmosphere)
return transmission
def dehaze(image, block_size=15, w=0.95):
# OpenCV暗通道去雾
dark = dark_channel(image, block_size)
atmosphere = estimate_atmosphere(image, dark)
transmission = estimate_transmission(dark, atmosphere, w)
transmission = cv2.max(transmission, 0.1)
result = np.empty_like(image)
for i in range(3):
result[:, :, i] = (image[:, :, i] - atmosphere[i]) / transmission + atmosphere[i]
result = cv2.normalize(result, None, 0, 255, cv2.NORM_MINMAX)
return result.astype(np.uint8)
```