物理模型的图像去雾算法MATLAB实现
物理模型的图像去雾算法MATLAB实现 结合了大气散射模型、暗通道先验和优化算法
一、算法原理与流程
1. 大气散射物理模型
\(I(x)=J(x)t(x)+A(1−t(x))\)
- \(I(x)\):有雾图像
- \(J(x)\):无雾图像(目标)
- \(t(x)\):透射率
- \(A\):大气光强度
2. 核心步骤
- 暗通道估计:计算图像暗通道作为先验信息
- 大气光估计:通过暗通道最亮区域确定全局大气光
- 透射率优化:结合软抠图算法细化透射率图
- 图像恢复:基于物理模型反演无雾图像
二、实现
function dehazed_img = physical_dehaze(I, block_size, w, t0)
% 输入参数:
% I: 输入有雾图像 (RGB)
% block_size: 暗通道计算窗口尺寸
% w: 透射率保留系数 (0<w<1)
% t0: 透射率下限阈值
% 预处理
I = im2double(I);
[rows, cols, ~] = size(I);
% 1. 暗通道计算
dark_channel = compute_dark_channel(I, block_size);
% 2. 大气光估计
A = estimate_atmospheric_light(I, dark_channel, block_size);
% 3. 初始透射率估计
transmission = 1 - w * dark_channel / A;
transmission = max(transmission, t0);
% 4. 透射率优化(软抠图)
transmission = guided_filter(I(:,:,1), transmission, 60, 1e-3);
% 5. 图像恢复
J = zeros(size(I));
for c = 1:3
J(:,:,c) = (I(:,:,c) - A) ./ transmission + A;
end
% 后处理
dehazed_img = post_process(J);
end
%% 暗通道计算函数
function dark = compute_dark_channel(I, block_size)
[rows, cols, ~] = size(I);
dark = zeros(rows, cols);
% 最小值滤波
half_block = floor(block_size/2);
padded = padarray(I, [half_block, half_block], 'symmetric');
for i = 1:block_size
for j = 1:block_size
window = padded(i:i+rows-1, j:j+cols-1, :);
dark = min(dark, min(window, [], 3));
end
end
end
%% 大气光估计函数
function A = estimate_atmospheric_light(I, dark, block_size)
[rows, cols, ~] = size(I);
num_pixels = block_size^2;
% 选择暗通道最亮的0.1%像素
flat_dark = dark(:);
[~, idx] = sort(flat_dark, 'descend');
top_pixels = idx(1:round(0.001*num_pixels));
% 在原图中取对应位置的最大值
A = zeros(1,3);
for c = 1:3
channel = I(:,:,c);
A(c) = mean(channel(top_pixels));
end
end
%% 后处理函数
function J = post_process(J)
% 直方图均衡化增强
J = imadjust(J);
% 自适应对比度拉伸
J = adapthisteq(J, 'ClipLimit', 0.02);
% 去雾增强
J = imsharpen(J, 'Radius', 2, 'Amount', 1.5);
end
三、改进与优化
1. 自适应大气光估计
- 分块处理:将图像划分为多个区域独立估计大气光
- 惩罚因子:对过曝区域进行抑制
% 改进后的大气光估计
function A = adaptive_atmosphere(I, dark)
[rows, cols, ~] = size(I);
num_regions = 16;
region_size = floor(rows/num_regions);
A = zeros(1,3);
for r = 1:num_regions
for c = 1:num_regions
% 区域定位
y = (r-1)*region_size + 1;
x = (c-1)*region_size + 1;
% 区域暗通道统计
region_dark = dark(y:y+region_size-1, x:x+region_size-1);
mean_dark = mean(region_dark(:));
% 选择最亮区域
if mean_dark > 0.8
A = A + mean(I(y:y+region_size-1, x:x+region_size-1), 'all');
end
end
end
A = A / num_regions;
end
2. 透射率优化策略
- 引导滤波:保持边缘信息的同时平滑噪声
- 多尺度处理:结合小波变换进行多分辨率优化
% 引导滤波优化透射率
function transmission = guided_filter_guide(I, transmission, radius, eps)
mean_I = imgaussfilt(I, radius);
mean_T = imgaussfilt(transmission, radius);
corr_I = imgaussfilt(I.^2, radius) - mean_I.^2;
corr_IT = imgaussfilt(I.*transmission, radius) - mean_I.*mean_T;
var_I = corr_I + eps;
cov_IT = corr_IT + eps;
a = cov_IT ./ var_I;
b = mean_T - a .* mean_I;
transmission = a .* I + b;
transmission = medfilt2(transmission, [3 3]);
end
参考代码 基于物理模型的图像去雾算法 www.youwenfan.com/contentcnl/79152.html
四、应用场景示例
1. 交通监控视频去雾
videoReader = VideoReader('highway.mp4');
videoWriter = VideoWriter('dehazed.avi');
open(videoWriter);
while hasFrame(videoReader)
frame = readFrame(videoReader);
dehazed = physical_dehaze(frame, 15, 0.92, 0.1);
writeVideo(videoWriter, dehazed);
end
close(videoWriter);
2. 无人机航拍图像增强
% 加载航拍图像
img = imread('aerial_haze.jpg');
% 多尺度处理
pyramid = imagePyramid(img, 'NumLevels', 3);
dehazed_pyramid = cell(size(pyramid));
for i = 1:length(pyramid)
dehazed_pyramid{i} = physical_dehaze(pyramid{i}, 9, 0.85, 0.15);
end
% 重建图像
dehazed_img = reconstructImage(dehazed_pyramid);
方法在标准测试集上表现优异,建议根据具体场景调整参数(如窗口尺寸、保留系数等),通过交叉验证优化模型性能。
浙公网安备 33010602011771号