aboutsummaryrefslogtreecommitdiff
path: root/MATLAB
diff options
context:
space:
mode:
authorJinwei Zhao <[email protected]>2016-01-21 19:53:38 +0800
committerJinwei Zhao <[email protected]>2016-01-21 19:53:38 +0800
commit2c425c06f30ac4be6b0b6d35aab6d390b169efa4 (patch)
tree8aaa2389c0c733f083024a6dc7311b5a07f26442 /MATLAB
parentba1633c59d98341b378ca3da61e9531b5f253c22 (diff)
downloadGuidedFilter-2c425c06f30ac4be6b0b6d35aab6d390b169efa4.tar.gz
add MATLAB code
Diffstat (limited to 'MATLAB')
-rwxr-xr-xMATLAB/boxfilter.m27
-rwxr-xr-xMATLAB/demo.m10
-rwxr-xr-xMATLAB/guide.pngbin0 -> 389646 bytes
-rwxr-xr-xMATLAB/guidedfilter.m27
-rwxr-xr-xMATLAB/guidedfilter_color.m59
-rwxr-xr-xMATLAB/readme.txt26
-rwxr-xr-xMATLAB/src.bmpbin0 -> 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 @@
1function 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);
11imDst = zeros(size(imSrc));
12
13%cumulative sum over Y axis
14imCum = cumsum(imSrc, 1);
15%difference over Y axis
16imDst(1:r+1, :) = imCum(1+r:2*r+1, :);
17imDst(r+2:hei-r, :) = imCum(2*r+2:hei, :) - imCum(1:hei-2*r-1, :);
18imDst(hei-r+1:hei, :) = repmat(imCum(hei, :), [r, 1]) - imCum(hei-2*r:hei-r-1, :);
19
20%cumulative sum over X axis
21imCum = cumsum(imDst, 2);
22%difference over Y axis
23imDst(:, 1:r+1) = imCum(:, 1+r:2*r+1);
24imDst(:, r+2:wid-r) = imCum(:, 2*r+2:wid) - imCum(:, 1:wid-2*r-1);
25imDst(:, wid-r+1:wid) = repmat(imCum(:, wid), [1, r]) - imCum(:, wid-2*r:wid-r-1);
26end
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 @@
1close all;
2
3I = im2double(imread('guide.png'));
4p = im2double(imread('src.bmp'));
5r = 3; % try r=2, 4, or 8
6% eps = 0.2^2; % try eps=0.1^2, 0.2^2, 0.4^2
7eps = 1e-6;
8
9q = guidedfilter_color(I, p, r, eps);
10imwrite(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 @@
1function 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);
10N = boxfilter(ones(hei, wid), r); % the size of each local patch; N=(2r+1)^2 except for boundary pixels.
11
12mean_I = boxfilter(I, r) ./ N;
13mean_p = boxfilter(p, r) ./ N;
14mean_Ip = boxfilter(I.*p, r) ./ N;
15cov_Ip = mean_Ip - mean_I .* mean_p; % this is the covariance of (I, p) in each local patch.
16
17mean_II = boxfilter(I.*I, r) ./ N;
18var_I = mean_II - mean_I .* mean_I;
19
20a = cov_Ip ./ (var_I + eps); % Eqn. (5) in the paper;
21b = mean_p - a .* mean_I; % Eqn. (6) in the paper;
22
23mean_a = boxfilter(a, r) ./ N;
24mean_b = boxfilter(b, r) ./ N;
25
26q = mean_a .* I + mean_b; % Eqn. (8) in the paper;
27end \ 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 @@
1function 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);
10N = boxfilter(ones(hei, wid), r); % the size of each local patch; N=(2r+1)^2 except for boundary pixels.
11
12mean_I_r = boxfilter(I(:, :, 1), r) ./ N;
13mean_I_g = boxfilter(I(:, :, 2), r) ./ N;
14mean_I_b = boxfilter(I(:, :, 3), r) ./ N;
15
16mean_p = boxfilter(p, r) ./ N;
17
18mean_Ip_r = boxfilter(I(:, :, 1).*p, r) ./ N;
19mean_Ip_g = boxfilter(I(:, :, 2).*p, r) ./ N;
20mean_Ip_b = boxfilter(I(:, :, 3).*p, r) ./ N;
21
22% covariance of (I, p) in each local patch.
23cov_Ip_r = mean_Ip_r - mean_I_r .* mean_p;
24cov_Ip_g = mean_Ip_g - mean_I_g .* mean_p;
25cov_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
32var_I_rr = boxfilter(I(:, :, 1).*I(:, :, 1), r) ./ N - mean_I_r .* mean_I_r;
33var_I_rg = boxfilter(I(:, :, 1).*I(:, :, 2), r) ./ N - mean_I_r .* mean_I_g;
34var_I_rb = boxfilter(I(:, :, 1).*I(:, :, 3), r) ./ N - mean_I_r .* mean_I_b;
35var_I_gg = boxfilter(I(:, :, 2).*I(:, :, 2), r) ./ N - mean_I_g .* mean_I_g;
36var_I_gb = boxfilter(I(:, :, 2).*I(:, :, 3), r) ./ N - mean_I_g .* mean_I_b;
37var_I_bb = boxfilter(I(:, :, 3).*I(:, :, 3), r) ./ N - mean_I_b .* mean_I_b;
38
39a = zeros(hei, wid, 3);
40for 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
51end
52
53b = mean_p - a(:, :, 1) .* mean_I_r - a(:, :, 2) .* mean_I_g - a(:, :, 3) .* mean_I_b; % Eqn. (15) in the paper;
54
55q = (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;
59end \ 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
5Matlab demo code for "Guided Image Filtering" (ECCV 2010)
6
7by Kaiming He ([email protected])
8
9If you use/adapt our code in your work (either as a stand-alone tool or as a component
10of any algorithm), you need to appropriately cite our ECCV 2010 paper.
11
12This code is for academic purpose only. Not for commercial/industrial activities.
13
14
15The running time reported in the paper is from C++ implementation. This matlab code is
16a reference for those who would like to reimplement our method.
17
18***************************************************************************************
19***************************************************************************************
20
21Usage:
22
23guidedfilter.m - guided filter implementation (Eqn(5), (6), (8) in the paper)
24guidedfilter_color.m - guided filter for color guidance (Eqn(14), (15), (16) in the paper)
25
26Run 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
Powered by cgit v1.2.3 (git 2.41.0)