baidut
6/3/2017 - 1:19 AM

## Dehaze 暗通道方法 dehaze2 来源-李霖师兄, 代码价值不大了

Dehaze 暗通道方法

dehaze2 来源-李霖师兄, 代码价值不大了

``````function S = dehaze2(I, DCP, w)
%   DEHAZE is a function used to remove haze in the image using He's method
%
%   - Input image: I (should be a color (RGB) image,normalized into [0,1])
%   - regularization parameter: w belongs to (0, 1), the lager w is ,the stronger is haze removed

I = im2double(I);

isize = size(I);
Ir = I(:,:,1);
Ig = I(:,:,2);
Ib = I(:,:,3);
L = rgb2gray(I);

Ir1 = Ir(:);
Ig1 = Ig(:);
Ib1 = Ib(:);
Il = (Ir1+Ig1+Ib1)./3;
n = length(Il);
N = floor(n*0.002);

Ir_d = ordfilt2(Ir,1,ones(15,15));
Ig_d = ordfilt2(Ig,1,ones(15,15));
Ib_d = ordfilt2(Ib,1,ones(15,15));
% darkc = min(min(Ir_d(:),Ig_d(:)),Ib_d(:));
darkc = DCP;

[~, i] = sort(darkc,1,'descend');
temp = Il(i(1:N));
[~,j] = sort(temp,1,'descend');
Ar = Ir1(i(j(1)));
Ag = Ig1(i(j(1)));
Ab = Ib1(i(j(1)));

A = cat(3,Ar,Ag,Ab);

% Ird = Ir./Ar;
% Igd = Ig./Ag;
% Ibd = Ib./Ab;
% temp = min(Ird,Igd);
% temp = min(temp,Ibd);
% % t = 1 - w*temp;
% w = 1-exp(-2*sqrt(L));

% w = 0.9*sqrt((1-(1-L).^2))+0.01;
% w = 1-exp(-2*sqrt((1-(1-L).^2)));
% w = 1-exp(-2*(1-(1-L).^2));
% w = 0.9*(1-(1-L).^2)+0.01;
if (~exist('w','var') || w<=0)
%     w = (1-10.^(-sqrt(L))).^2;
w = (1-64.^(-sqrt(L))).^2;
end
t = max(1-w.*min(min(Ir_d./Ar,Ig_d./Ag),Ib_d./Ab),1e-7);
% t = max(1-min(min(Ir_d./Ar,Ig_d./Ag),Ib_d./Ab),1e-7);
% t = max(1-w.*min(min(Ir./Ar,Ig./Ag),Ib./Ab),1e-7);
ts = guidedfilter(Ig, t, 95, 0.001);
% if (~exist('w','var') || w<=0)
% %     w = (1-10.^(-sqrt(L))).^2;
%     w = (64.^(-sqrt(L))).^2;
% end
% ts = (1+w).*ts;
% ts = guidedfilter(Ig, t, 1, 0.001);

% tsmin = min(ts(:));
% tsmax = max(ts(:))-tsmin;
% ts = (ts - tsmin)./tsmax;
ts(ts>1) = 1;
ts(ts<0) = 0;
% lc = ts<0.2;
% ts(lc) = 2 * ts(lc);

Sr = (Ir - Ar)./ts +Ar;
Sg = (Ig - Ag)./ts +Ag;
Sb = (Ib - Ab)./ts +Ab;
t = t(16:isize(1)+15,16:isize(2)+15);
ts = ts(16:isize(1)+15,16:isize(2)+15);
% Irx = guidedfilter(Ig, Ir, 1, 0.001);
% Igx = guidedfilter(Ig, Ig, 1, 0.001);
% Ibx = guidedfilter(Ig, Ib, 1, 0.001);
% Sr = (Irx - Ar)./ts +Ar;
% Sg = (Igx - Ag)./ts +Ag;
% Sb = (Ibx - Ab)./ts +Ab;
% Sr = (Ir - Ar)./t +Ar;
% Sg = (Ig - Ag)./t +Ag;
% Sb = (Ib - Ab)./t +Ab;
S = cat(3,Sr,Sg,Sb);
S = S(16:isize(1)+15,16:isize(2)+15,:);

