-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathwls_filter.py
200 lines (152 loc) · 5.76 KB
/
wls_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
import numpy as np
from scipy import sparse
from scipy.sparse.linalg import spsolve
import scipy
import cv2
# img format: lab for example al bl
# guide img format: lab
# height x width x channel,
def wls_filter_test(img, guide, alpha=1.2, Lambda=0.032):
epsilon = 0.0001
height = guide.shape[0]
width = guide.shape[1]
n = height * width
grayImgF_ = 1.0*cv2.cvtColor(guide, cv2.COLOR_RGB2GRAY)/255.0
grayImgF = grayImgF_
gradWeightX = np.zeros_like(grayImgF, dtype='float')
gradWeightY = np.zeros_like(grayImgF, dtype='float')
for y in range(height-1):
for x in range(width-1):
if x + 1 < width:
gx = grayImgF[y, x + 1] - grayImgF[y, x]
gradWeightX[y, x] = Lambda / (np.power(np.abs(gx), alpha) + epsilon)
if y + 1 < height:
gy = grayImgF[y + 1, x] - grayImgF[y, x]
gradWeightY[y, x] = Lambda / (np.power(np.abs(gy), alpha) + epsilon)
cv2.imwrite('grad_x.png', (gradWeightX*255.0).astype('uint8'))
A = sparse.lil_matrix((n, n))
bs0 = np.zeros(n, dtype='float')
bs1 = np.zeros(n, dtype='float')
bs2 = np.zeros(n, dtype='float')
xs0 = np.zeros(n, dtype='float')
xs1 = np.zeros(n, dtype='float')
xs2 = np.zeros(n, dtype='float')
for y in range(height):
for x in range(width):
a = np.zeros(5, dtype='float')
ii = y * width + x
if y - 1 >= 0:
gyw = gradWeightY[y-1, x]
a[2] += 1.0 * gyw
a[0] -= 1.0 * gyw
A[ii, ii- width] = a[0]
if x - 1 >= 0:
gxw = gradWeightX[y, x-1]
a[2] += 1.0 * gxw
a[1] -= 1.0 * gxw
A[ii, ii - 1] = a[1]
if x + 1 < width:
gxw = gradWeightX[y, x]
a[2] += 1.0 * gxw
a[3] -= 1.0 * gxw
A[ii, ii + 1] = a[3]
if y + 1 < height:
gyw = gradWeightY[y, x]
a[2] += 1.0 * gyw
a[4] -= 1.0 * gyw
A[ii, ii + width] = a[4]
a[2] += 1.0
A[ii, ii] = a[2]
r, g, b = img[y, x, :]
xs0[ii] = 0.0
xs1[ii] = 0.0
xs2[ii] = 0.0
bs0[ii] = float(r)/255
bs1[ii] = float(g)/255
bs2[ii] = float(b)/255
xs0 = np.clip(spsolve(A, bs0), 0, 255)
xs1 = np.clip(spsolve(A, bs1), 0, 255)
xs2 = np.clip(spsolve(A, bs2), 0, 255)
c0 = np.reshape(xs0, (height, width))
c1 = np.reshape(xs1, (height, width))
c2 = np.reshape(xs2, (height, width))
print(np.max(cv2.merge((c0, c1, c2))))
result = (cv2.merge((c0, c1, c2))*255).astype('uint8')
return result
#cv2.imwrite('result.png', result)
#return cv2.merge((c0, c1, c2))/255.0
def each_channel(img_channel, guide_channel, alpha=1.2, Lambda=1.0):
epsilon = 0.0001
grayImgF = guide_channel
height = grayImgF.shape[0]
width = grayImgF.shape[1]
n = height * width
gradWeightX = np.zeros_like(grayImgF, dtype='float')
gradWeightY = np.zeros_like(grayImgF, dtype='float')
for y in range(height - 1):
for x in range(width - 1):
if x + 1 < width:
gx = grayImgF[y, x + 1] - grayImgF[y, x]
gradWeightX[y, x] = Lambda / (np.power(np.abs(gx), alpha) + epsilon)
if y + 1 < height:
gy = grayImgF[y + 1, x] - grayImgF[y, x]
gradWeightY[y, x] = Lambda / (np.power(np.abs(gy), alpha) + epsilon)
cv2.imwrite('grad_x.png', (gradWeightX * 255.0).astype('uint8'))
A = sparse.lil_matrix((n, n))
bs0 = np.zeros(n, dtype='float')
bs1 = np.zeros(n, dtype='float')
bs2 = np.zeros(n, dtype='float')
xs0 = np.zeros(n, dtype='float')
xs1 = np.zeros(n, dtype='float')
xs2 = np.zeros(n, dtype='float')
for y in range(height):
for x in range(width):
a = np.zeros(5, dtype='float')
ii = y * width + x
if y - 1 >= 0:
gyw = gradWeightY[y - 1, x]
a[2] += 1.0 * gyw
a[0] -= 1.0 * gyw
A[ii, ii - width] = a[0]
if x - 1 >= 0:
gxw = gradWeightX[y, x - 1]
a[2] += 1.0 * gxw
a[1] -= 1.0 * gxw
A[ii, ii - 1] = a[1]
if x + 1 < width:
gxw = gradWeightX[y, x]
a[2] += 1.0 * gxw
a[3] -= 1.0 * gxw
A[ii, ii + 1] = a[3]
if y + 1 < height:
gyw = gradWeightY[y, x]
a[2] += 1.0 * gyw
a[4] -= 1.0 * gyw
A[ii, ii + width] = a[4]
a[2] += 1.0
A[ii, ii] = a[2]
r = img_channel[y, x]
xs0[ii] = 0.0
bs0[ii] = float(r)
xs0 = spsolve(A, bs0)
c0 = np.reshape(xs0, (height, width))
return c0
# guide lab 0~1
def wls_filter(img, guide, alpha=1.2, Lambda=0.032):
grayImgF_ = guide#1.0*cv2.cvtColor(guide, cv2.COLOR_RGB2LAB)/255.0
l, a, b = cv2.split(grayImgF_)
c0 = each_channel(img[:, :, 0], l)
c1 = each_channel(img[:, :, 1], l)
c2 = each_channel(img[:, :, 2], l)
print(np.max(cv2.merge((c0, c1, c2))))
#result = (cv2.merge((c0, c1, c2))).astype('uint8')
#cv2.imwrite('result.png', result)
return cv2.merge((c0, c1, c2))
if __name__ == '__main__':
img1 = 1.0 * cv2.imread('final_result7001400.png')/255.0
img2 = 1.0 * cv2.imread('v2_2272.jpeg')/255.0
result = wls_filter(img1, img2, 1.2, 1) + img2 - wls_filter(img2, img2, 1.2, 1)
result = np.clip(result, 0, 1)
diff = img1 - result
cv2.imwrite('result2.png', (result*255.0).astype('uint8'))
cv2.imwrite('diff.png', (diff).astype('uint8'))