kcml commited on
Commit
7631607
·
1 Parent(s): bac280a

apply both colmap depthmap and mono depthmap.

Browse files
Files changed (1) hide show
  1. handcrafted_solution.py +41 -24
handcrafted_solution.py CHANGED
@@ -12,7 +12,7 @@ from scipy.spatial.distance import cdist
12
  from hoho.read_write_colmap import read_cameras_binary, read_images_binary, read_points3D_binary
13
  from hoho.color_mappings import gestalt_color_mapping, ade20k_color_mapping
14
 
15
- DUMP_IMG = True
16
  if DUMP_IMG:
17
  from scipy.sparse import random
18
 
@@ -140,27 +140,32 @@ def get_smooth_uv_depth(vertices, depth, gest_seg_np, sfm_depth_np):
140
  def get_local_depth(x,y, H, W, depth, r=5):
141
  '''return a smooth version of detph in radius r'''
142
  local_depths = []
143
- for i in range(max(0, x - r), min(W-1, x + r)):
144
- for j in range(max(0, y - r), min(H-1, y + r)):
145
  if np.sqrt((i - x)**2 + (j - y)**2) <= r:
146
- if sfm_depth_np[j, i] != 0:
147
- local_depths.append(sfm_depth_np[j, i])
 
 
 
148
  else:
149
  local_depths.append(depth[j, i])
 
150
  return local_depths
151
 
152
  vertex_depth = []
153
  for x,y in zip(a,b):
154
  local_depths = get_local_depth(x,y, H, W, depth, 5)
155
  #print(f'local_depths={local_depths}')
156
- local_mean = np.mean(local_depths)
 
157
  vertex_depth.append(local_mean)
158
 
159
  vertex_depth = np.array(vertex_depth)
160
  return uv, vertex_depth
161
 
162
  # TODO: timeit
163
- def get_SfM_depth(points3D, depth_np, gest_seg_np, K, R, t):
164
  '''Project 3D sfm pointcloud to the image plane '''
165
  H, W = depth_np.shape[:2]
166
  sfm_depth_np = np.zeros(depth_np.shape)
@@ -174,8 +179,18 @@ def get_SfM_depth(points3D, depth_np, gest_seg_np, K, R, t):
174
  Rt = np.concatenate( (R, t.reshape((3,1))), axis=1)
175
  world_to_cam = K @ Rt
176
  xyz = world_to_cam @ XYZ1.transpose()
177
- xyz = np.transpose(xyz)
178
- dilate_r = 5
 
 
 
 
 
 
 
 
 
 
179
  for pt, c in zip(xyz, rgb):
180
  x, y, z = pt
181
  u, v = x/z, y/z
@@ -183,16 +198,12 @@ def get_SfM_depth(points3D, depth_np, gest_seg_np, K, R, t):
183
  v = v.astype(np.int32)
184
  for i in range(max(0, u - dilate_r), min(W, u + dilate_r)):
185
  for j in range(max(0, v - dilate_r), min(H, v + dilate_r)):
186
- if z > 0: #and sfm_depth_np[j, i]!=0 and z < sfm_depth_np[j, i]:
187
- sfm_depth_np[j, i] = z
188
- sfm_color_np[j, i] = c
189
-
190
- '''
191
- if u >= 0 and u < W and v >= 0 and v < H and z > 0:
192
- if sfm_depth_np[v, u] == 0 or z < sfm_depth_np[v, u]:
193
- sfm_depth_np[v, u] = z
194
- sfm_color_np[v, u] = c
195
- '''
196
  if DUMP_IMG:
197
  filename_sfm_depth = 'sfm_depth.png'
198
  cv2.imwrite(filename_sfm_depth, sfm_depth_np/100)
