Skip to content

Commit a912855

Browse files
author
Fujun Luan
committed
Add refinement code for posterization effect
1 parent 9e0d832 commit a912855

21 files changed

+149
-0
lines changed
394 KB
Loading
512 KB
Loading
597 KB
Loading
193 KB
Loading
207 KB
Loading
492 KB
Loading
521 KB
Loading
590 KB
Loading
572 KB
Loading
527 KB
Loading
413 KB
Loading
648 KB
Loading
717 KB
Loading
528 KB
Loading
395 KB
Loading
367 KB
Loading
+27
Original file line numberDiff line numberDiff line change
@@ -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+
+27
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -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
+26
Original file line numberDiff line numberDiff line change
@@ -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.

gen_laplacian/refine_posterization.m

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function K = refine_posterization(I, J)
2+
addpath guided_filter/
3+
r = 8;
4+
eps = 0.1^2;
5+
I_f = I;
6+
for c = 1 : 3
7+
I_f(:,:,c) = guidedfilter_color(I, I_f(:,:,c), r, eps);
8+
end
9+
K = J + I - I_f;
10+
end

0 commit comments

Comments
 (0)