This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
Binary file not shown.