-
Notifications
You must be signed in to change notification settings - Fork 11
/
homography_warping.py
292 lines (243 loc) · 12.3 KB
/
homography_warping.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Differentiable homography related.
"""
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]='2'
import tensorflow as tf
import numpy as np
def get_homographies_Twc(left_cam, right_cam, depth_num, depth_start, depth_interval):
with tf.name_scope('get_homographies'):
# input Twc[Rwc|twc]
# cameras (K, R, t)
R_left = tf.slice(left_cam, [0, 0, 0, 0], [-1, 1, 3, 3]) # Rwc
R_right = tf.slice(right_cam, [0, 0, 0, 0], [-1, 1, 3, 3]) # Rwc
t_left = tf.slice(left_cam, [0, 0, 0, 3], [-1, 1, 3, 1]) # twc
t_right = tf.slice(right_cam, [0, 0, 0, 3], [-1, 1, 3, 1]) # twc
K_left = tf.slice(left_cam, [0, 1, 0, 0], [-1, 1, 3, 3]) # K
K_right = tf.slice(right_cam, [0, 1, 0, 0], [-1, 1, 3, 3]) # K
# depth
depth_num = tf.reshape(tf.cast(depth_num, 'int32'), [])
depth = depth_start + tf.cast(tf.range(depth_num), tf.float32) * depth_interval
# preparation
num_depth = tf.shape(depth)[0]
K_left_inv = tf.matrix_inverse(tf.squeeze(K_left, axis=1)) # K-1
R_left_trans = tf.transpose(tf.squeeze(R_left, axis=1), perm=[0, 2, 1]) # Rcw
R_right_trans = tf.transpose(tf.squeeze(R_right, axis=1), perm=[0, 2, 1]) # Rcw
fronto_direction = tf.slice(R_left_trans, [0, 2, 0], [-1, 1, 3]) # (B, D, 1, 3) # Rcw
c_relative = tf.subtract(tf.squeeze(t_left, axis=1), tf.squeeze(t_right, axis=1)) # twc1-twc2
# compute
batch_size = tf.shape(R_left)[0]
temp_vec = tf.matmul(c_relative, fronto_direction)
depth_mat = tf.tile(tf.reshape(depth, [batch_size, num_depth, 1, 1]), [1, 1, 3, 3])
temp_vec = tf.tile(tf.expand_dims(temp_vec, axis=1), [1, num_depth, 1, 1])
middle_mat0 = tf.eye(3, batch_shape=[batch_size, num_depth]) - temp_vec / depth_mat
middle_mat1 = tf.tile(tf.expand_dims(tf.matmul(tf.squeeze(R_left, axis=1), K_left_inv), axis=1), [1, num_depth, 1, 1]) #Rwc
middle_mat2 = tf.matmul(middle_mat0, middle_mat1)
homographies = tf.matmul(tf.tile(K_right, [1, num_depth, 1, 1])
, tf.matmul(tf.tile(tf.expand_dims(R_right_trans, 1), [1, num_depth, 1, 1]) #Rcw
, middle_mat2))
return homographies
def get_homographies(left_cam, right_cam, depth_num, depth_start, depth_interval):
with tf.name_scope('get_homographies'):
# input Tcw[Rcw|tcw]
# cameras (K, R, t)
R_left = tf.slice(left_cam, [0, 0, 0, 0], [-1, 1, 3, 3])
R_right = tf.slice(right_cam, [0, 0, 0, 0], [-1, 1, 3, 3])
t_left = tf.slice(left_cam, [0, 0, 0, 3], [-1, 1, 3, 1])
t_right = tf.slice(right_cam, [0, 0, 0, 3], [-1, 1, 3, 1])
K_left = tf.slice(left_cam, [0, 1, 0, 0], [-1, 1, 3, 3])
K_right = tf.slice(right_cam, [0, 1, 0, 0], [-1, 1, 3, 3])
# depth
depth_num = tf.reshape(tf.cast(depth_num, 'int32'), [])
depth = depth_start + tf.cast(tf.range(depth_num), tf.float32) * depth_interval
# preparation
num_depth = tf.shape(depth)[0]
K_left_inv = tf.matrix_inverse(tf.squeeze(K_left, axis=1)) #K-1
R_left_trans = tf.transpose(tf.squeeze(R_left, axis=1), perm=[0, 2, 1])
R_right_trans = tf.transpose(tf.squeeze(R_right, axis=1), perm=[0, 2, 1])
fronto_direction = tf.slice(tf.squeeze(R_left, axis=1), [0, 2, 0], [-1, 1, 3]) # (B, D, 1, 3)
c_left = -tf.matmul(R_left_trans, tf.squeeze(t_left, axis=1))
c_right = -tf.matmul(R_right_trans, tf.squeeze(t_right, axis=1)) # (B, D, 3, 1)
c_relative = tf.subtract(c_right, c_left)
# compute
batch_size = tf.shape(R_left)[0]
temp_vec = tf.matmul(c_relative, fronto_direction)
depth_mat = tf.tile(tf.reshape(depth, [batch_size, num_depth, 1, 1]), [1, 1, 3, 3])
temp_vec = tf.tile(tf.expand_dims(temp_vec, axis=1), [1, num_depth, 1, 1])
middle_mat0 = tf.eye(3, batch_shape=[batch_size, num_depth]) - temp_vec / depth_mat
middle_mat1 = tf.tile(tf.expand_dims(tf.matmul(R_left_trans, K_left_inv), axis=1), [1, num_depth, 1, 1])
middle_mat2 = tf.matmul(middle_mat0, middle_mat1)
homographies = tf.matmul(tf.tile(K_right, [1, num_depth, 1, 1])
, tf.matmul(tf.tile(R_right, [1, num_depth, 1, 1])
, middle_mat2))
return homographies
def get_homographies_inv_depth(left_cam, right_cam, depth_num, depth_start, depth_end):
with tf.name_scope('get_homographies'):
# cameras (K, R, t)
R_left = tf.slice(left_cam, [0, 0, 0, 0], [-1, 1, 3, 3])
R_right = tf.slice(right_cam, [0, 0, 0, 0], [-1, 1, 3, 3])
t_left = tf.slice(left_cam, [0, 0, 0, 3], [-1, 1, 3, 1])
t_right = tf.slice(right_cam, [0, 0, 0, 3], [-1, 1, 3, 1])
K_left = tf.slice(left_cam, [0, 1, 0, 0], [-1, 1, 3, 3])
K_right = tf.slice(right_cam, [0, 1, 0, 0], [-1, 1, 3, 3])
# depth
depth_num = tf.reshape(tf.cast(depth_num, 'int32'), [])
inv_depth_start = tf.reshape(tf.div(1.0, depth_start), [])
inv_depth_end = tf.reshape(tf.div(1.0, depth_end), [])
inv_depth = tf.lin_space(inv_depth_start, inv_depth_end, depth_num)
depth = tf.div(1.0, inv_depth)
# preparation
num_depth = tf.shape(depth)[0]
K_left_inv = tf.matrix_inverse(tf.squeeze(K_left, axis=1))
R_left_trans = tf.transpose(tf.squeeze(R_left, axis=1), perm=[0, 2, 1])
R_right_trans = tf.transpose(tf.squeeze(R_right, axis=1), perm=[0, 2, 1])
fronto_direction = tf.slice(tf.squeeze(R_left, axis=1), [0, 2, 0], [-1, 1, 3]) # (B, D, 1, 3)
c_left = -tf.matmul(R_left_trans, tf.squeeze(t_left, axis=1))
c_right = -tf.matmul(R_right_trans, tf.squeeze(t_right, axis=1)) # (B, D, 3, 1)
c_relative = tf.subtract(c_right, c_left)
# compute
batch_size = tf.shape(R_left)[0]
temp_vec = tf.matmul(c_relative, fronto_direction)
depth_mat = tf.tile(tf.reshape(depth, [batch_size, num_depth, 1, 1]), [1, 1, 3, 3])
temp_vec = tf.tile(tf.expand_dims(temp_vec, axis=1), [1, num_depth, 1, 1])
middle_mat0 = tf.eye(3, batch_shape=[batch_size, num_depth]) - temp_vec / depth_mat
middle_mat1 = tf.tile(tf.expand_dims(tf.matmul(R_left_trans, K_left_inv), axis=1), [1, num_depth, 1, 1])
middle_mat2 = tf.matmul(middle_mat0, middle_mat1)
homographies = tf.matmul(tf.tile(K_right, [1, num_depth, 1, 1])
, tf.matmul(tf.tile(R_right, [1, num_depth, 1, 1])
, middle_mat2))
return homographies
def get_pixel_grids(height, width):
# texture coordinate
x_linspace = tf.linspace(0.5, tf.cast(width, 'float32') - 0.5, width)
y_linspace = tf.linspace(0.5, tf.cast(height, 'float32') - 0.5, height)
x_coordinates, y_coordinates = tf.meshgrid(x_linspace, y_linspace)
x_coordinates = tf.reshape(x_coordinates, [-1])
y_coordinates = tf.reshape(y_coordinates, [-1])
ones = tf.ones_like(x_coordinates)
indices_grid = tf.concat([x_coordinates, y_coordinates, ones], 0)
return indices_grid
def repeat_int(x, num_repeats):
ones = tf.ones((1, num_repeats), dtype='int32')
x = tf.reshape(x, shape=(-1, 1))
x = tf.matmul(x, ones)
return tf.reshape(x, [-1])
def repeat_float(x, num_repeats):
ones = tf.ones((1, num_repeats), dtype='float')
x = tf.reshape(x, shape=(-1, 1))
x = tf.matmul(x, ones)
return tf.reshape(x, [-1])
def interpolate(image, x, y):
image_shape = tf.shape(image)
batch_size = image_shape[0]
height =image_shape[1]
width = image_shape[2]
# image coordinate to pixel coordinate
x = x - 0.5
y = y - 0.5
x0 = tf.cast(tf.floor(x), 'int32')
x1 = x0 + 1
y0 = tf.cast(tf.floor(y), 'int32')
y1 = y0 + 1
max_y = tf.cast(height - 1, dtype='int32')
max_x = tf.cast(width - 1, dtype='int32')
x0 = tf.clip_by_value(x0, 0, max_x)
x1 = tf.clip_by_value(x1, 0, max_x)
y0 = tf.clip_by_value(y0, 0, max_y)
y1 = tf.clip_by_value(y1, 0, max_y)
b = repeat_int(tf.range(batch_size), height * width)
indices_a = tf.stack([b, y0, x0], axis=1)
indices_b = tf.stack([b, y0, x1], axis=1)
indices_c = tf.stack([b, y1, x0], axis=1)
indices_d = tf.stack([b, y1, x1], axis=1)
pixel_values_a = tf.gather_nd(image, indices_a)
pixel_values_b = tf.gather_nd(image, indices_b)
pixel_values_c = tf.gather_nd(image, indices_c)
pixel_values_d = tf.gather_nd(image, indices_d)
x0 = tf.cast(x0, 'float32')
x1 = tf.cast(x1, 'float32')
y0 = tf.cast(y0, 'float32')
y1 = tf.cast(y1, 'float32')
area_a = tf.expand_dims(((y1 - y) * (x1 - x)), 1)
area_b = tf.expand_dims(((y1 - y) * (x - x0)), 1)
area_c = tf.expand_dims(((y - y0) * (x1 - x)), 1)
area_d = tf.expand_dims(((y - y0) * (x - x0)), 1)
output = tf.add_n([area_a * pixel_values_a,
area_b * pixel_values_b,
area_c * pixel_values_c,
area_d * pixel_values_d])
return output
def homography_warping(input_image, homography):
with tf.name_scope('warping_by_homography'):
image_shape = tf.shape(input_image)
batch_size = image_shape[0]
height = image_shape[1]
width = image_shape[2]
# turn homography to affine_mat of size (B, 2, 3) and div_mat of size (B, 1, 3)
affine_mat = tf.slice(homography, [0, 0, 0], [-1, 2, 3])
div_mat = tf.slice(homography, [0, 2, 0], [-1, 1, 3])
# generate pixel grids of size (B, 3, (W+1) x (H+1))
pixel_grids = get_pixel_grids(height, width)
pixel_grids = tf.expand_dims(pixel_grids, 0)
pixel_grids = tf.tile(pixel_grids, [batch_size, 1])
pixel_grids = tf.reshape(pixel_grids, (batch_size, 3, -1))
# return pixel_grids
# affine + divide tranform, output (B, 2, (W+1) x (H+1))
grids_affine = tf.matmul(affine_mat, pixel_grids)
grids_div = tf.matmul(div_mat, pixel_grids)
grids_zero_add = tf.cast(tf.equal(grids_div, 0.0), dtype='float32') * 1e-7 # handle div 0
grids_div = grids_div + grids_zero_add
grids_div = tf.tile(grids_div, [1, 2, 1])
grids_inv_warped = tf.div(grids_affine, grids_div)
x_warped, y_warped = tf.unstack(grids_inv_warped, axis=1)
x_warped_flatten = tf.reshape(x_warped, [-1])
y_warped_flatten = tf.reshape(y_warped, [-1])
# interpolation
warped_image = interpolate(input_image, x_warped_flatten, y_warped_flatten)
warped_image = tf.reshape(warped_image, shape=image_shape, name='warped_feature')
# return input_image
return warped_image
def tf_transform_homography(input_image, homography):
# tf.contrib.image.transform is for pixel coordinate but our
# homograph parameters are for image coordinate (x_p = x_i + 0.5).
# So need to change the corresponding homography parameters
homography = tf.reshape(homography, [-1, 9])
a0 = tf.slice(homography, [0, 0], [-1, 1])
a1 = tf.slice(homography, [0, 1], [-1, 1])
a2 = tf.slice(homography, [0, 2], [-1, 1])
b0 = tf.slice(homography, [0, 3], [-1, 1])
b1 = tf.slice(homography, [0, 4], [-1, 1])
b2 = tf.slice(homography, [0, 5], [-1, 1])
c0 = tf.slice(homography, [0, 6], [-1, 1])
c1 = tf.slice(homography, [0, 7], [-1, 1])
c2 = tf.slice(homography, [0, 8], [-1, 1])
a_0 = a0 - c0 / 2
a_1 = a1 - c1 / 2
a_2 = (a0 + a1) / 2 + a2 - (c0 + c1) / 4 - c2 / 2
b_0 = b0 - c0 / 2
b_1 = b1 - c1 / 2
b_2 = (b0 + b1) / 2 + b2 - (c0 + c1) / 4 - c2 / 2
c_0 = c0
c_1 = c1
c_2 = c2 + (c0 + c1) / 2
homo = []
homo.append(a_0)
homo.append(a_1)
homo.append(a_2)
homo.append(b_0)
homo.append(b_1)
homo.append(b_2)
homo.append(c_0)
homo.append(c_1)
homo.append(c_2)
homography = tf.stack(homo, axis=1)
homography = tf.reshape(homography, [-1, 9])
homography_linear = tf.slice(homography, begin=[0, 0], size=[-1, 8])
homography_linear_div = tf.tile(tf.slice(homography, begin=[0, 8], size=[-1, 1]), [1, 8])
homography_linear = tf.div(homography_linear, homography_linear_div)
warped_image = tf.contrib.image.transform(
input_image, homography_linear, interpolation='BILINEAR')
# return input_image
return warped_image