基于直方图优化的图像去雾MATLAB实现,整合全局直方图均衡化、局部直方图处理和Retinex增强算法
一、核心算法实现
matlab
%% 基于直方图优化的图像去雾系统
clear; clc; close all;
% 读取图像
img = imread('foggy_scene.jpg');
if size(img,3)==1
img = cat(3,img,img,img); % 转换为RGB
end
% 参数设置
window_size = 15; % 局部处理窗口尺寸
clip_limit = 0.02; % 直方图裁剪阈值
rayleigh_dist = true;% 是否使用瑞利分布
% 预处理
[I_r, I_g, I_b] = deal(img(:,:,1), img(:,:,2), img(:,:,3));
% 全局直方图处理
[O_r_global, O_g_global, O_b_global] = global_histeq(I_r, I_g, I_b);
% 局部直方图处理
[O_r_local, O_g_local, O_b_local] = local_histeq(I_r, I_g, I_b, window_size, clip_limit, rayleigh_dist);
% Retinex增强处理
[O_r_retinex, O_g_retinex, O_b_retinex] = retinex_enhance(I_r, I_g, I_b);
% 结果整合
enhanced_global = cat(3,O_r_global,O_g_global,O_b_global);
enhanced_local = cat(3,O_r_local,O_g_local,O_b_local);
enhanced_retinex = cat(3,O_r_retinex,O_g_retinex,O_b_retinex);
% 显示对比
figure;
subplot(2,2,1); imshow(img); title('原始图像');
subplot(2,2,2); imshow(enhanced_global); title('全局直方图');
subplot(2,2,3); imshow(enhanced_local); title('局部直方图');
subplot(2,2,4); imshow(enhanced_retinex); title('Retinex增强');
% 直方图对比
figure;
subplot(2,2,1); imhist(img(:,:,1)); title('原图R通道直方图');
subplot(2,2,2); imhist(enhanced_global(:,:,1)); title('全局处理R通道');
subplot(2,2,3); imhist(enhanced_local(:,:,1)); title('局部处理R通道');
subplot(2,2,4); imhist(enhanced_retinex(:,:,1)); title('Retinex处理R通道');
二、关键算法模块
1. 全局直方图均衡化
matlab
function [R_out, G_out, B_out] = global_histeq(R, G, B)
R_eq = histeq(R);
G_eq = histeq(G);
B_eq = histeq(B);
R_out = R_eq; G_out = G_eq; B_out = B_eq;
end
2. 局部直方图处理
matlab
function [R_out, G_out, B_out] = local_histeq(R, G, B, window_size, clip_limit, rayleigh_dist)
% 自适应局部对比度增强
R_eq = adapthisteq(R, 'ClipLimit', clip_limit, 'Distribution', 'rayleigh' if rayleigh_dist else 'uniform');
G_eq = adapthisteq(G, 'ClipLimit', clip_limit, 'Distribution', 'rayleigh' if rayleigh_dist else 'uniform');
B_eq = adapthisteq(B, 'ClipLimit', clip_limit, 'Distribution', 'rayleigh' if rayleigh_dist else 'uniform');
R_out = R_eq; G_out = G_eq; B_out = B_eq;
end
3. Retinex增强算法
matlab
function [R_out, G_out, B_out] = retinex_enhance(R, G, B)
% 高斯卷积参数
sigma = [80, 53, 13000](@ref);
window = ;
% 多尺度高斯滤波
for ch = 1:3
log_img = log1p(im2double(eval(sprintf('R'))));
kernel = fspecial('gaussian', window(ch), sigma(ch));
smooth = imfilter(log_img, kernel, 'replicate');
detail = log_img - smooth;
% 透射率估计
transmission = 1 - 0.95*(detail - min(detail(:))/(max(detail(:))-min(detail(:))));
transmission = max(transmission, 0.1);
# 去雾恢复
recovered = (im2double(eval(sprintf('R'))) - 0.5)./ transmission + 0.5;
recovered = max(min(recovered, 1), 0);
eval(sprintf('%s_eq = recovered;', sprintf('R%d',ch)));
end
R_out = im2uint8(R1_eq); G_out = im2uint8(G2_eq); B_out = im2uint8(B3_eq);
end
三、算法性能对比
| 指标 | 全局直方图 | 局部直方图 | Retinex |
|---|---|---|---|
| 对比度提升 | ★★★☆ | ★★★★ | ★★★★★ |
| 色彩保真度 | ★★☆ | ★★★ | ★★★★☆ |
| 计算复杂度 | O(n) | O(n log n) | O(n²) |
| 适用场景 | 低雾浓度 | 中等雾浓度 | 重度雾霾 |
四、参数优化建议
-
窗口尺寸选择: 小窗口(5-15):保留细节,易产生块效应 中窗口(15-31):平衡细节与平滑度 大窗口(31-50):适合大面积雾霾
-
直方图分布控制: Rayleigh分布:增强高频细节 Gamma分布:保留中间灰度层次 均匀分布:最大化对比度
-
动态范围压缩:
matlab% 在Retinex模块中添加 transmission = 1 ./ (1 + 0.5*transmission.^2); % 非线性压缩
五、应用案例
1. 交通监控图像增强
matlab
% 加载低能见度监控图像
img = imread('highway_fog.jpg');
% 增强处理
[enhanced, trans] = enhance_scene(img, 'method', 'retinex', 'sigma', 120);
% 结果可视化
figure;
subplot(1,2,1); imshow(img); title('原始监控画面');
subplot(1,2,2); imshow(enhanced); title('增强后画面');
2. 无人机航拍去雾
matlab
% 读取航拍图像
img = imread('aerial_fog.png');
% 自适应参数处理
window_size = round(0.05*size(img,2)); % 图像宽度5%的窗口
clip_limit = 0.01 + 0.04*std(double(img(:,:,1)));
% 执行增强
enhanced = local_histeq(img(:,:,1), img(:,:,2), img(:,:,3), window_size, clip_limit);
参考代码 基于直方图优化的图像去雾技术matlab www.youwenfan.com/contentcso/96620.html
六、优化方向
- 多尺度融合:结合不同窗口尺寸的处理结果
- 深度学习辅助:使用预训练CNN模型优化透射率估计
- 硬件加速:利用MATLAB Parallel Computing Toolbox实现GPU加速