I = double(imread('C:\Users\Administrator\Desktop\88.png'))/255;
[h,w,c] = size(I);
%patch size = 15*15, so win_size = 7
win_size = 7;
omg = 0.8;
t0 = 0.1;
img_size = w*h;
dehaze = zeros(h,w,c);
dark_channel = zeros(h,w);
J = zeros(h,w,3);
%求暗通道
r=I(:,:,1);g=I(:,:,2);b=I(:,:,3);
r=reshape(r,1,img_size);g=reshape(g,1,img_size);b=reshape(b,1,img_size);
rgb = [r;g;b];
Ic_min = reshape(min(rgb),h,w);
%求Ic(p,q),定义为Ic_pq;以及暗原色点,定义为Ic_mn
Ic_pq = max(min(rgb));
Ic_mn = min(min(rgb));
for m = 1:w
for n = 1:h
dark_channel(n,m) = min(min(Ic_min(max(1,n-win_size):min(h,n+win_size),max(1,m-win_size):min(w,m+win_size))));
end
end
%大气光A
dark_col = reshape(dark_channel,img_size,1);
I_col = reshape(I,img_size,3);
[dark_sort,Index]=sort(dark_col,'descend');
num_A = floor(img_size/1000);
Index_A = Index(1:num_A);
A = max(max(I_col(Index_A,:)));
%估计透射率
t = ((Ic_pq-Ic_mn)*(A.*ones(h,w) - Ic_min))./(A*(Ic_pq-Ic_mn).*ones(h,w)-(Ic_min-Ic_mn).*Ic_min);
figure;
set(gcf,'outerposition',get(0,'screensize'));
subplot(121);
imshow(dark_channel);title('Dark Channel','Fontsize',16);
subplot(122);
imshow(t);title('透射率','Fontsize',16);
for mm = 1:3
for nn = 1:h
for kk = 1:w
J(nn,kk,mm) = (I(nn,kk,mm)-A)/t(nn,kk)+A;
end
end
end
figure;
set(gcf,'outerposition',get(0,'screensize'));
subplot(121);
imshow(I);title('去雾前','Fontsize',16);
subplot(122);
imshow(J);title('去雾后','Fontsize',16);