dist_coeff=0.1
Browse files- handcrafted_solution.py +42 -13
- script.py +1 -0
handcrafted_solution.py
CHANGED
@@ -13,7 +13,6 @@ from hoho.color_mappings import gestalt_color_mapping
|
|
13 |
from hoho.read_write_colmap import read_cameras_binary, read_images_binary, read_points3D_binary
|
14 |
from scipy.spatial import KDTree
|
15 |
from scipy.spatial.distance import cdist
|
16 |
-
import scipy.cluster.hierarchy as shc
|
17 |
from sklearn.cluster import DBSCAN
|
18 |
|
19 |
apex_color = gestalt_color_mapping["apex"]
|
@@ -257,7 +256,7 @@ def get_vertices_and_edges_from_segmentation(gest_seg_np, *, point_radius=30, ma
|
|
257 |
for edge_class in ['eave',
|
258 |
'step_flashing',
|
259 |
'flashing',
|
260 |
-
'post',
|
261 |
'valley',
|
262 |
'hip',
|
263 |
'transition_line']:
|
@@ -358,15 +357,40 @@ def merge_vertices_3d(vert_edge_per_image, merge_th=0.1, **kwargs):
|
|
358 |
'''Merge vertices that are close to each other in 3D space and are of same types'''
|
359 |
all_3d_vertices = []
|
360 |
connections_3d = []
|
361 |
-
all_indexes = []
|
362 |
cur_start = 0
|
363 |
types = []
|
|
|
364 |
for cimg_idx, (vertices, connections, vertices_3d) in vert_edge_per_image.items():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
365 |
types += [int(v['type'] == 'apex') for v in vertices]
|
|
|
|
|
|
|
|
|
|
|
366 |
all_3d_vertices.append(vertices_3d)
|
367 |
-
connections_3d +=
|
368 |
cur_start += len(vertices_3d)
|
|
|
369 |
all_3d_vertices = np.concatenate(all_3d_vertices, axis=0)
|
|
|
|
|
|
|
|
|
370 |
# print (connections_3d)
|
371 |
distmat = cdist(all_3d_vertices, all_3d_vertices)
|
372 |
types = np.array(types).reshape(-1, 1)
|
@@ -430,7 +454,8 @@ def prune_not_connected(all_3d_vertices, connections_3d):
|
|
430 |
return np.array(new_verts), connected_out
|
431 |
|
432 |
|
433 |
-
def predict(entry, visualize=False, scale_estimation_coefficient=2.5, clustering_eps=100, **kwargs) -> Tuple[
|
|
|
434 |
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:
|
435 |
print('Missing required fields in the entry')
|
436 |
return (entry['__key__'], *empty_solution())
|
@@ -474,23 +499,23 @@ def predict(entry, visualize=False, scale_estimation_coefficient=2.5, clustering
|
|
474 |
)):
|
475 |
|
476 |
try:
|
477 |
-
gest_seg = gest.resize(depthcm.size)
|
478 |
-
gest_seg_np = np.array(
|
479 |
vertices, connections = get_vertices_and_edges_from_segmentation(gest_seg_np, **kwargs)
|
480 |
if (len(vertices) < 2) or (len(connections) < 1):
|
481 |
print(f'Not enough vertices or connections in image {i}')
|
482 |
vert_edge_per_image[i] = np.empty((0, 2)), [], np.empty((0, 3))
|
483 |
continue
|
484 |
belonging_points = []
|
485 |
-
for
|
486 |
-
if
|
487 |
-
belonging_points.append(entry["points3d"][
|
488 |
|
489 |
if len(belonging_points) < 1:
|
490 |
print(f'No 3D points in image {i}')
|
491 |
vert_edge_per_image[i] = np.empty((0, 2)), [], np.empty((0, 3))
|
492 |
raise KeyError
|
493 |
-
projected2d, _ = cv2.projectPoints(np.array([i.xyz for i in belonging_points]), R, t, K,
|
494 |
important = np.where(np.all(projected2d >= 0, axis=2))
|
495 |
# Normalize the uv to the camera intrinsics
|
496 |
world_to_cam = np.eye(4)
|
@@ -499,7 +524,7 @@ def predict(entry, visualize=False, scale_estimation_coefficient=2.5, clustering
|
|
499 |
|
500 |
homo_belonging_points = cv2.convertPointsToHomogeneous(np.array([i.xyz for i in belonging_points]))
|
501 |
depth = cv2.convertPointsFromHomogeneous(cv2.transform(homo_belonging_points, world_to_cam))
|
502 |
-
depth =
|
503 |
depth = depth[important[0]]
|
504 |
projected2d = projected2d[important]
|
505 |
if len(depth) < 1:
|
@@ -508,7 +533,8 @@ def predict(entry, visualize=False, scale_estimation_coefficient=2.5, clustering
|
|
508 |
raise KeyError
|
509 |
# print(projected2d.shape, depth.shape)
|
510 |
|
511 |
-
interpolator = si.NearestNDInterpolator(projected2d, depth)
|
|
|
512 |
|
513 |
vertex_coordinates = np.array([v['xy'] for v in vertices])
|
514 |
xi, yi = vertex_coordinates[:, 0], vertex_coordinates[:, 1]
|
@@ -530,6 +556,9 @@ def predict(entry, visualize=False, scale_estimation_coefficient=2.5, clustering
|
|
530 |
gest_seg_np = np.array(gest_seg).astype(np.uint8)
|
531 |
# Metric3D
|
532 |
depth_np = np.array(depthcm) / scale_estimation_coefficient
|
|
|
|
|
|
|
533 |
vertices, connections = get_vertices_and_edges_from_segmentation(gest_seg_np, **kwargs)
|
534 |
if (len(vertices) < 2) or (len(connections) < 1):
|
535 |
print(f'Not enough vertices or connections in image {i}')
|
|
|
13 |
from hoho.read_write_colmap import read_cameras_binary, read_images_binary, read_points3D_binary
|
14 |
from scipy.spatial import KDTree
|
15 |
from scipy.spatial.distance import cdist
|
|
|
16 |
from sklearn.cluster import DBSCAN
|
17 |
|
18 |
apex_color = gestalt_color_mapping["apex"]
|
|
|
256 |
for edge_class in ['eave',
|
257 |
'step_flashing',
|
258 |
'flashing',
|
259 |
+
# 'post',
|
260 |
'valley',
|
261 |
'hip',
|
262 |
'transition_line']:
|
|
|
357 |
'''Merge vertices that are close to each other in 3D space and are of same types'''
|
358 |
all_3d_vertices = []
|
359 |
connections_3d = []
|
|
|
360 |
cur_start = 0
|
361 |
types = []
|
362 |
+
|
363 |
for cimg_idx, (vertices, connections, vertices_3d) in vert_edge_per_image.items():
|
364 |
+
# remove nan values and remap the connections
|
365 |
+
connections = [[a, b]
|
366 |
+
for (a, b) in connections
|
367 |
+
if
|
368 |
+
not np.any(np.isnan(vertices_3d[a]))
|
369 |
+
and
|
370 |
+
not np.any(np.isnan(vertices_3d[b]))
|
371 |
+
]
|
372 |
+
left_vertex_indices = np.where(np.all(~np.isnan(vertices_3d), axis=1))[0]
|
373 |
+
|
374 |
+
new_indices = np.arange(len(left_vertex_indices))
|
375 |
+
|
376 |
+
new_vertex_mapping = dict(zip(left_vertex_indices, new_indices))
|
377 |
+
|
378 |
+
vertices = [v for i, v in enumerate(vertices) if i in new_vertex_mapping]
|
379 |
types += [int(v['type'] == 'apex') for v in vertices]
|
380 |
+
vertices_3d = vertices_3d[left_vertex_indices]
|
381 |
+
connections = [[new_vertex_mapping[a] + cur_start, new_vertex_mapping[b] + cur_start] for a, b in connections]
|
382 |
+
|
383 |
+
|
384 |
+
|
385 |
all_3d_vertices.append(vertices_3d)
|
386 |
+
connections_3d += connections
|
387 |
cur_start += len(vertices_3d)
|
388 |
+
|
389 |
all_3d_vertices = np.concatenate(all_3d_vertices, axis=0)
|
390 |
+
|
391 |
+
# dbscan = DBSCAN(eps=merge_th, min_samples=1).fit(all_3d_vertices)
|
392 |
+
# print(dbscan.core_sample_indices_)
|
393 |
+
# print(dbscan.labels_[dbscan.core_sample_indices_])
|
394 |
# print (connections_3d)
|
395 |
distmat = cdist(all_3d_vertices, all_3d_vertices)
|
396 |
types = np.array(types).reshape(-1, 1)
|
|
|
454 |
return np.array(new_verts), connected_out
|
455 |
|
456 |
|
457 |
+
def predict(entry, visualize=False, scale_estimation_coefficient=2.5, clustering_eps=100, dist_coeff=0, **kwargs) -> Tuple[
|
458 |
+
np.ndarray, List[int]]:
|
459 |
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:
|
460 |
print('Missing required fields in the entry')
|
461 |
return (entry['__key__'], *empty_solution())
|
|
|
499 |
)):
|
500 |
|
501 |
try:
|
502 |
+
# gest_seg = gest.resize(depthcm.size)
|
503 |
+
gest_seg_np = np.array(gest).astype(np.uint8)
|
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 |
belonging_points = []
|
510 |
+
for point_id in image_dict[imagekey].point3D_ids[np.where(image_dict[imagekey].point3D_ids != -1)]:
|
511 |
+
if point_id in biggest_cluster_keys:
|
512 |
+
belonging_points.append(entry["points3d"][point_id])
|
513 |
|
514 |
if len(belonging_points) < 1:
|
515 |
print(f'No 3D points in image {i}')
|
516 |
vert_edge_per_image[i] = np.empty((0, 2)), [], np.empty((0, 3))
|
517 |
raise KeyError
|
518 |
+
projected2d, _ = cv2.projectPoints(np.array([i.xyz for i in belonging_points]), R, t, K, dist_coeff)
|
519 |
important = np.where(np.all(projected2d >= 0, axis=2))
|
520 |
# Normalize the uv to the camera intrinsics
|
521 |
world_to_cam = np.eye(4)
|
|
|
524 |
|
525 |
homo_belonging_points = cv2.convertPointsToHomogeneous(np.array([i.xyz for i in belonging_points]))
|
526 |
depth = cv2.convertPointsFromHomogeneous(cv2.transform(homo_belonging_points, world_to_cam))
|
527 |
+
depth = depth[:, 0, 2]
|
528 |
depth = depth[important[0]]
|
529 |
projected2d = projected2d[important]
|
530 |
if len(depth) < 1:
|
|
|
533 |
raise KeyError
|
534 |
# print(projected2d.shape, depth.shape)
|
535 |
|
536 |
+
interpolator = si.NearestNDInterpolator(projected2d, depth, rescale=True)
|
537 |
+
# interpolator = si.CloughTocher2DInterpolator(projected2d, depth, np.nan)
|
538 |
|
539 |
vertex_coordinates = np.array([v['xy'] for v in vertices])
|
540 |
xi, yi = vertex_coordinates[:, 0], vertex_coordinates[:, 1]
|
|
|
556 |
gest_seg_np = np.array(gest_seg).astype(np.uint8)
|
557 |
# Metric3D
|
558 |
depth_np = np.array(depthcm) / scale_estimation_coefficient
|
559 |
+
cv2.GaussianBlur(depth_np, (21, 21), 1, depth_np)
|
560 |
+
# cv2.medianBlur(depth_np, 5)
|
561 |
+
# depth_np = np.zeros_like(depth_np)
|
562 |
vertices, connections = get_vertices_and_edges_from_segmentation(gest_seg_np, **kwargs)
|
563 |
if (len(vertices) < 2) or (len(connections) < 1):
|
564 |
print(f'Not enough vertices or connections in image {i}')
|
script.py
CHANGED
@@ -138,6 +138,7 @@ if __name__ == "__main__":
|
|
138 |
min_missing_distance=350.0,
|
139 |
scale_estimation_coefficient=2.54,
|
140 |
clustering_eps=100,
|
|
|
141 |
))
|
142 |
|
143 |
for i, result in enumerate(tqdm(results)):
|
|
|
138 |
min_missing_distance=350.0,
|
139 |
scale_estimation_coefficient=2.54,
|
140 |
clustering_eps=100,
|
141 |
+
dist_coeff=0.1,
|
142 |
))
|
143 |
|
144 |
for i, result in enumerate(tqdm(results)):
|