暗通道先验去雾Single Image Haze Removal Using Dark Channel Prior

话不多说,直接上代码。MATLAB代码

暗通道加导向滤波

close all;clear all;
addpath(genpath('66image'));
    I=imread('104.png');
    IO=double(I);
    R_channel = IO(:, :, 1);
    G_channel = IO(:, :, 2);
    B_channel = IO(:, :, 3);
    [m, n] = size(R_channel);
    dark_channel_image = zeros(m,n);
    for i=1:m
        for j=1:n
        local_pixels =[R_channel(i,j), G_channel(i,j), B_channel(i,j)]; 
        dark_channel_image(i,j) = min(local_pixels );
        end
    end
%如果得出的图形一块一块的,将最小值滤波半径10变大,后同。
    Dark1 = ordfilt2(dark_channel_image,1,ones(10,10));
    Dark = uint8(Dark1);
    figure, imshow(Dark);
    title("Dark Channel Prior");
    sort_t_bar = sort(Dark); 
    [x_axis,y_axis] = find(Dark>=sort_t_bar(round(0.001*m*n)),round(0.001*m*n));
%设置大气光的上限,可以自行更改,如果图形曝光,改它
    A = min(max(max(max(IO(x_axis,y_axis)))),220);
    dark_channel_image1 = zeros(m,n);
    IA = IO./A;
    R_channel1 = IA(:, :, 1);
    G_channel1 = IA(:, :, 2);
    B_channel1 = IA(:, :, 3);
    for i=1:m
        for j=1:n
            dark_channel_image1(i,j) = min([R_channel1(i,j), G_channel1(i,j), B_channel1(i,j)]); 
        end
    end
%如果得出的图形一块一块的,将10变大,
    Dark1 = ordfilt2(dark_channel_image1,1,ones(10,10));
    Amodel = ones(m,n);
    t = Amodel -0.95*Dark1;
    gray_IO = rgb2gray(I);
    gray_IO =  double(gray_IO)./255;
%40调为最小值滤波半径的4倍,或者更大。
    t = guidedfilter(gray_IO,t,40,0.001);
    figure, imshow(t);
    mabug = 0.1;
    for i=1:m
        for j=1:n
            t(i,j) = max(mabug, t(i,j));
        end
    end
    JA = (IO - A)./ t + A;
    J = uint8(JA);
    figure;  imshow(J); 

函数:导向滤波

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

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