基于直方图优化的图像去雾MATLAB实现

基于直方图优化的图像去雾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²)
适用场景 低雾浓度 中等雾浓度 重度雾霾

四、参数优化建议

  1. 窗口尺寸选择: 小窗口(5-15):保留细节,易产生块效应 中窗口(15-31):平衡细节与平滑度 大窗口(31-50):适合大面积雾霾

  2. 直方图分布控制: Rayleigh分布:增强高频细节 Gamma分布:保留中间灰度层次 均匀分布:最大化对比度

  3. 动态范围压缩

    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

六、优化方向

  1. 多尺度融合:结合不同窗口尺寸的处理结果
  2. 深度学习辅助:使用预训练CNN模型优化透射率估计
  3. 硬件加速:利用MATLAB Parallel Computing Toolbox实现GPU加速
相关推荐
88号技师2 小时前
2026年3月中科院一区SCI-贝塞尔曲线优化算法Bezier curve-based optimization-附Matlab免费代码
开发语言·算法·matlab·优化算法
t198751282 小时前
三维点云最小二乘拟合MATLAB程序
开发语言·算法·matlab
做cv的小昊7 小时前
结合代码读3DGS论文(10)——ICLR 2025 3DGS加速&压缩新工作Sort-Free 3DGS论文及代码解读
论文阅读·人工智能·游戏·计算机视觉·3d·图形渲染·3dgs
机器学习之心7 小时前
PSO-LightGBM-ABKDE粒子群算法优化轻量级梯度提升机自适应带宽核密度估计多变量回归区间预测Matlab实现
算法·matlab·回归·abkde·自适应带宽核密度估计·pso-lightgbm·粒子群算法优化轻量级梯度提升机
棱镜研途10 小时前
EI会议分享 | 2026年图像处理与模式识别国际会议(IC-IPPR 2026)【SPIE出版】
图像处理·人工智能·深度学习·目标检测·计算机·计算机视觉·视觉检测
不懒不懒10 小时前
【实战案例:基于特征匹配的指纹识别系统开发】
人工智能·opencv·计算机视觉
3GPP仿真实验室12 小时前
【MATLAB源码】CSI-RS:信道估计仿真与评估平台
开发语言·matlab
困死,根本不会12 小时前
OpenCV视觉舵机控制系统:从坐标检测到串口控制完整实现
人工智能·opencv·计算机视觉
Fleshy数模12 小时前
基于OpenCV实现指纹识别与验证:原理与实战
人工智能·opencv·计算机视觉
CoovallyAIHub12 小时前
AAAI 2026 | 华中科大联合清华等提出Anomagic:跨模态提示零样本异常生成+万级AnomVerse数据集(附代码)
深度学习·算法·计算机视觉