diff options
Diffstat (limited to 'MATLAB')
-rwxr-xr-x | MATLAB/boxfilter.m | 27 | ||||
-rwxr-xr-x | MATLAB/demo.m | 10 | ||||
-rwxr-xr-x | MATLAB/guide.png | bin | 0 -> 389646 bytes | |||
-rwxr-xr-x | MATLAB/guidedfilter.m | 27 | ||||
-rwxr-xr-x | MATLAB/guidedfilter_color.m | 59 | ||||
-rwxr-xr-x | MATLAB/readme.txt | 26 | ||||
-rwxr-xr-x | MATLAB/src.bmp | bin | 0 -> 315478 bytes |
7 files changed, 149 insertions, 0 deletions
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 @@ | |||
1 | function imDst = boxfilter(imSrc, r) | ||
2 | |||
3 | % BOXFILTER O(1) time box filtering using cumulative sum | ||
4 | % | ||
5 | % - Definition imDst(x, y)=sum(sum(imSrc(x-r:x+r,y-r:y+r))); | ||
6 | % - Running time independent of r; | ||
7 | % - Equivalent to the function: colfilt(imSrc, [2*r+1, 2*r+1], 'sliding', @sum); | ||
8 | % - But much faster. | ||
9 | |||
10 | [hei, wid] = size(imSrc); | ||
11 | imDst = zeros(size(imSrc)); | ||
12 | |||
13 | %cumulative sum over Y axis | ||
14 | imCum = cumsum(imSrc, 1); | ||
15 | %difference over Y axis | ||
16 | imDst(1:r+1, :) = imCum(1+r:2*r+1, :); | ||
17 | imDst(r+2:hei-r, :) = imCum(2*r+2:hei, :) - imCum(1:hei-2*r-1, :); | ||
18 | imDst(hei-r+1:hei, :) = repmat(imCum(hei, :), [r, 1]) - imCum(hei-2*r:hei-r-1, :); | ||
19 | |||
20 | %cumulative sum over X axis | ||
21 | imCum = cumsum(imDst, 2); | ||
22 | %difference over Y axis | ||
23 | imDst(:, 1:r+1) = imCum(:, 1+r:2*r+1); | ||
24 | imDst(:, r+2:wid-r) = imCum(:, 2*r+2:wid) - imCum(:, 1:wid-2*r-1); | ||
25 | imDst(:, wid-r+1:wid) = repmat(imCum(:, wid), [1, r]) - imCum(:, wid-2*r:wid-r-1); | ||
26 | end | ||
27 | |||
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 @@ | |||
1 | close all; | ||
2 | |||
3 | I = im2double(imread('guide.png')); | ||
4 | p = im2double(imread('src.bmp')); | ||
5 | r = 3; % try r=2, 4, or 8 | ||
6 | % eps = 0.2^2; % try eps=0.1^2, 0.2^2, 0.4^2 | ||
7 | eps = 1e-6; | ||
8 | |||
9 | q = guidedfilter_color(I, p, r, eps); | ||
10 | imwrite(q, 'matlab.bmp'); | ||
diff --git a/MATLAB/guide.png b/MATLAB/guide.png new file mode 100755 index 0000000..cbee301 --- /dev/null +++ b/MATLAB/guide.png | |||
Binary files 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 @@ | |||
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 | ||
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 @@ | |||
1 | function q = guidedfilter_color(I, p, r, eps) | ||
2 | % GUIDEDFILTER_COLOR O(1) time implementation of guided filter using a color image as the guidance. | ||
3 | % | ||
4 | % - guidance image: I (should be a color (RGB) 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(p); | ||
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_r = boxfilter(I(:, :, 1), r) ./ N; | ||
13 | mean_I_g = boxfilter(I(:, :, 2), r) ./ N; | ||
14 | mean_I_b = boxfilter(I(:, :, 3), r) ./ N; | ||
15 | |||
16 | mean_p = boxfilter(p, r) ./ N; | ||
17 | |||
18 | mean_Ip_r = boxfilter(I(:, :, 1).*p, r) ./ N; | ||
19 | mean_Ip_g = boxfilter(I(:, :, 2).*p, r) ./ N; | ||
20 | mean_Ip_b = boxfilter(I(:, :, 3).*p, r) ./ N; | ||
21 | |||
22 | % covariance of (I, p) in each local patch. | ||
23 | cov_Ip_r = mean_Ip_r - mean_I_r .* mean_p; | ||
24 | cov_Ip_g = mean_Ip_g - mean_I_g .* mean_p; | ||
25 | cov_Ip_b = mean_Ip_b - mean_I_b .* mean_p; | ||
26 | |||
27 | % variance of I in each local patch: the matrix Sigma in Eqn (14). | ||
28 | % Note the variance in each local patch is a 3x3 symmetric matrix: | ||
29 | % rr, rg, rb | ||
30 | % Sigma = rg, gg, gb | ||
31 | % rb, gb, bb | ||
32 | var_I_rr = boxfilter(I(:, :, 1).*I(:, :, 1), r) ./ N - mean_I_r .* mean_I_r; | ||
33 | var_I_rg = boxfilter(I(:, :, 1).*I(:, :, 2), r) ./ N - mean_I_r .* mean_I_g; | ||
34 | var_I_rb = boxfilter(I(:, :, 1).*I(:, :, 3), r) ./ N - mean_I_r .* mean_I_b; | ||
35 | var_I_gg = boxfilter(I(:, :, 2).*I(:, :, 2), r) ./ N - mean_I_g .* mean_I_g; | ||
36 | var_I_gb = boxfilter(I(:, :, 2).*I(:, :, 3), r) ./ N - mean_I_g .* mean_I_b; | ||
37 | var_I_bb = boxfilter(I(:, :, 3).*I(:, :, 3), r) ./ N - mean_I_b .* mean_I_b; | ||
38 | |||
39 | a = zeros(hei, wid, 3); | ||
40 | for y=1:hei | ||
41 | for x=1:wid | ||
42 | Sigma = [var_I_rr(y, x), var_I_rg(y, x), var_I_rb(y, x); | ||
43 | var_I_rg(y, x), var_I_gg(y, x), var_I_gb(y, x); | ||
44 | var_I_rb(y, x), var_I_gb(y, x), var_I_bb(y, x)]; | ||
45 | %Sigma = Sigma + eps * eye(3); | ||
46 | |||
47 | cov_Ip = [cov_Ip_r(y, x), cov_Ip_g(y, x), cov_Ip_b(y, x)]; | ||
48 | |||
49 | a(y, x, :) = cov_Ip * inv(Sigma + eps * eye(3)); % Eqn. (14) in the paper; | ||
50 | end | ||
51 | end | ||
52 | |||
53 | b = mean_p - a(:, :, 1) .* mean_I_r - a(:, :, 2) .* mean_I_g - a(:, :, 3) .* mean_I_b; % Eqn. (15) in the paper; | ||
54 | |||
55 | q = (boxfilter(a(:, :, 1), r).* I(:, :, 1)... | ||
56 | + boxfilter(a(:, :, 2), r).* I(:, :, 2)... | ||
57 | + boxfilter(a(:, :, 3), r).* I(:, :, 3)... | ||
58 | + boxfilter(b, r)) ./ N; % Eqn. (16) in the paper; | ||
59 | 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 @@ | |||
1 | |||
2 | *************************************************************************************** | ||
3 | *************************************************************************************** | ||
4 | |||
5 | Matlab demo code for "Guided Image Filtering" (ECCV 2010) | ||
6 | |||
7 | by Kaiming He ([email protected]) | ||
8 | |||
9 | If you use/adapt our code in your work (either as a stand-alone tool or as a component | ||
10 | of any algorithm), you need to appropriately cite our ECCV 2010 paper. | ||
11 | |||
12 | This code is for academic purpose only. Not for commercial/industrial activities. | ||
13 | |||
14 | |||
15 | The running time reported in the paper is from C++ implementation. This matlab code is | ||
16 | a reference for those who would like to reimplement our method. | ||
17 | |||
18 | *************************************************************************************** | ||
19 | *************************************************************************************** | ||
20 | |||
21 | Usage: | ||
22 | |||
23 | guidedfilter.m - guided filter implementation (Eqn(5), (6), (8) in the paper) | ||
24 | guidedfilter_color.m - guided filter for color guidance (Eqn(14), (15), (16) in the paper) | ||
25 | |||
26 | 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 --- /dev/null +++ b/MATLAB/src.bmp | |||
Binary files differ | |||