Siromanec commited on
Commit
aae4726
1 Parent(s): 14542ca

some depth maps are nov retrieved from the pointcloud

Browse files
Files changed (2) hide show
  1. handcrafted_solution.py +107 -41
  2. script.py +3 -3
handcrafted_solution.py CHANGED
@@ -5,7 +5,9 @@ from collections import defaultdict
5
  from typing import Tuple, List
6
 
7
  import cv2
 
8
  import numpy as np
 
9
  from PIL import Image as PImage
10
  from hoho.color_mappings import gestalt_color_mapping
11
  from hoho.read_write_colmap import read_cameras_binary, read_images_binary, read_points3D_binary
@@ -243,14 +245,19 @@ def get_vertices_and_edges_from_segmentation(gest_seg_np, *, point_radius=30, ma
243
 
244
  missed_vertices = []
245
  if len(ridge_edges) > 0 and len(rake_edges) > 0:
246
-
247
  inferred_vertices = infer_missing_vertices(ridge_edges, rake_edges)
248
  missed_vertices = get_missed_vertices(vertices, inferred_vertices, **kwargs)
249
  vertices = np.concatenate([vertices, missed_vertices])
250
 
251
  vertices = KDTree(vertices)
252
 
253
- for edge_class in ['eave', 'valley', 'flashing', 'step_flashing', 'hip']:
 
 
 
 
 
 
254
  class_edges, class_directions = get_lines_and_directions(gest_seg_np, edge_class,
255
  rho=rho,
256
  theta=theta,
@@ -334,6 +341,7 @@ def get_vertices_and_edges_from_segmentation(gest_seg_np, *, point_radius=30, ma
334
  def get_uv_depth(vertices, depth):
335
  '''Get the depth of the vertices from the depth image'''
336
 
 
337
  uv = np.array([v['xy'] for v in vertices])
338
  uv_int = uv.astype(np.int32)
339
  H, W = depth.shape[:2]
@@ -380,12 +388,10 @@ def merge_vertices_3d(vert_edge_per_image, merge_th=0.1, **kwargs):
380
  for vv in v:
381
  already_there.add(vv)
382
  old_idx_to_new = {}
383
- count = 0
384
- for idxs in merged:
385
  new_vertices.append(all_3d_vertices[idxs].mean(axis=0))
386
  for idx in idxs:
387
  old_idx_to_new[idx] = count
388
- count += 1
389
  # print (connections_3d)
390
  new_vertices = np.array(new_vertices)
391
  # print (connections_3d)
@@ -422,49 +428,109 @@ def prune_not_connected(all_3d_vertices, connections_3d):
422
 
423
 
424
  def predict(entry, visualize=False, scale_estimation_coefficient=2.5, **kwargs) -> Tuple[np.ndarray, List[int]]:
425
- good_entry = convert_entry_to_human_readable(entry)
426
- if 'gestalt' not in good_entry or 'depthcm' not in good_entry or 'K' not in good_entry or 'R' not in good_entry or 't' not in good_entry:
427
  print('Missing required fields in the entry')
428
- return (good_entry['__key__'], *empty_solution())
 
 
429
  vert_edge_per_image = {}
430
- for i, (gest, depth, K, R, t) in enumerate(zip(good_entry['gestalt'],
431
- good_entry['depthcm'],
432
- good_entry['K'],
433
- good_entry['R'],
434
- good_entry['t']
435
- )):
436
- gest_seg = gest.resize(depth.size)
437
- gest_seg_np = np.array(gest_seg).astype(np.uint8)
438
- # Metric3D
439
- depth_np = np.array(depth) / scale_estimation_coefficient
440
- vertices, connections = get_vertices_and_edges_from_segmentation(gest_seg_np, **kwargs)
441
- if (len(vertices) < 2) or (len(connections) < 1):
442
- print(f'Not enough vertices or connections in image {i}')
443
- vert_edge_per_image[i] = np.empty((0, 2)), [], np.empty((0, 3))
444
- continue
445
- uv, depth_vert = get_uv_depth(vertices, depth_np)
446
- # Normalize the uv to the camera intrinsics
447
- xy_local = np.ones((len(uv), 3))
448
- xy_local[:, 0] = (uv[:, 0] - K[0, 2]) / K[0, 0]
449
- xy_local[:, 1] = (uv[:, 1] - K[1, 2]) / K[1, 1]
450
- # Get the 3D vertices
451
- vertices_3d_local = depth_vert[..., None] * (xy_local / np.linalg.norm(xy_local, axis=1)[..., None])
452
- world_to_cam = np.eye(4)
453
- world_to_cam[:3, :3] = R
454
- world_to_cam[:3, 3] = t.reshape(-1)
455
- cam_to_world = np.linalg.inv(world_to_cam)
456
- vertices_3d = cv2.transform(cv2.convertPointsToHomogeneous(vertices_3d_local), cam_to_world)
457
- vertices_3d = cv2.convertPointsFromHomogeneous(vertices_3d).reshape(-1, 3)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
458
  vert_edge_per_image[i] = vertices, connections, vertices_3d
459
  all_3d_vertices, connections_3d = merge_vertices_3d(vert_edge_per_image, **kwargs)
460
- all_3d_vertices_clean, connections_3d_clean = prune_not_connected(all_3d_vertices, connections_3d)
 
461
  if (len(all_3d_vertices_clean) < 2) or len(connections_3d_clean) < 1:
462
  print(f'Not enough vertices or connections in the 3D vertices')
463
- return (good_entry['__key__'], *empty_solution())
464
  if visualize:
465
  from hoho.viz3d import plot_estimate_and_gt
466
  plot_estimate_and_gt(all_3d_vertices_clean,
467
  connections_3d_clean,
468
- good_entry['wf_vertices'],
469
- good_entry['wf_edges'])
470
- return good_entry['__key__'], all_3d_vertices_clean, connections_3d_clean
 
5
  from typing import Tuple, List
6
 
7
  import cv2
8
+ import hoho
9
  import numpy as np
10
+ import scipy.interpolate as si
11
  from PIL import Image as PImage
12
  from hoho.color_mappings import gestalt_color_mapping
13
  from hoho.read_write_colmap import read_cameras_binary, read_images_binary, read_points3D_binary
 
245
 
246
  missed_vertices = []
247
  if len(ridge_edges) > 0 and len(rake_edges) > 0:
 
248
  inferred_vertices = infer_missing_vertices(ridge_edges, rake_edges)
249
  missed_vertices = get_missed_vertices(vertices, inferred_vertices, **kwargs)
250
  vertices = np.concatenate([vertices, missed_vertices])
251
 
252
  vertices = KDTree(vertices)
253
 
254
+ for edge_class in ['eave',
255
+ 'step_flashing',
256
+ 'flashing',
257
+ 'post',
258
+ 'valley',
259
+ 'hip',
260
+ 'transition_line']:
261
  class_edges, class_directions = get_lines_and_directions(gest_seg_np, edge_class,
262
  rho=rho,
263
  theta=theta,
 
341
  def get_uv_depth(vertices, depth):
342
  '''Get the depth of the vertices from the depth image'''
343
 
344
+ # depth[depth > 5000] = np.inf
345
  uv = np.array([v['xy'] for v in vertices])
346
  uv_int = uv.astype(np.int32)
347
  H, W = depth.shape[:2]
 
388
  for vv in v:
389
  already_there.add(vv)
390
  old_idx_to_new = {}
391
+ for count, idxs in enumerate(merged):
 
392
  new_vertices.append(all_3d_vertices[idxs].mean(axis=0))
393
  for idx in idxs:
394
  old_idx_to_new[idx] = count
 
395
  # print (connections_3d)
396
  new_vertices = np.array(new_vertices)
397
  # print (connections_3d)
 
428
 
429
 
430
  def predict(entry, visualize=False, scale_estimation_coefficient=2.5, **kwargs) -> Tuple[np.ndarray, List[int]]:
431
+ if 'gestalt' not in entry or 'depthcm' not in entry or 'K' not in entry or 'R' not in entry or 't' not in entry:
 
432
  print('Missing required fields in the entry')
433
+ return (entry['__key__'], *empty_solution())
434
+ entry = hoho.decode(entry)
435
+
436
  vert_edge_per_image = {}
437
+ image_dict = {}
438
+ for k, v in entry["images"].items():
439
+ image_dict[v.name] = v
440
+ for i, (gest, depthcm, K, R, t, imagekey) in enumerate(zip(entry['gestalt'],
441
+ entry['depthcm'],
442
+ entry['K'],
443
+ entry['R'],
444
+ entry['t'],
445
+ entry['__imagekey__']
446
+ )):
447
+
448
+ try:
449
+ gest_seg = gest.resize(depthcm.size)
450
+ gest_seg_np = np.array(gest_seg).astype(np.uint8)
451
+ vertices, connections = get_vertices_and_edges_from_segmentation(gest_seg_np, **kwargs)
452
+ if (len(vertices) < 2) or (len(connections) < 1):
453
+ print(f'Not enough vertices or connections in image {i}')
454
+ vert_edge_per_image[i] = np.empty((0, 2)), [], np.empty((0, 3))
455
+ continue
456
+ belonging_points = []
457
+ for i in image_dict[imagekey].point3D_ids[np.where(image_dict[imagekey].point3D_ids != -1)]:
458
+ belonging_points.append(entry["points3d"][i])
459
+
460
+ if len(belonging_points) < 1:
461
+ print(f'No 3D points in image {i}')
462
+ vert_edge_per_image[i] = np.empty((0, 2)), [], np.empty((0, 3))
463
+ raise KeyError
464
+ projected2d, _ = cv2.projectPoints(np.array([i.xyz for i in belonging_points]), R, t, K, 0)
465
+ important = np.where(np.all(projected2d >= 0, axis=2))
466
+ # Normalize the uv to the camera intrinsics
467
+ world_to_cam = np.eye(4)
468
+ world_to_cam[:3, :3] = R
469
+ world_to_cam[:3, 3] = t
470
+
471
+ homo_belonging_points = cv2.convertPointsToHomogeneous(np.array([i.xyz for i in belonging_points]))
472
+ depth = cv2.convertPointsFromHomogeneous(cv2.transform(homo_belonging_points, world_to_cam))
473
+ depth = np.array([i[0][2] for i in depth])
474
+ depth = depth[important[0]]
475
+ projected2d = projected2d[important]
476
+ if len(depth) < 1:
477
+ print(f'No 3D points in image {i}')
478
+ vert_edge_per_image[i] = np.empty((0, 2)), [], np.empty((0, 3))
479
+ raise KeyError
480
+ # print(projected2d.shape, depth.shape)
481
+
482
+ interpolator = si.NearestNDInterpolator(projected2d, depth)
483
+
484
+ vertex_coordinates = np.array([v['xy'] for v in vertices])
485
+ xi, yi = vertex_coordinates[:, 0], vertex_coordinates[:, 1]
486
+ depth_vert = interpolator(xi, yi)
487
+ xy_local = np.ones((len(vertex_coordinates), 3))
488
+ xy_local[:, 0] = (vertex_coordinates[:, 0] - K[0, 2]) / K[0, 0]
489
+ xy_local[:, 1] = (vertex_coordinates[:, 1] - K[1, 2]) / K[1, 1]
490
+ # Get the 3D vertices
491
+ vertices_3d_local = depth_vert[..., None] * (xy_local / np.linalg.norm(xy_local, axis=1)[..., None])
492
+ world_to_cam = np.eye(4)
493
+ world_to_cam[:3, :3] = R
494
+ world_to_cam[:3, 3] = t.reshape(-1)
495
+ cam_to_world = np.linalg.inv(world_to_cam)
496
+ vertices_3d = cv2.transform(cv2.convertPointsToHomogeneous(vertices_3d_local), cam_to_world)
497
+ vertices_3d = cv2.convertPointsFromHomogeneous(vertices_3d).reshape(-1, 3)
498
+
499
+ except KeyError:
500
+ gest_seg = gest.resize(depthcm.size)
501
+ gest_seg_np = np.array(gest_seg).astype(np.uint8)
502
+ # Metric3D
503
+ depth_np = np.array(depthcm) / scale_estimation_coefficient
504
+ vertices, connections = get_vertices_and_edges_from_segmentation(gest_seg_np, **kwargs)
505
+ if (len(vertices) < 2) or (len(connections) < 1):
506
+ print(f'Not enough vertices or connections in image {i}')
507
+ vert_edge_per_image[i] = np.empty((0, 2)), [], np.empty((0, 3))
508
+ continue
509
+ uv, depth_vert = get_uv_depth(vertices, depth_np)
510
+ # Normalize the uv to the camera intrinsics
511
+ xy_local = np.ones((len(uv), 3))
512
+ xy_local[:, 0] = (uv[:, 0] - K[0, 2]) / K[0, 0]
513
+ xy_local[:, 1] = (uv[:, 1] - K[1, 2]) / K[1, 1]
514
+ # Get the 3D vertices
515
+ vertices_3d_local = depth_vert[..., None] * (xy_local / np.linalg.norm(xy_local, axis=1)[..., None])
516
+ world_to_cam = np.eye(4)
517
+ world_to_cam[:3, :3] = R
518
+ world_to_cam[:3, 3] = t.reshape(-1)
519
+ cam_to_world = np.linalg.inv(world_to_cam)
520
+ vertices_3d = cv2.transform(cv2.convertPointsToHomogeneous(vertices_3d_local), cam_to_world)
521
+ vertices_3d = cv2.convertPointsFromHomogeneous(vertices_3d).reshape(-1, 3)
522
+
523
  vert_edge_per_image[i] = vertices, connections, vertices_3d
524
  all_3d_vertices, connections_3d = merge_vertices_3d(vert_edge_per_image, **kwargs)
525
+ all_3d_vertices_clean, connections_3d_clean = all_3d_vertices, connections_3d
526
+ # all_3d_vertices_clean, connections_3d_clean = prune_not_connected(all_3d_vertices, connections_3d)
527
  if (len(all_3d_vertices_clean) < 2) or len(connections_3d_clean) < 1:
528
  print(f'Not enough vertices or connections in the 3D vertices')
529
+ return (entry['__key__'], *empty_solution())
530
  if visualize:
531
  from hoho.viz3d import plot_estimate_and_gt
532
  plot_estimate_and_gt(all_3d_vertices_clean,
533
  connections_3d_clean,
534
+ entry['wf_vertices'],
535
+ entry['wf_edges'])
536
+ return entry['__key__'], all_3d_vertices_clean, connections_3d_clean
script.py CHANGED
@@ -132,9 +132,9 @@ if __name__ == "__main__":
132
  point_radius=25,
133
  max_angle=15,
134
  extend=30,
135
- merge_th=3.0,
136
- min_missing_distance=300.0,
137
- scale_estimation_coefficient=4.5,
138
  ))
139
 
140
  for i, result in enumerate(tqdm(results)):
 
132
  point_radius=25,
133
  max_angle=15,
134
  extend=30,
135
+ merge_th=100.0,
136
+ min_missing_distance=30000000.0,
137
+ scale_estimation_coefficient=2.54,
138
  ))
139
 
140
  for i, result in enumerate(tqdm(results)):