python去雾算法代码
时间: 2023-10-12 11:15:48 浏览: 96
以下是一个基于Python的去雾算法的示例代码:
```python
import cv2
import numpy as np
def atmospheric_light(img, p=0.1):
assert img.ndim == 3, "image should be RGB"
h, w, _ = img.shape
num_pixels = int(max(h, w) * p)
flattened = img.reshape((-1, 3))
indices = np.argsort(np.linalg.norm(flattened, axis=1))
brightest_pixels = flattened[indices[-num_pixels:]]
brightest_pixels = brightest_pixels[brightest_pixels[:, 0].argsort()[::-1]]
return np.mean(brightest_pixels, axis=0)
def estimate_transmission(img, A, w=15):
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
dark = np.min(gray.reshape(-1, w), axis=1)
dark = dark.reshape(int(img.shape[0] / w), int(img.shape[1] / w))
vmin = np.min(dark)
t = 1 - 0.95 * (gray - vmin) / (A - vmin + 1e-6)
return cv2.medianBlur(t, 5)
def dehaze(img, t, A, tmin=0.1):
img = (img - A) / np.maximum(t, tmin)[:, :, np.newaxis]
return np.clip(img, 0, 1)
def dehaze_image(img_path):
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
A = atmospheric_light(img)
t = estimate_transmission(img, A)
output = dehaze(img, t, A)
return output
```
该算法的实现基于以下三个步骤:
1. 估计图像中的大气光A
2. 估计透射率t
3. 执行去雾操作
最终输出为经过去雾算法处理后的图像。
阅读全文