diff options
Diffstat (limited to 'MATLAB/guidedfilter.m')
-rwxr-xr-x | MATLAB/guidedfilter.m | 27 |
1 files changed, 27 insertions, 0 deletions
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 @@ | |||
1 | function q = guidedfilter(I, p, r, eps) | ||
2 | % GUIDEDFILTER O(1) time implementation of guided filter. | ||
3 | % | ||
4 | % - guidance image: I (should be a gray-scale/single channel image) | ||
5 | % - filtering input image: p (should be a gray-scale/single channel image) | ||
6 | % - local window radius: r | ||
7 | % - regularization parameter: eps | ||
8 | |||
9 | [hei, wid] = size(I); | ||
10 | N = boxfilter(ones(hei, wid), r); % the size of each local patch; N=(2r+1)^2 except for boundary pixels. | ||
11 | |||
12 | mean_I = boxfilter(I, r) ./ N; | ||
13 | mean_p = boxfilter(p, r) ./ N; | ||
14 | mean_Ip = boxfilter(I.*p, r) ./ N; | ||
15 | cov_Ip = mean_Ip - mean_I .* mean_p; % this is the covariance of (I, p) in each local patch. | ||
16 | |||
17 | mean_II = boxfilter(I.*I, r) ./ N; | ||
18 | var_I = mean_II - mean_I .* mean_I; | ||
19 | |||
20 | a = cov_Ip ./ (var_I + eps); % Eqn. (5) in the paper; | ||
21 | b = mean_p - a .* mean_I; % Eqn. (6) in the paper; | ||
22 | |||
23 | mean_a = boxfilter(a, r) ./ N; | ||
24 | mean_b = boxfilter(b, r) ./ N; | ||
25 | |||
26 | q = mean_a .* I + mean_b; % Eqn. (8) in the paper; | ||
27 | end \ No newline at end of file | ||