(论文)基于深度学习的端到端图像去雾系统DehazeNet---matlab

Paper:
《DehazeNet: An End-to-End System for Single Image Haze Removal》

首先看一下效果:
girls原图
(论文)基于深度学习的端到端图像去雾系统DehazeNet---matlab_第1张图片
然后处理结果:

可以看出来效果还是可以的,但是对有的图像颜色失真可能会比较明显。
举个栗子:
gugong.bmp原图
(论文)基于深度学习的端到端图像去雾系统DehazeNet---matlab_第2张图片

对比可以看出颜色失真情况,以及对图像最上方区域,也就是实景的远处,去雾效果不明显。

下面是代码部分,在加载原图时需要注意两点,一是路径要明确,二是一定记得对应修改图片格式

demo.m

clc;
clear;
close all;

haze1=imread('gugong.bmp');
haze1=double(haze1)./255;
dehaze1=run_cnn(haze1);
imshow(dehaze1);
title("results"); 

boxfilter.m

function imDst = boxfilter(imSrc, r)

%   BOXFILTER   O(1) time box filtering using cumulative sum
%
%   - Definition imDst(x, y)=sum(sum(imSrc(x-r:x+r,y-r:y+r)));
%   - Running time independent of r; 
%   - Equivalent to the function: colfilt(imSrc, [2*r+1, 2*r+1], 'sliding', @sum);
%   - But much faster.

[hei, wid] = size(imSrc);
imDst = zeros(size(imSrc));

%cumulative sum over Y axis
imCum = cumsum(imSrc, 1);
%difference over Y axis
imDst(1:r+1, :) = imCum(1+r:2*r+1, :);
imDst(r+2:hei-r, :) = imCum(2*r+2:hei, :) - imCum(1:hei-2*r-1, :);
imDst(hei-r+1:hei, :) = repmat(imCum(hei, :), [r, 1]) - imCum(hei-2*r:hei-r-1, :);

%cumulative sum over X axis
imCum = cumsum(imDst, 2);
%difference over Y axis
imDst(:, 1:r+1) = imCum(:, 1+r:2*r+1);
imDst(:, r+2:wid-r) = imCum(:, 2*r+2:wid) - imCum(:, 1:wid-2*r-1);
imDst(:, wid-r+1:wid) = repmat(imCum(:, wid), [1, r]) - imCum(:, wid-2*r:wid-r-1);
end
在这里插入代码片

convMax.m

function J = convMax( I, r, nomex )
% Extremely fast 2D image convolution with a max filter.
%
% For each location computes J(y,x) = max(max(I(y-r:y+r,x-r:x+r))). The
% filtering is constant time per-window, independent of r. First, the
% filtering is separable, which brings the complexity down to O(r) per
% window from O(r*r). To bring the implemention down to constant time
% (independent of r) we use the van Herk/Gil-Werman algorithm. Ignoring
% boundaries, just 3 max operations are need per-window regardless of r.
%  http://www.leptonica.com/grayscale-morphology.html#FAST-IMPLEMENTATION
%
% The output is exactly equivalent to the following Matlab operations:
%  I=padarray(I,[r r],'replicate','both'); [h,w,d]=size(I); J=I;
%  for z=1:d, for x=r+1:w-r, for y=r+1:h-r
%        J(y,x,z) = max(max(I(y-r:y+r,x-r:x+r,z))); end; end; end
%  J=J(r+1:h-r,r+1:w-r,:);
% The computation, however, is an order of magnitude faster than the above.
%
% USAGE
%  J = convMax( I, r, [nomex] )
%
% INPUTS
%  I      - [hxwxk] input k channel single image
%  r      - integer filter radius or radii along y and x
%  nomex  - [0] if true perform computation in matlab (for testing/timing)
%
% OUTPUTS
%  J      - [hxwxk] max image
%
% EXAMPLE
%  I = single(imResample(imread('cameraman.tif'),[480 640]))/255;
%  r = 5; % set parameter as desired
%  tic, J1=convMax(I,r); toc % mex version (fast)
%  tic, J2=convMax(I,r,1); toc % matlab version (slow)
%  figure(1); im(J1); figure(2); im(abs(J2-J1));
%
% See also conv2, convTri, convBox
%
% Piotr's Computer Vision Matlab Toolbox      Version 3.00
% Copyright 2014 Piotr Dollar & Ron Appel.  [pdollar-at-gmail.com]
% Licensed under the Simplified BSD License [see external/bsd.txt]

assert( all(r>=0) );
if( nargin<3 ), nomex=0; end
if( all(r==0) ), J = I; return; end
if( numel(r)==1 ), ry=r; rx=r; else ry=r(1); rx=r(2); end

