-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinflation.py
232 lines (197 loc) · 7.58 KB
/
inflation.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import cv2
import numpy as np
from scipy.sparse import coo_matrix, linalg
def depth2orthomesh(depth, x_step=1, y_step=1, scale=[1.0, 1.0, 1.0], minus_depth=True):
vertices = []
faces = []
if len(depth.shape) != 2:
return None
h, w = depth.shape
vertex_id = 0
added_table = {}
for y in range(0, h, y_step):
for x in range(0, w, x_step):
added_table[(y, x)] = -1
max_connect_z_diff = 99999.9
# TODO
# pixel-wise loop in pure python is toooooooo slow
for y in range(0, h, y_step):
for x in range(0, w, x_step):
d = depth[y, x]
if d <= 0.000001:
continue
if minus_depth:
d = -d
vertices.append([x * scale[0], y * scale[1], d * scale[2]])
added_table[(y, x)] = vertex_id
current_index = vertex_id
upper_left_index = added_table[((y - y_step), (x - x_step))]
upper_index = added_table[((y - y_step), x)]
left_index = added_table[(y, (x - x_step))]
upper_left_diff = np.abs(depth[y - y_step, x - x_step] - d)
upper_diff = np.abs(depth[y - y_step, x] - d)
left_diff = np.abs(depth[y, x - x_step] - d)
if upper_left_index > 0 and upper_index > 0\
and upper_left_diff < max_connect_z_diff\
and upper_diff < max_connect_z_diff:
faces.append([upper_left_index, current_index, upper_index])
if upper_left_index > 0 and left_index > 0\
and upper_left_diff < max_connect_z_diff\
and left_diff < max_connect_z_diff:
faces.append([upper_left_index, left_index, current_index])
vertex_id += 1
return vertices, faces
def _make_ply_txt(vertices, faces, color=[], normal=[]):
header_lines = ["ply", "format ascii 1.0",
"element vertex " + str(len(vertices)),
"property float x", "property float y", "property float z"]
has_normal = len(vertices) == len(normal)
has_color = len(vertices) == len(color)
if has_normal:
header_lines += ["property float nx",
"property float ny", "property float nz"]
if has_color:
header_lines += ["property uchar red", "property uchar green",
"property uchar blue", "property uchar alpha"]
# no face
header_lines += ["element face " + str(len(faces)),
"property list uchar int vertex_indices", "end_header"]
header = "\n".join(header_lines) + "\n"
data_lines = []
for i in range(len(vertices)):
line = [vertices[i][0], vertices[i][1], vertices[i][2]]
if has_normal:
line += [normal[i][0], normal[i][1], normal[i][2]]
if has_color:
line += [int(color[i][0]), int(color[i][1]), int(color[i][2]), 255]
line_txt = " ".join([str(x) for x in line])
data_lines.append(line_txt)
for f in faces:
line_txt = " ".join(['3'] + [str(int(x)) for x in f])
data_lines.append(line_txt)
data_txt = "\n".join(data_lines)
ply_txt = header + data_txt
return ply_txt
def writeMeshAsPly(path, vertices, faces):
with open(path, 'w') as f:
txt = _make_ply_txt(vertices, faces)
f.write(txt)
def inflationByDistanceTransform(mask, activation_func=None):
dist = cv2.distanceTransform(mask, cv2.DIST_L2, cv2.DIST_MASK_PRECISE)
depth = dist
if activation_func is None:
return depth
activated_depth = depth.copy()
h, w = depth.shape
for j in range(h):
for i in range(w):
#print(d, activation_func(d))
activated_depth[j, i] = activation_func(depth[j, i])
return activated_depth
def activation_tanh(factor):
return lambda x: np.tanh(x * factor) / factor
# Implementation of the following paper
# "Notes on Inflating Curves" [Baran and Lehtinen 2009].
# http://alecjacobson.com/weblog/media/notes-on-inflating-curves-2009-baran.pdf
def inflationByBaran(mask, use_sparse=True):
h, w = mask.shape
depth = np.zeros((h, w))
img2param_idx = {}
param_idx = 0
def get_idx(x, y):
return y * w + x
for y in range(h):
for x in range(w):
c = mask[y, x]
if c != 0:
img2param_idx[get_idx(x, y)] = param_idx
param_idx += 1
num_param = len(img2param_idx.keys())
triplets = []
cur_row = 0
# 4 neighbor laplacian
for y in range(1, h-1):
for x in range(1, w-1):
c = mask[y, x]
if c == 0:
continue
triplets.append([cur_row, img2param_idx[get_idx(x, y)], -4.0])
kernels = [(y, x - 1), (y, x + 1), (y - 1, x), (y + 1, x)]
for kernel in kernels:
jj, ii = kernel
if mask[jj, ii] != 0:
triplets.append([cur_row, img2param_idx[get_idx(ii, jj)], 1.0])
cur_row += 1 # Go to the next equation
# Prepare right hand side
b = np.zeros((num_param, 1))
rhs = -4.0
cur_row = 0
for y in range(1, h-1):
for x in range(1, w-1):
c = mask[y, x]
if c == 0:
continue
b[cur_row] = rhs
cur_row += 1
if use_sparse:
# Sparse matrix version
data, row, col = [], [], []
for tri in triplets:
row.append(tri[0])
col.append(tri[1])
data.append(tri[2])
data = np.array(data)
row = np.array(row, dtype=np.int)
col = np.array(col, dtype=np.int)
A = coo_matrix((data, (row, col)), shape=(num_param, num_param))
x = linalg.spsolve(A, b)
else:
# Dense matrix version
A = np.zeros((num_param, num_param))
# Set from triplets
for tri in triplets:
row = tri[0]
col = tri[1]
val = tri[2]
A[row, col] = val
x = np.linalg.solve(A, b)
for j in range(1, h-1):
for i in range(1, w-1):
c = mask[j, i]
if c == 0:
continue
idx = img2param_idx[get_idx(i, j)]
# setting z = √ h
depth[j, i] = np.sqrt(x[idx])
return depth
def visualizeDepth(depth, path='', dmin=0, dmax=50, cm_name='viridis'):
import matplotlib.pyplot as plt
cm = plt.get_cmap(cm_name)
colors = (np.array(cm.colors) * 255).astype(np.uint8)
colors = colors[..., ::-1] # -> BGR
normed = np.clip((depth - dmin) / (dmax - dmin), 0, 1)
normed = (normed * 255).astype(np.uint8)
vis = colors[normed]
if path != '':
cv2.imwrite(path, vis)
return vis
if __name__ == '__main__':
names = ['A', 'circle', 'character', 'hiragana', 'square']
for name in names:
mask_path = './data/' + name + '.png'
print(mask_path)
mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
mask[mask > 100] = 255
mask[mask <= 100] = 0
depth = inflationByBaran(mask)
visualizeDepth(depth, name + '_baran.jpg')
vertices, faces = depth2orthomesh(depth)
writeMeshAsPly(name + '_baran.ply', vertices, faces)
depth = inflationByDistanceTransform(mask)
visualizeDepth(depth, name + '_dist.jpg')
vertices, faces = depth2orthomesh(depth)
writeMeshAsPly(name + '_dist.ply', vertices, faces)
depth = inflationByDistanceTransform(mask, activation_tanh(0.02))
visualizeDepth(depth, name + '_dist_tanh.jpg')
vertices, faces = depth2orthomesh(depth)
writeMeshAsPly(name + '_dist_tanh.ply', vertices, faces)