基于暗通道先验的图像去雾算法解析与实现
创作时间:
作者:
@小白创作中心
基于暗通道先验的图像去雾算法解析与实现
引用
CSDN
1.
https://blog.csdn.net/hhhhhhhhhhwwwwwwwwww/article/details/145707812
算法背景
何凯明团队于2009年提出的暗通道先验去雾算法《single image haze removal using dark channel prior》,通过统计发现:在无雾图像的局部区域中,至少存在一个颜色通道的像素值趋近于零。这一发现为图像去雾提供了重要的理论依据,其数学模型可表示为:
I(x) = J(x)t(x) + A(1 - t(x))
其中:
- I(x):观测到的有雾图像
- J(x):待恢复的无雾图像
- t(x):透射率
- A:全局大气光值
算法原理
1. 暗通道计算
通过取RGB三通道最小值并进行形态学腐蚀操作:
def dark_channel(img, size=15):
r, g, b = cv2.split(img)
min_img = cv2.min(r, cv2.min(g, b))
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (size, size))
return cv2.erode(min_img, kernel)
2. 大气光估计
选取图像中最亮像素的0.1%作为大气光值:
def get_atmo(img, percent=0.001):
mean_perpix = np.mean(img, axis=2).reshape(-1)
mean_topper = mean_perpix[:int(img.shape[0] * img.shape[1] * percent)]
return np.mean(mean_topper)
3. 透射率估计
t(x) = 1 - ω ⋅ dark_channel(I/A)
def get_trans(img, atom, w=0.95):
x = img / atom
return 1 - w * dark_channel(x, 7)
4. 引导滤波优化
使用灰度图作为引导图像进行透射率优化:
# 引导滤波
def guided_filter(p, i, r, e):
"""
:param p: input image
:param i: guidance image
:param r: radius
:param e: regularization
:return: filtering output q
"""
# 1
mean_I = cv2.boxFilter(i, cv2.CV_64F, (r, r))
mean_p = cv2.boxFilter(p, cv2.CV_64F, (r, r))
corr_I = cv2.boxFilter(i * i, cv2.CV_64F, (r, r))
corr_Ip = cv2.boxFilter(i * p, cv2.CV_64F, (r, r))
# 2
var_I = corr_I - mean_I * mean_I
cov_Ip = corr_Ip - mean_I * mean_p
# 3
a = cov_Ip / (var_I + e)
b = mean_p - a * mean_I
# 4
mean_a = cv2.boxFilter(a, cv2.CV_64F, (r, r))
mean_b = cv2.boxFilter(b, cv2.CV_64F, (r, r))
# 5
q = mean_a * i + mean_b
return q
完整实现代码
import cv2
import numpy as np
import os
# 计算雾化图像的暗通道
def dark_channel(img, size=15):
r, g, b = cv2.split(img)
min_img = cv2.min(r, cv2.min(g, b)) # 取最暗通道
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (size, size))
dc_img = cv2.erode(min_img, kernel)
return dc_img
# 估计全局大气光值
def get_atmo(img, percent=0.001):
mean_perpix = np.mean(img, axis=2).reshape(-1)
mean_topper = mean_perpix[:int(img.shape[0] * img.shape[1] * percent)]
return np.mean(mean_topper)
# 估算透射率图
def get_trans(img, atom, w=0.95):
x = img / atom
t = 1 - w * dark_channel(x, 15)
return t
# 引导滤波
def guided_filter(p, i, r, e):
"""
:param p: input image
:param i: guidance image
:param r: radius
:param e: regularization
:return: filtering output q
"""
# 1
mean_I = cv2.boxFilter(i, cv2.CV_64F, (r, r))
mean_p = cv2.boxFilter(p, cv2.CV_64F, (r, r))
corr_I = cv2.boxFilter(i * i, cv2.CV_64F, (r, r))
corr_Ip = cv2.boxFilter(i * p, cv2.CV_64F, (r, r))
# 2
var_I = corr_I - mean_I * mean_I
cov_Ip = corr_Ip - mean_I * mean_p
# 3
a = cov_Ip / (var_I + e)
b = mean_p - a * mean_I
# 4
mean_a = cv2.boxFilter(a, cv2.CV_64F, (r, r))
mean_b = cv2.boxFilter(b, cv2.CV_64F, (r, r))
# 5
q = mean_a * i + mean_b
return q
def dehaze(im):
img = im.astype('float64') / 255
img_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY).astype('float64') / 255
atom = get_atmo(img)
trans = get_trans(img, atom)
trans_guided = guided_filter(trans, img_gray, 20, 0.0001)
trans_guided = cv2.max(trans_guided, 0.25)
result = np.empty_like(img)
for i in range(3):
result[:, :, i] = (img[:, :, i] - atom) / trans_guided + atom
return result * 255
if __name__ == '__main__':
image_path= 'images/img.png'
im = cv2.imread(image_path)
img = im.astype('float64') / 255
img_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY).astype('float64') / 255
atom = get_atmo(img)
trans = get_trans(img, atom)
trans_guided = guided_filter(trans, img_gray, 10, 0.0001)
trans_guided = cv2.max(trans_guided, 0.25)
result = np.empty_like(img)
for i in range(3):
result[:, :, i] = (img[:, :, i] - atom) / trans_guided + atom
cv2.imwrite('images/img.png', result * 255)
热门推荐
糖代谢异常怎么办
从耳鸣的临床特征推断耳鸣的病因
穷游必备,青岛免费的12个景点,到青岛游玩必打卡
秋冬祛痰湿,姜撞奶超简单!
薏米控湿大作战:痰湿体质的饮食秘籍
秋游本溪:关门山枫叶与水洞奇观的自驾之旅
本溪水洞自驾游攻略:冬季行车需注意!
本溪自驾游打卡三大网红景点
本溪自驾游攻略:本溪水洞&关门山深度游览指南
瑞典冬季最值得的体验
瑞典是哪个国家,瑞典地理位置和人口介绍!
饭后站一会儿,真的能瘦吗?
郑州二七纪念馆
饭后站立半小时,助你远离胃病!
专家推荐:饭后站立5分钟最养生
饭后站一站,真的更健康吗?协和专家这样建议
上海启动2025年春节后来沪务工劳动者"点对点"返岗活动
血糖检查在体检中的重要性和检查方法是什么?
应对黎明现象:降低空腹血糖水平的策略
美元/日元:日本央行下周会议前日元走强因素显现
冬季养生必备:揭秘神奇中药材菟丝子
山东中医药大学推荐:菟丝子明目良方
老花镜也要验光?配错了可对双眼造成严重伤害!
昆明腾冲直飞航班大揭秘!
昆明至腾冲机票价格波动揭秘!
DeepSeek开源风暴席卷全球AI圈!
探秘云南古城景点:怒江之旅
53岁大姐每天用豆瓣酱炒菜,3年后体检,医生惊叹:这是吃了啥?
详解AR光学核心技术——衍射光波导
脑血流动力学改变与脑小血管病的关系解析