if( nomex==0 )
  d=size(I,3);
  if(d==1), J=convConst('convMax',convConst('convMax',I,ry,1)',rx,1)'; else
    J=I; for z=1:d, J(:,:,z) = ...
        convConst('convMax',convConst('convMax',J(:,:,z),ry,1)',rx,1)'; end
  end
else
  I=padarray(I,[ry rx],'replicate','both'); [h,w,d]=size(I); J=I;
  for z=1:d, for x=rx+1:w-rx, for y=ry+1:h-ry
        J(y,x,z) = max(max(I(y-ry:y+ry,x-rx:x+rx,z))); end; end; end
  J=J(ry+1:h-ry,rx+1:w-rx,:);
end

end

convolution.m

function [ output_data ] = convolution(input_data, weights_conv, biases_conv)
%CONVOLUTION Summary of this function goes here
%   Detailed explanation goes here
weights_conv=double(weights_conv);
biases_conv=double(biases_conv);
hei = size(input_data,1);
wid = size(input_data,2);
[conv_channels,conv_patchsize2,conv_filters] = size(weights_conv);
conv_patchsize = sqrt(conv_patchsize2);

output_data = zeros(hei, wid, conv_filters);
for i = 1 : conv_filters
    for j = 1 : conv_channels
        conv_subfilter = reshape(weights_conv(j,:,i), conv_patchsize, conv_patchsize);
        output_data(:,:,i) = output_data(:,:,i) + imfilter(input_data(:,:,j), conv_subfilter, 'same', 'symmetric');
    end
    output_data(:,:,i) = output_data(:,:,i) + biases_conv(i);
end

end

guidedfilter.m

function q = guidedfilter(I, p, r, eps)
%   GUIDEDFILTER   O(1) time implementation of guided filter.
%
%   - guidance image: I (should be a gray-scale/single channel image)
%   - filtering input image: p (should be a gray-scale/single channel image)
%   - local window radius: r
%   - regularization parameter: eps

[hei, wid] = size(I);
N = boxfilter(ones(hei, wid), r); % the size of each local patch; N=(2r+1)^2 except for boundary pixels.

mean_I = boxfilter(I, r) ./ N;
mean_p = boxfilter(p, r) ./ N;
mean_Ip = boxfilter(I.*p, r) ./ N;
cov_Ip = mean_Ip - mean_I .* mean_p; % this is the covariance of (I, p) in each local patch.

mean_II = boxfilter(I.*I, r) ./ N;
var_I = mean_II - mean_I .* mean_I;

a = cov_Ip ./ (var_I + eps); % Eqn. (5) in the paper;
b = mean_p - a .* mean_I; % Eqn. (6) in the paper;

mean_a = boxfilter(a, r) ./ N;
mean_b = boxfilter(b, r) ./ N;

q = mean_a .* I + mean_b; % Eqn. (8) in the paper;
end

最后是CNN模型
run_cnn.m

unction [ dehaze ] = run_cnn( im )
%RUN_CNN Summary of this function goes here
%   Detailed explanation goes here

r0 = 50;
eps = 10^-3; 
gray_I = rgb2gray(im);

load dehaze
haze=im-0.5;

%% Feature Extraction F1
f1=convolution(haze, weights_conv1, biases_conv1);
F1=[];
f1temp=reshape(f1,size(f1,1)*size(f1,2),size(f1,3));
for step=1:4
    maxtemp=max(f1temp(:,(step*4-3):step*4),[],2);
    F1=[F1,maxtemp]; %#ok<AGROW>
end
F1=reshape(F1,size(f1,1),size(f1,2),size(F1,2));

%% Multi-scale Mapping F2
F2=zeros(size(F1,1),size(F1,2),48);
F2(:,:,1:16)=convolution(F1, weights_conv3x3, biases_conv3x3);
F2(:,:,17:32)=convolution(F1, weights_conv5x5, biases_conv5x5);
F2(:,:,33:48)=convolution(F1, weights_conv7x7, biases_conv7x7);

%% Local Extremum F3
F3=convMax(single(F2), 3);

%% Non-linear Regression F4
F4=min(max(convolution(F3, weights_ip, biases_ip),0),1);

%% Atmospheric light
sortdata = sort(F4(:), 'ascend');
idx = round(0.01 * length(sortdata));
val = sortdata(idx); 
id_set = find(F4 <= val);
BrightPxls = gray_I(id_set);
iBright = BrightPxls >= max(BrightPxls);
id = id_set(iBright);
Itemp=reshape(im,size(im,1)*size(im,2),size(im,3));
A = mean(Itemp(id, :),1);
A=reshape(A,1,1,3);

F4 = guidedfilter(gray_I, F4, r0, eps);

J=bsxfun(@minus,im,A);
J=bsxfun(@rdivide,J,F4);
J=bsxfun(@plus,J,A);
dehaze=J;
end

你可能感兴趣的:(matlab,图像处理,matlab,图像处理)