@@ -340,6 +351,8 @@ def get_vertices_and_edges_from_two_segmentations(ade_seg_np, gest_seg_np, edge_
340
  if DUMP_IMG:
341
  filename_edges_map = f'edges_map_{rid}.jpg'
342
  cv2.imwrite(filename_edges_map, line_img)
 
 
343
  return vertices, connections
344
 
345
  def get_uv_dept_category(vertices, depth, ade_seg):
@@ -464,6 +477,7 @@ def predict(entry, visualize=False) -> Tuple[np.ndarray, List[int]]:
464
  elif i==2: # only visualize view 0,1
465
  continue
466
  '''
 
467
  depth_scale = 2.5
468
 
469
 
@@ -475,17 +489,20 @@ def predict(entry, visualize=False) -> Tuple[np.ndarray, List[int]]:
475
  depth_np = np.array(depth) / depth_scale # / 2.5 # 2.5 is the scale estimation coefficient # don't use 2.5...
476
  #vertices, connections = get_vertices_and_edges_from_segmentation(gest_seg_np, edge_th = 20.)
477
  vertices, connections = get_vertices_and_edges_from_two_segmentations(ade_seg_np, gest_seg_np, edge_th = 20.)
 
478
 
479
  if (len(vertices) < 2) or (len(connections) < 1):
480
- print (f'Not enough vertices or connections in image {i}')
481
  vert_edge_per_image[i] = np.empty((0, 2)), [], np.empty((0, 3))
482
  continue
483
 
484
 
485
- sfm_depth_np = get_SfM_depth(points3D, depth_np, gest_seg_np, K, R, t)
486
- #uv, depth_vert = get_uv_depth(vertices, depth_np)
487
- uv, depth_vert = get_smooth_uv_depth(vertices, depth_np, gest_seg_np, sfm_depth_np)
488
 
 
 
 
 
 
489
  # Normalize the uv to the camera intrinsics
490
  xy_local = np.ones((len(uv), 3))
491
  xy_local[:, 0] = (uv[:, 0] - K[0,2]) / K[0,0]
@@ -499,7 +516,7 @@ def predict(entry, visualize=False) -> Tuple[np.ndarray, List[int]]:
499
  vertices_3d = cv2.transform(cv2.convertPointsToHomogeneous(vertices_3d_local), cam_to_world)
500
  vertices_3d = cv2.convertPointsFromHomogeneous(vertices_3d).reshape(-1, 3)
501
  vert_edge_per_image[i] = vertices, connections, vertices_3d
502
- all_3d_vertices, connections_3d = merge_vertices_3d(vert_edge_per_image, 3.0) # TODO: 3cm looks too small
503
  #print(f'after merge, {len(all_3d_vertices)} 3d vertices and {len(connections_3d)} 3d connections')
504
  #all_3d_vertices_clean, connections_3d_clean = prune_not_connected(all_3d_vertices, connections_3d)
505
  all_3d_vertices_clean, connections_3d_clean = all_3d_vertices, connections_3d # don't prune -> cost:2.0
 
12
  from hoho.read_write_colmap import read_cameras_binary, read_images_binary, read_points3D_binary
13
  from hoho.color_mappings import gestalt_color_mapping, ade20k_color_mapping
14
 
15
+ DUMP_IMG = False
16
  if DUMP_IMG:
17
  from scipy.sparse import random
18
 
 
140
  def get_local_depth(x,y, H, W, depth, r=5):
141
  '''return a smooth version of detph in radius r'''
142
  local_depths = []
143
+ for i in range(max(0, x - r), min(W, x + r)):
144
+ for j in range(max(0, y - r), min(H, y + r)):
145
  if np.sqrt((i - x)**2 + (j - y)**2) <= r:
146
+ if sfm_depth_np is not None:
147
+ if sfm_depth_np[j, i] != 0:
148
+ local_depths.append(sfm_depth_np[j, i])
149
+ else:
150
+ local_depths.append(depth[j, i])
151
  else:
152
  local_depths.append(depth[j, i])
153
+
154
  return local_depths
155
 
156
  vertex_depth = []
157
  for x,y in zip(a,b):
158
  local_depths = get_local_depth(x,y, H, W, depth, 5)
159
  #print(f'local_depths={local_depths}')
160
+ #local_mean = np.mean(local_depths)
161
+ local_mean = np.min(local_depths)
162
  vertex_depth.append(local_mean)
163
 
164
  vertex_depth = np.array(vertex_depth)
165
  return uv, vertex_depth
166
 
167
  # TODO: timeit
168
+ def get_SfM_depth(points3D, depth_np, gest_seg_np, K, R, t, dilate_r = 5):
169
  '''Project 3D sfm pointcloud to the image plane '''
170
  H, W = depth_np.shape[:2]
171
  sfm_depth_np = np.zeros(depth_np.shape)
 
179
  Rt = np.concatenate( (R, t.reshape((3,1))), axis=1)
180
  world_to_cam = K @ Rt
181
  xyz = world_to_cam @ XYZ1.transpose()
182
+ xyz = np.transpose(xyz)
183
+ us, vs, zs = xyz[0]/xyz[2], xyz[1]/xyz[2], xyz[2]
184
+ us = us.astype(np.int32)
185
+ vs = vs.astype(np.int32)
186
+ for u,v,z,c in zip(us,vs,zs, rgb):
187
+ for i in range(max(0, u - dilate_r), min(W, u + dilate_r)):
188
+ for j in range(max(0, v - dilate_r), min(H, v + dilate_r)):
189
+ if z > 0:
190
+ if (sfm_depth_np[j, i]!=0 and z < sfm_depth_np[j, i]) or (sfm_depth_np[j, i]==0):
191
+ sfm_depth_np[j, i] = z
192
+ sfm_color_np[j, i] = c
193
+ '''
194
  for pt, c in zip(xyz, rgb):
195
  x, y, z = pt
196
  u, v = x/z, y/z
 
198
  v = v.astype(np.int32)
199
  for i in range(max(0, u - dilate_r), min(W, u + dilate_r)):
200
  for j in range(max(0, v - dilate_r), min(H, v + dilate_r)):
201
+ if z > 0:
202
+ if (sfm_depth_np[j, i]!=0 and z < sfm_depth_np[j, i]) or (sfm_depth_np[j, i]==0):
203
+ sfm_depth_np[j, i] = z
204
+ sfm_color_np[j, i] = c
205
+ '''
206
+
 
 
 
 
207
  if DUMP_IMG:
208
  filename_sfm_depth = 'sfm_depth.png'
209
  cv2.imwrite(filename_sfm_depth, sfm_depth_np/100)
 
351
  if DUMP_IMG:
352
  filename_edges_map = f'edges_map_{rid}.jpg'
353
  cv2.imwrite(filename_edges_map, line_img)
354
+
355
+
356
  return vertices, connections
357
 
358
  def get_uv_dept_category(vertices, depth, ade_seg):
 
477
  elif i==2: # only visualize view 0,1
478
  continue
479
  '''
480
+
481
  depth_scale = 2.5
482
 
483
 
 
489
  depth_np = np.array(depth) / depth_scale # / 2.5 # 2.5 is the scale estimation coefficient # don't use 2.5...
490
  #vertices, connections = get_vertices_and_edges_from_segmentation(gest_seg_np, edge_th = 20.)
491
  vertices, connections = get_vertices_and_edges_from_two_segmentations(ade_seg_np, gest_seg_np, edge_th = 20.)
492
+ #vertices, connections = get_vertices_and_edges_from_two_segmentations(ade_seg_np, gest_seg_np, edge_th = 50.)
493
 
494
  if (len(vertices) < 2) or (len(connections) < 1):
495
+ print (f'Not enough vertices ({len(vertices)}) or connections ({len(connections)}) in image {i}')
496
  vert_edge_per_image[i] = np.empty((0, 2)), [], np.empty((0, 3))
497
  continue
498
 
499
 
 
 
 
500
 
501
+ #uv, depth_vert = get_uv_depth(vertices, depth_np)
502
+ sfm_depth_np = get_SfM_depth(points3D, depth_np, gest_seg_np, K, R, t, 10)
503
+ uv, depth_vert = get_smooth_uv_depth(vertices, depth_np, gest_seg_np, sfm_depth_np)
504
+ #uv, depth_vert = get_smooth_uv_depth(vertices, depth_np, gest_seg_np, None)
505
+
506
  # Normalize the uv to the camera intrinsics
507
  xy_local = np.ones((len(uv), 3))
508
  xy_local[:, 0] = (uv[:, 0] - K[0,2]) / K[0,0]
 
516
  vertices_3d = cv2.transform(cv2.convertPointsToHomogeneous(vertices_3d_local), cam_to_world)
517
  vertices_3d = cv2.convertPointsFromHomogeneous(vertices_3d).reshape(-1, 3)
518
  vert_edge_per_image[i] = vertices, connections, vertices_3d
519
+ all_3d_vertices, connections_3d = merge_vertices_3d(vert_edge_per_image, 3.0) # TODO: 3cm looks too small
520
  #print(f'after merge, {len(all_3d_vertices)} 3d vertices and {len(connections_3d)} 3d connections')
521
  #all_3d_vertices_clean, connections_3d_clean = prune_not_connected(all_3d_vertices, connections_3d)
522
  all_3d_vertices_clean, connections_3d_clean = all_3d_vertices, connections_3d # don't prune -> cost:2.0