基于直方图优化的图像去雾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加速
相关推荐
CoovallyAIHub19 小时前
仿生学突破:SILD模型如何让无人机在电力线迷宫中发现“隐形威胁”
深度学习·算法·计算机视觉
CoovallyAIHub19 小时前
从春晚机器人到零样本革命:YOLO26-Pose姿态估计实战指南
深度学习·算法·计算机视觉
CoovallyAIHub19 小时前
Le-DETR:省80%预训练数据,这个实时检测Transformer刷新SOTA|Georgia Tech & 北交大
深度学习·算法·计算机视觉
CoovallyAIHub19 小时前
强化学习凭什么比监督学习更聪明?RL的“聪明”并非来自算法,而是因为它学会了“挑食”
深度学习·算法·计算机视觉
CoovallyAIHub20 小时前
YOLO-IOD深度解析:打破实时增量目标检测的三重知识冲突
深度学习·算法·计算机视觉
feifeigo1232 天前
matlab画图工具
开发语言·matlab
Gofarlic_oms13 天前
避免Kisssoft高级分析模块过度采购的科学评估方法
大数据·linux·运维·人工智能·matlab
rit84324993 天前
全变分正则化图像去噪的MATLAB实现
开发语言·matlab
Evand J3 天前
通过matlab实现机器学习的小项目示例(鸢尾花分类)
机器学习·支持向量机·matlab
bu_shuo3 天前
MATLAB命令行窗口中的字体放大操作
matlab·命令行