S = im2double(S);

``````
``````function [S,t,ts,A] = dehaze(I,w)
%   DEHAZE is a function used to remove haze in the image using He's method
%
%   - Input image: I (should be a color (RGB) image,normalized into [0,1])
%   - regularization parameter: w belongs to (0, 1), the lager w is ,the stronger is haze removed
isize = size(I);
Ir = I(:,:,1);
Ig = I(:,:,2);
Ib = I(:,:,3);
L = rgb2gray(I);

Ir1 = Ir(:);
Ig1 = Ig(:);
Ib1 = Ib(:);
Il = (Ir1+Ig1+Ib1)./3;
n = length(Il);
N = floor(n*0.002);

Ir_d = ordfilt2(Ir,1,ones(15,15));
Ig_d = ordfilt2(Ig,1,ones(15,15));
Ib_d = ordfilt2(Ib,1,ones(15,15));
darkc = min(min(Ir_d(:),Ig_d(:)),Ib_d(:));
[~, i] = sort(darkc,1,'descend');
temp = Il(i(1:N));
[~,j] = sort(temp,1,'descend');
Ar = Ir1(i(j(1)));
Ag = Ig1(i(j(1)));
Ab = Ib1(i(j(1)));

A = cat(3,Ar,Ag,Ab);

% Ird = Ir./Ar;
% Igd = Ig./Ag;
% Ibd = Ib./Ab;
% temp = min(Ird,Igd);
% temp = min(temp,Ibd);
% % t = 1 - w*temp;
% w = 1-exp(-2*sqrt(L));

% w = 0.9*sqrt((1-(1-L).^2))+0.01;
% w = 1-exp(-2*sqrt((1-(1-L).^2)));
% w = 1-exp(-2*(1-(1-L).^2));
% w = 0.9*(1-(1-L).^2)+0.01;
if (~exist('w','var') || w<=0)
%     w = (1-10.^(-sqrt(L))).^2;
w = (1-64.^(-sqrt(L))).^2;
end
t = max(1-w.*min(min(Ir_d./Ar,Ig_d./Ag),Ib_d./Ab),1e-7);
% t = max(1-min(min(Ir_d./Ar,Ig_d./Ag),Ib_d./Ab),1e-7);
% t = max(1-w.*min(min(Ir./Ar,Ig./Ag),Ib./Ab),1e-7);
ts = guidedfilter(Ig, t, 95, 0.001);
% if (~exist('w','var') || w<=0)
% %     w = (1-10.^(-sqrt(L))).^2;
%     w = (64.^(-sqrt(L))).^2;
% end
% ts = (1+w).*ts;
% ts = guidedfilter(Ig, t, 1, 0.001);

% tsmin = min(ts(:));
% tsmax = max(ts(:))-tsmin;
% ts = (ts - tsmin)./tsmax;
ts(ts>1) = 1;
ts(ts<0) = 0;
% lc = ts<0.2;
% ts(lc) = 2 * ts(lc);

Sr = (Ir - Ar)./ts +Ar;
Sg = (Ig - Ag)./ts +Ag;
Sb = (Ib - Ab)./ts +Ab;
t = t(16:isize(1)+15,16:isize(2)+15);
ts = ts(16:isize(1)+15,16:isize(2)+15);
% Irx = guidedfilter(Ig, Ir, 1, 0.001);
% Igx = guidedfilter(Ig, Ig, 1, 0.001);
% Ibx = guidedfilter(Ig, Ib, 1, 0.001);
% Sr = (Irx - Ar)./ts +Ar;
% Sg = (Igx - Ag)./ts +Ag;
% Sb = (Ibx - Ab)./ts +Ab;
% Sr = (Ir - Ar)./t +Ar;
% Sg = (Ig - Ag)./t +Ag;
% Sb = (Ib - Ab)./t +Ab;
S = cat(3,Sr,Sg,Sb);
S = S(16:isize(1)+15,16:isize(2)+15,:);

``````