Commit ·
f35c1a6
1
Parent(s): 6ff27f2
final
Browse files
tools2025/hoho2025/example_solutions.py
CHANGED
|
@@ -373,75 +373,96 @@ def create_3d_wireframe_single_image(vertices: List[dict],
|
|
| 373 |
return vertices_3d
|
| 374 |
|
| 375 |
|
| 376 |
-
def merge_vertices_3d(vert_edge_per_image, th=0.5):
|
| 377 |
'''Merge vertices that are close to each other in 3D space and are of same types'''
|
| 378 |
# Initialize structures to collect vertices and connections from all images
|
| 379 |
-
|
|
|
|
| 380 |
connections_3d = []
|
| 381 |
-
all_indexes = []
|
| 382 |
cur_start = 0
|
| 383 |
-
types = []
|
| 384 |
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
|
|
|
|
|
|
|
|
|
|
| 393 |
# Calculate distance matrix between all vertices
|
| 394 |
distmat = cdist(all_3d_vertices, all_3d_vertices)
|
| 395 |
-
|
| 396 |
-
same_types = cdist(types, types)
|
| 397 |
|
| 398 |
# Create mask for vertices that should be merged (close in space and same type)
|
| 399 |
-
mask_to_merge = (distmat <= th) & (same_types==0)
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
# Calculate new vertex positions (average of merged groups)
|
| 428 |
old_idx_to_new = {}
|
| 429 |
-
count=0
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 445 |
|
| 446 |
|
| 447 |
def prune_not_connected(all_3d_vertices, connections_3d, keep_largest=True):
|
|
@@ -712,6 +733,27 @@ def validate_edges_with_colmap(all_3d_vertices, connections_3d, colmap_rec,
|
|
| 712 |
|
| 713 |
return list(set(validated_connections))
|
| 714 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 715 |
|
| 716 |
def predict_wireframe(entry) -> Tuple[np.ndarray, List[int]]:
|
| 717 |
"""
|
|
@@ -767,6 +809,14 @@ def predict_wireframe(entry) -> Tuple[np.ndarray, List[int]]:
|
|
| 767 |
|
| 768 |
all_3d_vertices_clean, connections_3d_clean = prune_not_connected(all_3d_vertices, connections_3d, keep_largest=False)
|
| 769 |
all_3d_vertices_clean, connections_3d_clean = prune_too_far(all_3d_vertices_clean, connections_3d_clean, colmap_rec, th=3.0) # default=4.0, 3.0*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 770 |
|
| 771 |
if (len(all_3d_vertices_clean) < 2) or len(connections_3d_clean) < 1:
|
| 772 |
print (f'Not enough vertices or connections in the 3D vertices')
|
|
|
|
| 373 |
return vertices_3d
|
| 374 |
|
| 375 |
|
| 376 |
+
def merge_vertices_3d(vert_edge_per_image, th = 0.5):
|
| 377 |
'''Merge vertices that are close to each other in 3D space and are of same types'''
|
| 378 |
# Initialize structures to collect vertices and connections from all images
|
| 379 |
+
all_3d_vertices_list = []
|
| 380 |
+
vertex_data_for_typing = []
|
| 381 |
connections_3d = []
|
|
|
|
| 382 |
cur_start = 0
|
|
|
|
| 383 |
|
| 384 |
+
for _img_idx, (img_vertex_data, img_conns, img_3d_coords) in vert_edge_per_image.items():
|
| 385 |
+
if len(img_3d_coords) == 0:
|
| 386 |
+
continue
|
| 387 |
+
all_3d_vertices_list.append(img_3d_coords)
|
| 388 |
+
vertex_data_for_typing.extend(img_vertex_data)
|
| 389 |
+
for v1_local, v2_local in img_conns:
|
| 390 |
+
connections_3d.append((v1_local + cur_start, v2_local + cur_start))
|
| 391 |
+
cur_start += len(img_3d_coords)
|
| 392 |
+
|
| 393 |
+
if not vertex_data_for_typing:
|
| 394 |
+
return np.empty((0, 3)), []
|
| 395 |
+
|
| 396 |
+
all_3d_vertices = np.concatenate(all_3d_vertices_list, axis=0)
|
| 397 |
+
if all_3d_vertices.shape[0] == 0:
|
| 398 |
+
return np.empty((0, 3)), []
|
| 399 |
+
|
| 400 |
+
unique_str_types = sorted(list(set(v_data['type'] for v_data in vertex_data_for_typing)))
|
| 401 |
+
type_to_int_map = {str_type: i for i, str_type in enumerate(unique_str_types)}
|
| 402 |
|
| 403 |
+
types_list_int = [type_to_int_map.get(v_data['type'], -1) for v_data in vertex_data_for_typing]
|
| 404 |
+
types = np.array(types_list_int).reshape(-1, 1)
|
| 405 |
+
|
| 406 |
# Calculate distance matrix between all vertices
|
| 407 |
distmat = cdist(all_3d_vertices, all_3d_vertices)
|
| 408 |
+
same_types = cdist(types, types)
|
|
|
|
| 409 |
|
| 410 |
# Create mask for vertices that should be merged (close in space and same type)
|
| 411 |
+
mask_to_merge = (distmat <= th) & (same_types == 0)
|
| 412 |
+
|
| 413 |
+
num_original_vertices = len(all_3d_vertices)
|
| 414 |
+
parent = list(range(num_original_vertices))
|
| 415 |
+
|
| 416 |
+
def find_set(vertex_idx):
|
| 417 |
+
if vertex_idx == parent[vertex_idx]:
|
| 418 |
+
return vertex_idx
|
| 419 |
+
parent[vertex_idx] = find_set(parent[vertex_idx])
|
| 420 |
+
return parent[vertex_idx]
|
| 421 |
+
|
| 422 |
+
def unite_sets(idx_a, idx_b):
|
| 423 |
+
root_a = find_set(idx_a)
|
| 424 |
+
root_b = find_set(idx_b)
|
| 425 |
+
if root_a != root_b:
|
| 426 |
+
parent[root_b] = root_a
|
| 427 |
+
|
| 428 |
+
for i in range(num_original_vertices):
|
| 429 |
+
for j in range(i + 1, num_original_vertices):
|
| 430 |
+
if mask_to_merge[i, j]:
|
| 431 |
+
unite_sets(i, j)
|
| 432 |
+
|
| 433 |
+
merged_groups_map = defaultdict(list)
|
| 434 |
+
for i in range(num_original_vertices):
|
| 435 |
+
root = find_set(i)
|
| 436 |
+
merged_groups_map[root].append(i)
|
| 437 |
+
|
| 438 |
+
new_vertices_list = []
|
|
|
|
| 439 |
old_idx_to_new = {}
|
| 440 |
+
count = 0
|
| 441 |
+
|
| 442 |
+
for original_indices_group in merged_groups_map.values():
|
| 443 |
+
merged_coord = np.mean(all_3d_vertices[original_indices_group], axis=0)
|
| 444 |
+
new_vertices_list.append(merged_coord)
|
| 445 |
+
for old_idx in original_indices_group:
|
| 446 |
+
old_idx_to_new[old_idx] = count
|
| 447 |
+
count += 1
|
| 448 |
+
|
| 449 |
+
if not new_vertices_list:
|
| 450 |
+
return np.empty((0, 3)), []
|
| 451 |
+
|
| 452 |
+
new_vertices = np.array(new_vertices_list)
|
| 453 |
+
|
| 454 |
+
new_connections_set = set()
|
| 455 |
+
for v1_orig, v2_orig in connections_3d:
|
| 456 |
+
new_v1 = old_idx_to_new.get(v1_orig)
|
| 457 |
+
new_v2 = old_idx_to_new.get(v2_orig)
|
| 458 |
+
|
| 459 |
+
if new_v1 is not None and new_v2 is not None:
|
| 460 |
+
if new_v1 == new_v2:
|
| 461 |
+
continue
|
| 462 |
+
edge = tuple(sorted((new_v1, new_v2)))
|
| 463 |
+
new_connections_set.add(edge)
|
| 464 |
+
|
| 465 |
+
return new_vertices, list(new_connections_set)
|
| 466 |
|
| 467 |
|
| 468 |
def prune_not_connected(all_3d_vertices, connections_3d, keep_largest=True):
|
|
|
|
| 733 |
|
| 734 |
return list(set(validated_connections))
|
| 735 |
|
| 736 |
+
def refine_vertex_positions_weighted_avg(vertices_3d, colmap_rec,
|
| 737 |
+
search_radius=0.3,
|
| 738 |
+
min_colmap_points_for_avg=1,
|
| 739 |
+
colmap_weight=0.5):
|
| 740 |
+
if len(vertices_3d) == 0: return vertices_3d
|
| 741 |
+
sfm_points_list = [p3D.xyz for pid, p3D in colmap_rec.points3D.items()] if colmap_rec and colmap_rec.points3D else []
|
| 742 |
+
if not sfm_points_list: return vertices_3d
|
| 743 |
+
|
| 744 |
+
sfm_points_np = np.array(sfm_points_list)
|
| 745 |
+
sfm_kdtree = KDTree(sfm_points_np)
|
| 746 |
+
|
| 747 |
+
new_vertices_3d = np.copy(vertices_3d)
|
| 748 |
+
refined_count = 0
|
| 749 |
+
for i, vertex_coord in enumerate(vertices_3d):
|
| 750 |
+
nearby_indices = sfm_kdtree.query_ball_point(vertex_coord, r=search_radius)
|
| 751 |
+
if len(nearby_indices) >= min_colmap_points_for_avg:
|
| 752 |
+
centroid_colmap = np.mean(sfm_points_np[nearby_indices], axis=0)
|
| 753 |
+
new_vertices_3d[i] = (1 - colmap_weight) * vertex_coord + colmap_weight * centroid_colmap
|
| 754 |
+
refined_count += 1
|
| 755 |
+
return new_vertices_3d
|
| 756 |
+
|
| 757 |
|
| 758 |
def predict_wireframe(entry) -> Tuple[np.ndarray, List[int]]:
|
| 759 |
"""
|
|
|
|
| 809 |
|
| 810 |
all_3d_vertices_clean, connections_3d_clean = prune_not_connected(all_3d_vertices, connections_3d, keep_largest=False)
|
| 811 |
all_3d_vertices_clean, connections_3d_clean = prune_too_far(all_3d_vertices_clean, connections_3d_clean, colmap_rec, th=3.0) # default=4.0, 3.0*
|
| 812 |
+
|
| 813 |
+
all_3d_vertices_clean = refine_vertex_positions_weighted_avg(
|
| 814 |
+
all_3d_vertices_clean,
|
| 815 |
+
colmap_rec,
|
| 816 |
+
search_radius=0.3,
|
| 817 |
+
min_colmap_points_for_avg=1,
|
| 818 |
+
colmap_weight=0.5
|
| 819 |
+
)
|
| 820 |
|
| 821 |
if (len(all_3d_vertices_clean) < 2) or len(connections_3d_clean) < 1:
|
| 822 |
print (f'Not enough vertices or connections in the 3D vertices')
|