From 2c425c06f30ac4be6b0b6d35aab6d390b169efa4 Mon Sep 17 00:00:00 2001 From: Jinwei Zhao Date: Thu, 21 Jan 2016 19:53:38 +0800 Subject: add MATLAB code --- MATLAB/boxfilter.m | 27 ++++++++++++++++++++ MATLAB/demo.m | 10 ++++++++ MATLAB/guide.png | Bin 0 -> 389646 bytes MATLAB/guidedfilter.m | 27 ++++++++++++++++++++ MATLAB/guidedfilter_color.m | 59 ++++++++++++++++++++++++++++++++++++++++++++ MATLAB/readme.txt | 26 +++++++++++++++++++ MATLAB/src.bmp | Bin 0 -> 315478 bytes 7 files changed, 149 insertions(+) create mode 100755 MATLAB/boxfilter.m create mode 100755 MATLAB/demo.m create mode 100755 MATLAB/guide.png create mode 100755 MATLAB/guidedfilter.m create mode 100755 MATLAB/guidedfilter_color.m create mode 100755 MATLAB/readme.txt create mode 100755 MATLAB/src.bmp diff --git a/MATLAB/boxfilter.m b/MATLAB/boxfilter.m new file mode 100755 index 0000000..9990fa6 --- /dev/null +++ b/MATLAB/boxfilter.m @@ -0,0 +1,27 @@ +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 + diff --git a/MATLAB/demo.m b/MATLAB/demo.m new file mode 100755 index 0000000..3324f2d --- /dev/null +++ b/MATLAB/demo.m @@ -0,0 +1,10 @@ +close all; + +I = im2double(imread('guide.png')); +p = im2double(imread('src.bmp')); +r = 3; % try r=2, 4, or 8 +% eps = 0.2^2; % try eps=0.1^2, 0.2^2, 0.4^2 +eps = 1e-6; + +q = guidedfilter_color(I, p, r, eps); +imwrite(q, 'matlab.bmp'); diff --git a/MATLAB/guide.png b/MATLAB/guide.png new file mode 100755 index 0000000..cbee301 Binary files /dev/null and b/MATLAB/guide.png differ diff --git a/MATLAB/guidedfilter.m b/MATLAB/guidedfilter.m new file mode 100755 index 0000000..9e9595e --- /dev/null +++ b/MATLAB/guidedfilter.m @@ -0,0 +1,27 @@ +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 \ No newline at end of file diff --git a/MATLAB/guidedfilter_color.m b/MATLAB/guidedfilter_color.m new file mode 100755 index 0000000..226b5df --- /dev/null +++ b/MATLAB/guidedfilter_color.m @@ -0,0 +1,59 @@ +function q = guidedfilter_color(I, p, r, eps) +% GUIDEDFILTER_COLOR O(1) time implementation of guided filter using a color image as the guidance. +% +% - guidance image: I (should be a color (RGB) image) +% - filtering input image: p (should be a gray-scale/single channel image) +% - local window radius: r +% - regularization parameter: eps + +[hei, wid] = size(p); +N = boxfilter(ones(hei, wid), r); % the size of each local patch; N=(2r+1)^2 except for boundary pixels. + +mean_I_r = boxfilter(I(:, :, 1), r) ./ N; +mean_I_g = boxfilter(I(:, :, 2), r) ./ N; +mean_I_b = boxfilter(I(:, :, 3), r) ./ N; + +mean_p = boxfilter(p, r) ./ N; + +mean_Ip_r = boxfilter(I(:, :, 1).*p, r) ./ N; +mean_Ip_g = boxfilter(I(:, :, 2).*p, r) ./ N; +mean_Ip_b = boxfilter(I(:, :, 3).*p, r) ./ N; + +% covariance of (I, p) in each local patch. +cov_Ip_r = mean_Ip_r - mean_I_r .* mean_p; +cov_Ip_g = mean_Ip_g - mean_I_g .* mean_p; +cov_Ip_b = mean_Ip_b - mean_I_b .* mean_p; + +% variance of I in each local patch: the matrix Sigma in Eqn (14). +% Note the variance in each local patch is a 3x3 symmetric matrix: +% rr, rg, rb +% Sigma = rg, gg, gb +% rb, gb, bb +var_I_rr = boxfilter(I(:, :, 1).*I(:, :, 1), r) ./ N - mean_I_r .* mean_I_r; +var_I_rg = boxfilter(I(:, :, 1).*I(:, :, 2), r) ./ N - mean_I_r .* mean_I_g; +var_I_rb = boxfilter(I(:, :, 1).*I(:, :, 3), r) ./ N - mean_I_r .* mean_I_b; +var_I_gg = boxfilter(I(:, :, 2).*I(:, :, 2), r) ./ N - mean_I_g .* mean_I_g; +var_I_gb = boxfilter(I(:, :, 2).*I(:, :, 3), r) ./ N - mean_I_g .* mean_I_b; +var_I_bb = boxfilter(I(:, :, 3).*I(:, :, 3), r) ./ N - mean_I_b .* mean_I_b; + +a = zeros(hei, wid, 3); +for y=1:hei + for x=1:wid + Sigma = [var_I_rr(y, x), var_I_rg(y, x), var_I_rb(y, x); + var_I_rg(y, x), var_I_gg(y, x), var_I_gb(y, x); + var_I_rb(y, x), var_I_gb(y, x), var_I_bb(y, x)]; + %Sigma = Sigma + eps * eye(3); + + cov_Ip = [cov_Ip_r(y, x), cov_Ip_g(y, x), cov_Ip_b(y, x)]; + + a(y, x, :) = cov_Ip * inv(Sigma + eps * eye(3)); % Eqn. (14) in the paper; + end +end + +b = mean_p - a(:, :, 1) .* mean_I_r - a(:, :, 2) .* mean_I_g - a(:, :, 3) .* mean_I_b; % Eqn. (15) in the paper; + +q = (boxfilter(a(:, :, 1), r).* I(:, :, 1)... ++ boxfilter(a(:, :, 2), r).* I(:, :, 2)... ++ boxfilter(a(:, :, 3), r).* I(:, :, 3)... ++ boxfilter(b, r)) ./ N; % Eqn. (16) in the paper; +end \ No newline at end of file diff --git a/MATLAB/readme.txt b/MATLAB/readme.txt new file mode 100755 index 0000000..6844b4d --- /dev/null +++ b/MATLAB/readme.txt @@ -0,0 +1,26 @@ + +*************************************************************************************** +*************************************************************************************** + +Matlab demo code for "Guided Image Filtering" (ECCV 2010) + +by Kaiming He (kahe@microsoft.com) + +If you use/adapt our code in your work (either as a stand-alone tool or as a component +of any algorithm), you need to appropriately cite our ECCV 2010 paper. + +This code is for academic purpose only. Not for commercial/industrial activities. + + +The running time reported in the paper is from C++ implementation. This matlab code is +a reference for those who would like to reimplement our method. + +*************************************************************************************** +*************************************************************************************** + +Usage: + +guidedfilter.m - guided filter implementation (Eqn(5), (6), (8) in the paper) +guidedfilter_color.m - guided filter for color guidance (Eqn(14), (15), (16) in the paper) + +Run the four examples to see the results shown in the paper. diff --git a/MATLAB/src.bmp b/MATLAB/src.bmp new file mode 100755 index 0000000..58d8485 Binary files /dev/null and b/MATLAB/src.bmp differ -- cgit v1.2.3