图像预处理:去雾算法总结



图像预处理:图像去雾算法总结

去雾算法总结

1、暗通道去雾算法

何凯明暗通道去雾算法
matlab源码实现像素逐个运行,比较耗时,可查找C++实现的代码。
matlab源码和C++源码底部有下载链接

1.1 单张图片去雾处理
% 功能:去雾算法单张图片测试

clc
clear all
I = imread('F:\\去雾算法\\2.jpg');
[ J,tmap,tmap_ref ] = darkChannel( I,15,0.95);     % J 为处理后输出的图像
1.2 图像批量去雾处理
% 功能:读取指定文件夹下的所有图片,改变尺寸后去雾再保存。
 
clc;
clear;
 
% PathName = '.\train\';                   %指定当前程序所在路径下的文件夹
PathName = 'F:\input_img\';                %指定其他路径下的文件夹
Num = dir([PathName,'*.jpg']);             %关联文件夹下某种类型的文件
 
% if ~exist('newData','dir')               %若当前工作路径下不存在,则创建新文件夹newData
% mkdir('newData')
% end
% paths=[pwd,'\newData'];                  %在当前路径下生成新的存储路径
fprintf('处理ing,请稍等...\n');
for i=1:length(Num)
    FileName = Num(i).name;
    trainImg = imread([PathName FileName]);
    newImg = imresize(trainImg,[416 416]);                         %统一尺寸
    % newImg = rgb2gray(newImg);%灰度化
    [ J,tmap,tmap_ref ] = darkChannel( newImg,15,0.95);
    newImg = J;
    %重命名
    newName = num2str(Num(i).name(1:end-4));                  %提取原图像文件名-不含格式
    fileName = strcat('F:\output_img\',newName,'.jpg');       %指定新的存储路径+文件名+格式
    imwrite(newImg,fileName);                   %保存图像
    
%     figure;
%     subplot(121);imshow(trainImg);
%     subplot(122);imshow(newImg);
%     close all;
end
fprintf('处理Over,请稍等...\n');

2、基于SSR算法的图像去雾

% 图像效果:去雾效果较好,颜色亮丽,但分辨率有所损失
% 参考网址:https://blog.csdn.net/baimafujinji/article/details/73824787

I = imread('canon.jpg');

R = I(:, :, 1);
[N1, M1] = size(R);
R0 = double(R);
Rlog = log(R0+1);
Rfft2 = fft2(R0);

sigma = 250;
F = fspecial('gaussian', [N1,M1], sigma);
Efft = fft2(double(F));

DR0 = Rfft2.* Efft;
DR = ifft2(DR0);

DRlog = log(DR +1);
Rr = Rlog - DRlog;
EXPRr = exp(Rr);
MIN = min(min(EXPRr));
MAX = max(max(EXPRr));
EXPRr = (EXPRr - MIN)/(MAX - MIN);
EXPRr = adapthisteq(EXPRr);

G = I(:, :, 2);

G0 = double(G);
Glog = log(G0+1);
Gfft2 = fft2(G0);

DG0 = Gfft2.* Efft;
DG = ifft2(DG0);

DGlog = log(DG +1);
Gg = Glog - DGlog;
EXPGg = exp(Gg);
MIN = min(min(EXPGg));
MAX = max(max(EXPGg));
EXPGg = (EXPGg - MIN)/(MAX - MIN);
EXPGg = adapthisteq(EXPGg);

B = I(:, :, 3);

B0 = double(B);
Blog = log(B0+1);
Bfft2 = fft2(B0);

DB0 = Bfft2.* Efft;
DB = ifft2(DB0);

DBlog = log(DB+1);
Bb = Blog - DBlog;
EXPBb = exp(Bb);
MIN = min(min(EXPBb));
MAX = max(max(EXPBb));
EXPBb = (EXPBb - MIN)/(MAX - MIN);
EXPBb = adapthisteq(EXPBb);

result = cat(3, EXPRr, EXPGg, EXPBb);
subplot(121), imshow(I);
subplot(122), imshow(result);

3、基于单幅图像的快速去雾算法

% 去雾效果:图像偏绿
% 参考链接:https://blog.csdn.net/wanglongfei_hust/article/details/19190307

% 基于单幅图像的快速去雾算法
fileName = 'D:\\image\\haze image\\He\\canon3.bmp';
 
% step1: 输入:有雾图像H(x)
H = imread(fileName);
H = double(H) / 255.0;
imtool(H);
[h, w] = size(H);
sv = ceil(max(h, w) / 50);
if mod(sv, 2) == 0
    sv = sv + 1;
end
 
% step2: 求取H(x)三通道的最小值M(x)
M = min(H, [], 3);
 
% step3: 对M(x)进行均值滤波,得到Mave(x)
 
f = fspecial('average', sv);
Mave = imfilter(M, f, 'symmetric');   
 
% step4: 求取M(x)中所有元素的均值Mav
[h, w] = size(Mave);
n = 0.0;
for i=1:h
    for j=1:w
        n = n + Mave(i, j);  
    end
end
Mav = n / (h * w);
 
% step5: 利用Mave(x)求出L(x)
e = 2.0;    % e是一个用来调节的参数,当e值越大时,去雾后的图像就越暗,去雾效果就越明显,e值越小时,图像偏白,有明显的雾气。
L = min(min(e*Mav, 0.9)*Mave, M);
 
 
% step6: 利用Mave(x)和H(x),求出A
temp = max(H, [], 3);
A = 0.5 * (max(temp(:)) + max(Mave(:))) * [1; 1; 1];
 
% step7: 去雾后图像F(x)
[h, w, c] = size(H);
F = zeros(h, w, c);
for i=1:h
    for j=1:w
        for k=1:c
            F(i,j,k) = (H(i,j,k) - L(i,j)) / (1 - L(i,j)/A(k));
        end
    end
end
 
imtool(F);