-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguided_filter.py
207 lines (165 loc) · 6.39 KB
/
guided_filter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# https://github.com/swehrwein/python-guided-filter
import numpy as np
import scipy as sp
import scipy.ndimage
def box(img, r):
""" O(1) box filter
img - >= 2d image
r - radius of box filter
"""
(rows, cols) = img.shape[:2]
imDst = np.zeros_like(img)
tile = [1] * img.ndim
tile[0] = r
imCum = np.cumsum(img, 0)
imDst[0:r + 1, :, ...] = imCum[r:2 * r + 1, :, ...]
imDst[r + 1:rows -
r, :, ...] = imCum[2 * r + 1:rows, :, ...] - imCum[0:rows - 2 * r -
1, :, ...]
imDst[rows - r:rows, :, ...] = np.tile(
imCum[rows - 1:rows, :, ...],
tile) - imCum[rows - 2 * r - 1:rows - r - 1, :, ...]
tile = [1] * img.ndim
tile[1] = r
imCum = np.cumsum(imDst, 1)
imDst[:, 0:r + 1, ...] = imCum[:, r:2 * r + 1, ...]
imDst[:, r + 1:cols -
r, ...] = imCum[:, 2 * r + 1:cols, ...] - imCum[:, 0:cols - 2 * r -
1, ...]
imDst[:, cols - r:cols, ...] = np.tile(
imCum[:, cols - 1:cols, ...],
tile) - imCum[:, cols - 2 * r - 1:cols - r - 1, ...]
return imDst
def _gf_color(I, p, r, eps, s=None):
""" Color guided filter
I - guide image (rgb)
p - filtering input (single channel)
r - window radius
eps - regularization (roughly, variance of non-edge noise)
s - subsampling factor for fast guided filter
"""
fullI = I
fullP = p
if s is not None:
I = sp.ndimage.zoom(fullI, [1 / s, 1 / s, 1], order=1)
p = sp.ndimage.zoom(fullP, [1 / s, 1 / s], order=1)
r = round(r / s)
h, w = p.shape[:2]
N = box(np.ones((h, w)), r)
mI_r = box(I[:, :, 0], r) / N
mI_g = box(I[:, :, 1], r) / N
mI_b = box(I[:, :, 2], r) / N
mP = box(p, r) / N
# mean of I * p
mIp_r = box(I[:, :, 0] * p, r) / N
mIp_g = box(I[:, :, 1] * p, r) / N
mIp_b = box(I[:, :, 2] * p, r) / N
# per-patch covariance of (I, p)
covIp_r = mIp_r - mI_r * mP
covIp_g = mIp_g - mI_g * mP
covIp_b = mIp_b - mI_b * mP
# symmetric covariance matrix of I in each patch:
# rr rg rb
# rg gg gb
# rb gb bb
var_I_rr = box(I[:, :, 0] * I[:, :, 0], r) / N - mI_r * mI_r
var_I_rg = box(I[:, :, 0] * I[:, :, 1], r) / N - mI_r * mI_g
var_I_rb = box(I[:, :, 0] * I[:, :, 2], r) / N - mI_r * mI_b
var_I_gg = box(I[:, :, 1] * I[:, :, 1], r) / N - mI_g * mI_g
var_I_gb = box(I[:, :, 1] * I[:, :, 2], r) / N - mI_g * mI_b
var_I_bb = box(I[:, :, 2] * I[:, :, 2], r) / N - mI_b * mI_b
a = np.zeros((h, w, 3))
for i in range(h):
for j in range(w):
sig = np.array([[var_I_rr[i, j], var_I_rg[i, j], var_I_rb[i, j]],
[var_I_rg[i, j], var_I_gg[i, j], var_I_gb[i, j]],
[var_I_rb[i, j], var_I_gb[i, j], var_I_bb[i, j]]])
covIp = np.array([covIp_r[i, j], covIp_g[i, j], covIp_b[i, j]])
a[i, j, :] = np.linalg.solve(sig + eps * np.eye(3), covIp)
b = mP - a[:, :, 0] * mI_r - a[:, :, 1] * mI_g - a[:, :, 2] * mI_b
meanA = box(a, r) / N[..., np.newaxis]
meanB = box(b, r) / N
if s is not None:
meanA = sp.ndimage.zoom(meanA, [s, s, 1], order=1)
meanB = sp.ndimage.zoom(meanB, [s, s], order=1)
q = np.sum(meanA * fullI, axis=2) + meanB
return q
def _gf_gray(I, p, r, eps, s=None):
""" grayscale (fast) guided filter
I - guide image (1 channel)
p - filter input (1 channel)
r - window raidus
eps - regularization (roughly, allowable variance of non-edge noise)
s - subsampling factor for fast guided filter
"""
if s is not None:
Isub = sp.ndimage.zoom(I, 1 / s, order=1)
Psub = sp.ndimage.zoom(p, 1 / s, order=1)
r = round(r / s)
else:
Isub = I
Psub = p
(rows, cols) = Isub.shape
N = box(np.ones([rows, cols]), r)
meanI = box(Isub, r) / N
meanP = box(Psub, r) / N
corrI = box(Isub * Isub, r) / N
corrIp = box(Isub * Psub, r) / N
varI = corrI - meanI * meanI
covIp = corrIp - meanI * meanP
a = covIp / (varI + eps)
b = meanP - a * meanI
meanA = box(a, r) / N
meanB = box(b, r) / N
if s is not None:
meanA = sp.ndimage.zoom(meanA, s, order=1)
meanB = sp.ndimage.zoom(meanB, s, order=1)
q = meanA * I + meanB
return q
def _gf_colorgray(I, p, r, eps, s=None):
""" automatically choose color or gray guided filter based on I's shape """
if I.ndim == 2 or I.shape[2] == 1:
return _gf_gray(I, p, r, eps, s)
elif I.ndim == 3 and I.shape[2] == 3:
return _gf_color(I, p, r, eps, s)
else:
print("Invalid guide dimensions:", I.shape)
def guided_filter(I, p, r, eps, s=None):
""" run a guided filter per-channel on filtering input p
I - guide image (1 or 3 channel)
p - filter input (n channel)
r - window raidus
eps - regularization (roughly, allowable variance of non-edge noise)
s - subsampling factor for fast guided filter
"""
if p.ndim == 2:
p3 = p[:, :, np.newaxis]
else:
p3 = p
out = np.zeros_like(p3)
for ch in range(p3.shape[2]):
out[:, :, ch] = _gf_colorgray(I, p3[:, :, ch], r, eps, s)
return np.squeeze(out) if p.ndim == 2 else out
def test_gf():
import imageio
cat = imageio.imread('cat.bmp').astype(np.float32) / 255
tulips = imageio.imread('tulips.bmp').astype(np.float32) / 255
r = 8
eps = 0.05
cat_smoothed = guided_filter(cat, cat, r, eps)
cat_smoothed_s4 = guided_filter(cat, cat, r, eps, s=4)
imageio.imwrite('cat_smoothed.png', cat_smoothed)
imageio.imwrite('cat_smoothed_s4.png', cat_smoothed_s4)
tulips_smoothed4s = np.zeros_like(tulips)
for i in range(3):
tulips_smoothed4s[:, :, i] = guided_filter(tulips,
tulips[:, :, i],
r,
eps,
s=4)
imageio.imwrite('tulips_smoothed4s.png', tulips_smoothed4s)
tulips_smoothed = np.zeros_like(tulips)
for i in range(3):
tulips_smoothed[:, :, i] = guided_filter(tulips, tulips[:, :, i], r,
eps)
imageio.imwrite('tulips_smoothed.png', tulips_smoothed)