kcml commited on
Commit
3fc5c6f
·
1 Parent(s): f111e99

code to clean pcd, but roof points seem critical

Browse files
Files changed (1) hide show
  1. handcrafted_solution.py +31 -4
handcrafted_solution.py CHANGED
@@ -8,6 +8,8 @@ import cv2
8
  from typing import Tuple, List
9
 
10
  from scipy.spatial.distance import cdist
 
 
11
 
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
@@ -267,14 +269,14 @@ def fill_range(u, v, z, dilate_r, c, sfm_depth_np, sfm_color_np, H, W):
267
  return sfm_depth_np, sfm_color_np
268
  ''''''
269
 
270
- def get_SfM_depth(points3D, depth_np, gest_seg_np, K, R, t, dilate_r = 5):
271
  '''Project 3D sfm pointcloud to the image plane '''
272
  H, W = depth_np.shape[:2]
273
  sfm_depth_np = np.zeros(depth_np.shape)
274
  sfm_color_np = np.zeros(gest_seg_np.shape)
275
  #XYZ1 = np.stack([(p.xyz, 1) for p in points3D.values()])
276
- XYZ = np.stack([p.xyz for p in points3D.values()])
277
- rgb = np.stack([p.rgb for p in points3D.values()])
278
  #print('XYZ is ', XYZ.shape)
279
  XYZ1 = np.concatenate((XYZ, np.ones((len(XYZ), 1))), axis=1)
280
  #print('XYZ1 is ', XYZ1.shape)
@@ -770,9 +772,34 @@ def clean_gest(gest_seg_np):
770
 
771
  return gest_seg_np
772
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
773
  def predict(entry, visualize=False, prune_dist_thr=600, depth_scale=2.5, ) -> Tuple[np.ndarray, List[int]]:
774
  good_entry = convert_entry_to_human_readable(entry)
775
  points3D = good_entry['points3d']
 
 
 
776
  vert_edge_per_image = {}
777
  for i, (ade, gest, depth, K, R, t) in enumerate(zip(
778
  good_entry['ade20k'],
@@ -816,7 +843,7 @@ def predict(entry, visualize=False, prune_dist_thr=600, depth_scale=2.5, ) -> Tu
816
  continue
817
 
818
  #uv, depth_vert = get_uv_depth(vertices, depth_np)
819
- sfm_depth_np = get_SfM_depth(points3D, depth_np, gest_seg_np, K, R, t, 5) # Sensitive. 10 is worse than 0 in testset
820
  uv, depth_vert = get_smooth_uv_depth(vertices, depth_np, gest_seg_np, sfm_depth_np, 75)
821
  #uv, depth_vert = get_smooth_uv_depth(vertices, depth_np, gest_seg_np, None)
822
  #print('uv:', uv, ' d:', depth_vert)
 
8
  from typing import Tuple, List
9
 
10
  from scipy.spatial.distance import cdist
11
+ from sklearn import metrics
12
+ from sklearn.cluster import DBSCAN
13
 
14
  from hoho.read_write_colmap import read_cameras_binary, read_images_binary, read_points3D_binary
15
  from hoho.color_mappings import gestalt_color_mapping, ade20k_color_mapping
 
269
  return sfm_depth_np, sfm_color_np
270
  ''''''
271
 
272
+ def get_SfM_depth(XYZ, rgb, depth_np, gest_seg_np, K, R, t, dilate_r = 5):
273
  '''Project 3D sfm pointcloud to the image plane '''
274
  H, W = depth_np.shape[:2]
275
  sfm_depth_np = np.zeros(depth_np.shape)
276
  sfm_color_np = np.zeros(gest_seg_np.shape)
277
  #XYZ1 = np.stack([(p.xyz, 1) for p in points3D.values()])
278
+ #XYZ = np.stack([p.xyz for p in points3D.values()])
279
+ #rgb = np.stack([p.rgb for p in points3D.values()])
280
  #print('XYZ is ', XYZ.shape)
281
  XYZ1 = np.concatenate((XYZ, np.ones((len(XYZ), 1))), axis=1)
282
  #print('XYZ1 is ', XYZ1.shape)
 
772
 
773
  return gest_seg_np
774
 
775
+ def clean_PCD(XYZ, rgb):
776
+ db = DBSCAN(eps=150, min_samples=20).fit(XYZ)
777
+ labels = db.labels_
778
+ unique_labels = set(labels)
779
+ core_samples_mask = np.zeros_like(labels, dtype=bool)
780
+ center_thr = 500
781
+ retain_class_mask = labels == -2
782
+
783
+ for k in unique_labels:
784
+ if k == -1:
785
+ continue
786
+ class_member_mask = labels == k
787
+ pt_k = XYZ[class_member_mask]
788
+ Xmean = np.mean(pt_k[:,0])
789
+ Ymean = np.mean(pt_k[:,1])
790
+ if Xmean < center_thr and Ymean < center_thr:
791
+ retain_class_mask = retain_class_mask | class_member_mask
792
+
793
+ XYZ = XYZ[retain_class_mask]
794
+ rgb = rgb[retain_class_mask]
795
+ return XYZ, rgb
796
+
797
  def predict(entry, visualize=False, prune_dist_thr=600, depth_scale=2.5, ) -> Tuple[np.ndarray, List[int]]:
798
  good_entry = convert_entry_to_human_readable(entry)
799
  points3D = good_entry['points3d']
800
+ XYZ = np.stack([p.xyz for p in points3D.values()])
801
+ rgb = np.stack([p.rgb for p in points3D.values()])
802
+ #XYZ, rgb = clean_PCD(XYZ, rgb)
803
  vert_edge_per_image = {}
804
  for i, (ade, gest, depth, K, R, t) in enumerate(zip(
805
  good_entry['ade20k'],
 
843
  continue
844
 
845
  #uv, depth_vert = get_uv_depth(vertices, depth_np)
846
+ sfm_depth_np = get_SfM_depth(XYZ, rgb, depth_np, gest_seg_np, K, R, t, 5) # Sensitive. 10 is worse than 0 in testset
847
  uv, depth_vert = get_smooth_uv_depth(vertices, depth_np, gest_seg_np, sfm_depth_np, 75)
848
  #uv, depth_vert = get_smooth_uv_depth(vertices, depth_np, gest_seg_np, None)
849
  #print('uv:', uv, ' d:', depth_vert)